Str*_*keW 17 jsp el session-scope
public class LoginAction extends ActionSupport {
private String username;
private String password;
@Override
public String execute() throws Exception {
ActionContext ctx = ActionContext.getContext();
Integer counter = (Integer)ctx.getApplication().get("counter");
// put counter into application
ctx.getApplication().put("counter", counter);
// put username into session
ctx.getSession().put("user", username);
if (getUsername().equals("crazyit.org")
&& getPassword().equals("leegang")) {
ctx.put("tip", "Login Success! ");
return SUCCESS;
}
else {
ctx.put("tip", "Login Falied!");
return ERROR;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我把"counter"在应用"user"会话和"tip"中ActionContext.在JSP中,我可以使用${session.user}和${sessionScope.user}引用"user"属性.${request.tip}并${requestScope.tip}参考tip.
我的问题:
sessionScope,requestScope,applicationScope在EL?ActionContext和request(requestScope)?之间的关系?PS:
我测试${request == requestScope}哪个是真的,这意味着它们是一样的?
Dip*_*ole 21
默认情况下page, request, session and application,JSP页面可以使用对象.因此,您可以使用EL语法访问.
下表显示了EL可用的IMPLICIT对象.
Implicit object Description
1. pageScope Scoped variables from page scope
2. requestScope Scoped variables from request scope
3. sessionScope Scoped variables from session scope
4. applicationScope Scoped variables from application scope
5. param Request parameters as strings
6. paramValues Request parameters as collections of strings
7. header HTTP request headers as strings
8. headerValues HTTP request headers as collections of strings
9. initParam Context-initialization parameters
10. cookie Cookie values
11. pageContext The JSP PageContext object for the current page
Run Code Online (Sandbox Code Playgroud)
因此,会议和sessionScope是相同的,但在不同的情况下,他们是专门used.More session is object和sessionScope is map (key, value) of Attribute and its value.
如果你说它${session.sessionAttr}引用了JSP页面可用的会话对象.
如果你说它${sessionScope.sessionAttr}指的是EL可用的IMPLICIT会话对象.
Rus*_*ton 21
使用表达式语言(EL),范围项是它们引用的对象中属性的值映射.例如,requestScope是请求对象中值的映射表示.本页非常详细地解释了这一点:Java Servlet和JSP.如果您通读EL部分,您会注意到请求与请求范围有关:requestScope不是请求对象.
我建议通过这个页面阅读,以便更好地理解servlet/jsp.
至于ActionContext如何与这些项相关,它实际上是struts用来封装servlet的包装器.您可以在此处阅读有关它的更多细节:访问应用程序,会话,请求对象.
这里给出了隐含值的一些参考,但我觉得只是说隐含的内容并没有真正解释得太多.使用EL访问servlet变量时,可以显式声明要引用的范围,例如:
${requestScope.myVariable}
Run Code Online (Sandbox Code Playgroud)
您也可以通过省略范围来隐式引用它:
${myVariable}
Run Code Online (Sandbox Code Playgroud)
现在,这里可能出现的问题是具有相同名称的变量可能导致冲突.EL将按特定顺序检查隐式值:pageScope,requestScope,sessionScope和applicationScope,param,paramValues,header,headervalues,initParam,cookie,pageContext.这意味着,如果请求范围中的变量与会话或应用程序范围中的变量具有相同的名称,则将引用请求范围的值.
| 归档时间: |
|
| 查看次数: |
42852 次 |
| 最近记录: |