jsp的范围:useBean

发布于 2024-08-14 06:43:35 字数 345 浏览 4 评论 0原文

home.jsp

<jsp:useBean id="username" class="java.lang.String" scope="application"/>

<%
      username="Jitendra";
%>

<jsp:include page="include.jsp"/>

include.jsp

<%=username%>

这给出了一个错误,指出“username”在 include.jsp 中未定义,即使 Bean 的范围是 application...

home.jsp

<jsp:useBean id="username" class="java.lang.String" scope="application"/>

<%
      username="Jitendra";
%>

<jsp:include page="include.jsp"/>

include.jsp

<%=username%>

This gives an error saying “username” is undefined in include.jsp, even though the scope of Bean is application…

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剪不断理还乱 2024-08-21 06:43:35

至于您的问题,您使用旧式脚本在本地声明的任何内容都jsp:useBean链接。此外,声明本地 scriptlet 变量在包含的页面中可见,您需要至少将它们明确地放入请求范围中。因为使用 scriptlet 是一种不好的做法。我建议完全忘记它。

在您的具体情况下,只需创建一个真实 java bean 来保存数据。也就是说,具有(隐式)默认构造函数和由公共 getter/setter 公开的私有属性的类。下面是一个基本示例:

public class User {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

然后您可以使用 servlet 类来预处理请求。为此,您可以使用 servlet 的 doGet() 方法。

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
    User user = new User();
    user.setName("Jitendra");
    request.setAttribute("user", user); // Store in request scope.
    request.getRequestDispatcher("/WEB-INF/show.jsp").forward(request, response);
}

将此 Servlet 映射到 web.xml 中的 url-pattern(例如 /show)。然后,该 servlet 应该可以通过 http://example.com/context/show 访问,并且它的 doGet() 将立即执行。

然后更改/创建 JSP 文件 show.jsp 并将其放置在 /WEB-INF 中以防止直接访问(以便客户端无法通过 http 访问它: //example.com/context/show.jsp 但被“强制”调用 servlet)并使用以下行:

<p>User name: ${user.name}</p>

${user} 引用与以下对象关联的对象任何请求/会话/应用程序属性键user。这在幕后进行 jspContext.findAttribute(“用户”)。由于返回的 User 实例符合 javabean 规范,因此 ${user.name} 将调用 上的 getName() 方法User 实例和 EL 将显示其结果。

哦,我应该补充一点,您不需要为此需要jsp:useBean,因为 servlet 已经创建并将所需的 bean 放入范围中。

也就是说,我建议从一本像样的 JSP/Servlet 教程/书籍开始。示例:

希望这有帮助。

As to your problem, anything which you declare locally using the old fashioned scriptlets is not linked with a jsp:useBean. Also, declaring a local scriptlet variable is not visible in the included pages, you need to explicitly put them in at least the request scope. As using scriptlets is a bad practice. I recommend to forget about it at all.

In your specific case, just create a real java bean to hold the data. That is, a class with an (implicit) default constructor and private properties which are exposed by public getters/setters. Here's a basic example:

public class User {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Then you can use a servlet class to preprocess requests. You can use servlet's doGet() method for this.

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
    User user = new User();
    user.setName("Jitendra");
    request.setAttribute("user", user); // Store in request scope.
    request.getRequestDispatcher("/WEB-INF/show.jsp").forward(request, response);
}

Map this servlet in web.xml on an url-pattern of for example /show. This servlet should then be accessible by http://example.com/context/show and its doGet() would be executed immediately.

Then change/create the JSP file show.jsp which you place in /WEB-INF to prevent from direct access (so that clients cannot access it by http://example.com/context/show.jsp but are "forced" to call the servlet) with the following line:

<p>User name: ${user.name}</p>

The ${user} refers to the object which is associated with any request/session/application attribute key user. This does behind the scenes jspContext.findAttribute("user"). As the returned User instance conforms the javabean spec, the ${user.name} will invoke the getName() method on the User instance and the EL will display its outcome.

Oh, I should add, you do not need jsp:useBean for this as the servlet has already created and put the desired bean in the scope.

That said, I recommend to start with a decent JSP/Servlet tutorial/book. Examples:

Hope this helps.

墟烟 2024-08-21 06:43:35

您的代码是“邪恶的”,因为 java.lang.String 并不是真正的 bean。值得注意的是,它没有“set”方法来更改其文本(这是故意的,它应该是不可变的)。

访问 bean 的方法是声明一个 bean,然后使用它的属性(即 get() 和 set() 方法后面的名称)来更改它。您不能直接更改实际的 bean 实例,只能更改其值。

Your code is "evil" because a java.lang.String is not really a bean. Significantly, it doesn't have a "set" method to change its text (that's intentional, it's meant to be immutable).

The way you access beans is to declare one, and then use its properties (i.e. the names behind the get() and set() methods) to change it. You cannot directly change the actual bean instance, only its values.

找个人就嫁了吧 2024-08-21 06:43:35

在 Tomcat 6 上,home.jsp 被转换为 Servlet 代码:

java.lang.String username = null;
synchronized (application) {
  username = (java.lang.String) _jspx_page_context.getAttribute("username", 
                                                  PageContext.APPLICATION_SCOPE);
  if (username == null){
    username = new java.lang.String();
    _jspx_page_context.setAttribute("username", username,
                                                  PageContext.APPLICATION_SCOPE);
  }
}

username="Jitendra"; 

org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, 
                                                 "include.jsp", out, false);

虽然行为相同,但生成的确切代码取决于应用程序服务器实现。

本地 username 变量不会扩展到将从 include.jsp 生成的 Servlet 中。您没有将值“Jitendra”设置到应用程序范围中,仅设置局部变量的值。

正如其他人所指出的,不可变的 String 并不能成为一个很好的 bean。

On Tomcat 6, home.jsp is translated to the Servlet code:

java.lang.String username = null;
synchronized (application) {
  username = (java.lang.String) _jspx_page_context.getAttribute("username", 
                                                  PageContext.APPLICATION_SCOPE);
  if (username == null){
    username = new java.lang.String();
    _jspx_page_context.setAttribute("username", username,
                                                  PageContext.APPLICATION_SCOPE);
  }
}

username="Jitendra"; 

org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, 
                                                 "include.jsp", out, false);

Although the behaviour is the same, the exact code generated depends on the application server implementation.

The scope of the local username variable does not extend into the Servlet that will be generated from include.jsp. You are not setting the value "Jitendra" into the application scope, only setting the value of the local variable.

As others have pointed out, an immutable String does not make a very good bean.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文