第01步:编写bean类
package com.self.bean; import java.util.Date; public class User {
private Date birthday = null; public Date getBirthday() {
System.out.println("获得-User:" + birthday);
return birthday;
} public void setBirthday(Date birthday) {
System.out.println("设置-User:" + birthday);
this.birthday = birthday;
} }
第02步:编写action类
package com.self.action; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext;
import com.self.bean.User; public class ThreeScopes { private User lilan; public ThreeScopes() {
} /**向3种范围加入值**/
public String showdata() {
/**方式01**/
ActionContext ac= ActionContext.getContext();
ac.put("request", "请求范围01");//向request范围加入值
ac.getSession().put("session", "会话范围01");//向Session范围加入值
ac.getApplication().put("app", "应用范围01");//向应用范围加入值 /**方式02**/
ServletContext sc=ServletActionContext.getServletContext();//应用范围
HttpServletRequest request=ServletActionContext.getRequest();//会话范围
request.setAttribute("request2", "请求范围2");
request.getSession().setAttribute("session2", "会话范围2");
sc.setAttribute("app2", "应用范围2");
return "tsuccess";
} public User getLilan() {
return lilan;
} public void setLilan(User lilan) {
this.lilan = lilan;
} }
第03步:配置struts.xml
<?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>
<constant name="struts.action.extension" value="do"/> <package name="transform" namespace="/" extends="struts-default">
<action name="list_*" class="com.self.action.ThreeScopes" method="{1}">
<result name="tsuccess">
/outdata.jsp
</result>
</action>
</package>
</struts>
第04步:编写访问action界面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<body>
<form action="list_showdata.do">
<BR>
<input type="submit" value="访问action,显示结果">
</form>
</body>
</html>
第05步:编写数据显示界面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<body>
访问不同范围属性值01:<BR>
${applicationScope.app }<BR>
${sessionScope.session }<BR>
${requestScope.request }<BR><BR><BR>
访问不同范围属性值02:<BR>
${applicationScope.app2 }<BR>
${sessionScope.session2 }<BR>
${requestScope.request2 }<BR>
</body>
</html>