一个struts2的request问题, 非常奇怪。
有一个TestAction类packagecom.jin.action;importcom.jin.bean.User;importcom.opensymphony.xw...
有一个TestAction类
package com.jin.action;
import com.jin.bean.User;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport
{
private User user;
public User getUser()
{
return user;
}
public void setUser(User user)
{
this.user = user;
}
public String execute() throws Exception
{
user = new User("安切洛蒂", "christmas tree");
return SUCCESS;
}
}
User类
package com.jin.bean;
public class User
{
private String username;
private String password;
public User(){};
public User(String username, String password)
{
this.username = username;
this.password = password;
}
//...getter和setter..
}
用户访问testAction时,跳转到print.jsp
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="myStruts" extends="struts-default">
<action name="testAction" class="com.jin.action.TestAction">
<result>/print.jsp</result>
</action>
</package>
</struts>
print.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Print Test Page</title>
</head>
<body>
<h1><font color="red">输出测试页面</font></h1>
姓名:<s:property value="user.username"/><br>
密码:<s:property value="user.password"/><br>
<hr>
姓名:${requestScope.user.username}<br>
</body>
</html>
当我直接访问http://localhost:8080/ognl/testAction.action时,页面输出为:
姓名:安切洛蒂
密码:christmas tree
-------------------
姓名:安切洛蒂
前面两个输出结果如我所料。因为每当一个request到来时,struts2会自动产生一个ValueStack实例,并且把被访问的action实例放入ValueStack中,所以在print.jsp中,我可以用ognl输出action中user信息。
但后面那个使用jspEL表达式竟然也可以输出,就令我感到想当费解。我访问testAction.action时,并没有传递参数。为什么这里可以拿到${requestScope.user.username}请求对象request里的user信息呢?
难道struts2也自动把user放到了request中了吗?请问是怎么回事呢? 展开
package com.jin.action;
import com.jin.bean.User;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport
{
private User user;
public User getUser()
{
return user;
}
public void setUser(User user)
{
this.user = user;
}
public String execute() throws Exception
{
user = new User("安切洛蒂", "christmas tree");
return SUCCESS;
}
}
User类
package com.jin.bean;
public class User
{
private String username;
private String password;
public User(){};
public User(String username, String password)
{
this.username = username;
this.password = password;
}
//...getter和setter..
}
用户访问testAction时,跳转到print.jsp
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="myStruts" extends="struts-default">
<action name="testAction" class="com.jin.action.TestAction">
<result>/print.jsp</result>
</action>
</package>
</struts>
print.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Print Test Page</title>
</head>
<body>
<h1><font color="red">输出测试页面</font></h1>
姓名:<s:property value="user.username"/><br>
密码:<s:property value="user.password"/><br>
<hr>
姓名:${requestScope.user.username}<br>
</body>
</html>
当我直接访问http://localhost:8080/ognl/testAction.action时,页面输出为:
姓名:安切洛蒂
密码:christmas tree
-------------------
姓名:安切洛蒂
前面两个输出结果如我所料。因为每当一个request到来时,struts2会自动产生一个ValueStack实例,并且把被访问的action实例放入ValueStack中,所以在print.jsp中,我可以用ognl输出action中user信息。
但后面那个使用jspEL表达式竟然也可以输出,就令我感到想当费解。我访问testAction.action时,并没有传递参数。为什么这里可以拿到${requestScope.user.username}请求对象request里的user信息呢?
难道struts2也自动把user放到了request中了吗?请问是怎么回事呢? 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询