java jsp的作用域:useBean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1855507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scope of jsp:useBean
提问by TheLameProgrammer
home.jsp
主页.jsp
<jsp:useBean id="username" class="java.lang.String" scope="application"/>
<%
username="Jitendra";
%>
<jsp:include page="include.jsp"/>
include.jsp
包含.jsp
<%=username%>
This gives an error saying “username” is undefined in include.jsp, even though the scope of Bean is application…
这给出了一个错误,指出“用户名”在 include.jsp 中未定义,即使 Bean 的范围是应用程序......
回答by BalusC
As to your problem, anything which you declare locally using the old fashioned scriptlets is notlinked with a jsp:useBean. Also, declaring a local scriptlet variable is notvisible 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.
至于您的问题,您使用老式脚本在本地声明的任何内容都不与jsp:useBean. 此外,声明本地 scriptlet 变量在包含的页面中不可见,您至少需要将它们明确地放在请求范围内。因为使用 scriptlet 是一种不好的做法。我建议完全忘记它。
In your specific case, just create a realjava 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:
在您的特定情况下,只需创建一个真正的java bean 来保存数据。也就是说,具有(隐式)默认构造函数和由公共 getter/setter 公开的私有属性的类。这是一个基本示例:
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.
然后您可以使用 servlet 类来预处理请求。您可以doGet()为此使用 servlet 的方法。
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.xmlon an url-patternof for example /show. This servlet should then be accessible by http://example.com/context/showand its doGet()would be executed immediately.
将此 servlet 映射web.xml到一个url-pattern例如/show。然后应该可以访问这个 servlet,http://example.com/context/show并且它doGet()会立即执行。
Then change/create the JSP file show.jspwhich you place in /WEB-INFto prevent from direct access (so that clients cannot access it by http://example.com/context/show.jspbut are "forced" to call the servlet) with the following line:
然后使用以下行更改/创建show.jsp您放置的 JSP 文件/WEB-INF以防止直接访问(以便客户端无法访问它http://example.com/context/show.jsp但“被迫”调用 servlet):
<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 Userinstance conforms the javabean spec, the ${user.name}will invoke the getName()method on the Userinstance and the EL will display its outcome.
的${user}指的是与任何请求/会话/应用属性键相关联的对象user。这在幕后呢jspContext.findAttribute("user")。由于返回的User实例符合 javabean 规范,因此${user.name}将调用实例getName()上的方法,UserEL 将显示其结果。
Oh, I should add, you do notneed jsp:useBeanfor this as the servlet has already created and put the desired bean in the scope.
哦,我要补充的,你就不会需要jsp:useBean这个作为servlet已经创建,并把所需的bean的范畴。
That said, I recommend to start with a decent JSP/Servlet tutorial/book. Examples:
也就是说,我建议从一本不错的 JSP/Servlet 教程/书籍开始。例子:
- Sun Java EE tutorial part II chapters 1-8.
- Coreservlets.com JSP/Servlet tutorials.
- Head First Servlets & JSP book.
Hope this helps.
希望这可以帮助。
回答by McDowell
On Tomcat 6, home.jspis translated to the Servlet code:
在 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);
Although the behaviour is the same, the exact code generated depends on the application server implementation.
尽管行为相同,但生成的确切代码取决于应用程序服务器实现。
The scope of the local usernamevariable 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.
局部username变量的范围不会扩展到将从 生成的 Servlet 中include.jsp。您没有将值“Jitendra”设置到应用程序范围内,只是设置了局部变量的值。
As others have pointed out, an immutable String does not make a very good bean.
正如其他人指出的那样,不可变的 String 并不是一个很好的 bean。
回答by Carl Smotricz
Your code is "evil" because a java.lang.Stringis 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).
您的代码是“邪恶的”,因为 ajava.lang.String并不是真正的 bean。值得注意的是,它没有“设置”方法来更改其文本(这是有意的,它意味着不可变)。
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.
访问bean 的方式是声明一个bean,然后使用它的属性(即get() 和set() 方法后面的名称)来更改它。您不能直接更改实际的 bean 实例,只能更改其值。

