Struts2中访问web元素的四种方式及前台jsp页面获取后台值的方式
四种方式:
1. 通过ActionContext来访问request,session,application对象
2. 通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象
3. 通过ServletActionContext来访问request,session,application对象
4. 通过实现ServletRequestAware接口来访问request,session,application对象
演示代码:
方式一:
- /**
- * 通过ActionContext来访问request,session,application对象
- * @author 健
- */
- public class UserAction1 extends ActionSupport{
- /**
- * 序列化
- */
- private static final long serialVersionUID = 1L;
- /**
- * request
- */
- private Map <String,Object>request;
- /**
- * response
- */
- private Map <String,Object>session;
- /**
- * application
- */
- private Map <String,Object>application;
- /**
- * 添加用户
- * @return 用户是否添加成功
- */
- @SuppressWarnings("unchecked")
- @Override
- public String execute(){
- System.out.println("通过ActionContext来访问request,session,application对象");
- // 初始化
- request = (Map<String,Object>)ActionContext.getContext().get("request");
- session = ActionContext.getContext().getSession();
- application = ActionContext.getContext().getApplication();
- // 赋值
- request.put("requestKey", "requestValue");
- session.put("sessionKey", "sessionValue");
- application.put("applicationKey", "applicationValue");
- return "success";
- }
- }
- /**
- * 通过ActionContext来访问request,session,application对象
- * @author 健
- */
- public class UserAction1 extends ActionSupport{
- /**
- * 序列化
- */
- private static final long serialVersionUID = 1L;
- /**
- * request
- */
- private Map <String,Object>request;
- /**
- * response
- */
- private Map <String,Object>session;
- /**
- * application
- */
- private Map <String,Object>application;
- /**
- * 添加用户
- * @return 用户是否添加成功
- */
- @SuppressWarnings("unchecked")
- @Override
- public String execute(){
- System.out.println("通过ActionContext来访问request,session,application对象");
- // 初始化
- request = (Map<String,Object>)ActionContext.getContext().get("request");
- session = ActionContext.getContext().getSession();
- application = ActionContext.getContext().getApplication();
- // 赋值
- request.put("requestKey", "requestValue");
- session.put("sessionKey", "sessionValue");
- application.put("applicationKey", "applicationValue");
- return "success";
- }
- }
方式二:
- /**
- * 通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象
- * @author 健
- */
- public class UserAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
- /**
- * 序列化
- */
- private static final long serialVersionUID = 1L;
- /**
- * request
- */
- private Map <String,Object>request;
- /**
- * response
- */
- private Map <String,Object>session;
- /**
- * application
- */
- private Map <String,Object>application;
- /**
- * 控制器
- */
- @SuppressWarnings("unchecked")
- @Override
- public String execute(){
- System.out.println("通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象");
- // 赋值
- request.put("requestKey", "requestValue");
- session.put("sessionKey", "sessionValue");
- application.put("applicationKey", "applicationValue");
- return "success";
- }
- /*
- * 实现RequestAware中的方法
- */
- @Override
- public void setRequest(Map<String, Object> request) {
- this.request = request;
- }
- /*
- * 实现ApplicationAware中的方法
- */
- @Override
- public void setApplication(Map<String, Object> application) {
- this.application = application;
- }
- /*
- * 实现SessionAware中的方法
- */
- @Override
- public void setSession(Map<String, Object> session) {
- this.session = session;
- }
- }
- /**
- * 通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象
- * @author 健
- */
- public class UserAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
- /**
- * 序列化
- */
- private static final long serialVersionUID = 1L;
- /**
- * request
- */
- private Map <String,Object>request;
- /**
- * response
- */
- private Map <String,Object>session;
- /**
- * application
- */
- private Map <String,Object>application;
- /**
- * 控制器
- */
- @SuppressWarnings("unchecked")
- @Override
- public String execute(){
- System.out.println("通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象");
- // 赋值
- request.put("requestKey", "requestValue");
- session.put("sessionKey", "sessionValue");
- application.put("applicationKey", "applicationValue");
- return "success";
- }
- /*
- * 实现RequestAware中的方法
- */
- @Override
- public void setRequest(Map<String, Object> request) {
- this.request = request;
- }
- /*
- * 实现ApplicationAware中的方法
- */
- @Override
- public void setApplication(Map<String, Object> application) {
- this.application = application;
- }
- /*
- * 实现SessionAware中的方法
- */
- @Override
- public void setSession(Map<String, Object> session) {
- this.session = session;
- }
- }
方式三:
- +/**
- * 通过ServletActionContext来访问request,session,application对象
- * @author 健
- */
- public class UserAction3 extends ActionSupport{
- /**
- * 序列化
- */
- private static final long serialVersionUID = 1L;
- /**
- * request
- */
- private HttpServletRequest request;
- /**
- * response
- */
- private HttpSession session;
- /**
- * application
- */
- private ServletContext application;
- /**
- * 控制器
- */
- @SuppressWarnings("unchecked")
- @Override
- public String execute(){
- System.out.println("通过ServletActionContext来访问request,session,application对象");
- // 初始化
- request = ServletActionContext.getRequest();
- session = request.getSession();
- application = session.getServletContext();
- // 赋值
- request.setAttribute("requestKey", "requestValue");
- session.setAttribute("sessionKey", "sessionValue");
- application.setAttribute("applicationKey", "applicationValue");
- return "success";
- }
- }
- +/**
- * 通过ServletActionContext来访问request,session,application对象
- * @author 健
- */
- public class UserAction3 extends ActionSupport{
- /**
- * 序列化
- */
- private static final long serialVersionUID = 1L;
- /**
- * request
- */
- private HttpServletRequest request;
- /**
- * response
- */
- private HttpSession session;
- /**
- * application
- */
- private ServletContext application;
- /**
- * 控制器
- */
- @SuppressWarnings("unchecked")
- @Override
- public String execute(){
- System.out.println("通过ServletActionContext来访问request,session,application对象");
- // 初始化
- request = ServletActionContext.getRequest();
- session = request.getSession();
- application = session.getServletContext();
- // 赋值
- request.setAttribute("requestKey", "requestValue");
- session.setAttribute("sessionKey", "sessionValue");
- application.setAttribute("applicationKey", "applicationValue");
- return "success";
- }
- }
方式四:
- /**
- * 通过实现ServletRequestAware接口来访问request,session,application对象
- * @author 健
- */
- public class UserAction4 extends ActionSupport implements ServletRequestAware{
- /**
- * 序列化
- */
- private static final long serialVersionUID = 1L;
- /**
- * request
- */
- private HttpServletRequest request;
- /**
- * response
- */
- private HttpSession session;
- /**
- * application
- */
- private ServletContext application;
- /**
- * 控制器
- */
- @SuppressWarnings("unchecked")
- @Override
- public String execute(){
- System.out.println("通过实现ServletRequestAware接口来访问request,session,application对象");
- // 赋值
- request.setAttribute("requestKey", "requestValue");
- session.setAttribute("sessionKey", "sessionValue");
- application.setAttribute("applicationKey", "applicationValue");
- return "success";
- }
- /*
- * 实现ServletRequestAware接口中的方法
- */
- @Override
- public void setServletRequest(HttpServletRequest request) {
- this.request = request;
- this.session = request.getSession();
- this.application = session.getServletContext();
- }
- }
- /**
- * 通过实现ServletRequestAware接口来访问request,session,application对象
- * @author 健
- */
- public class UserAction4 extends ActionSupport implements ServletRequestAware{
- /**
- * 序列化
- */
- private static final long serialVersionUID = 1L;
- /**
- * request
- */
- private HttpServletRequest request;
- /**
- * response
- */
- private HttpSession session;
- /**
- * application
- */
- private ServletContext application;
- /**
- * 控制器
- */
- @SuppressWarnings("unchecked")
- @Override
- public String execute(){
- System.out.println("通过实现ServletRequestAware接口来访问request,session,application对象");
- // 赋值
- request.setAttribute("requestKey", "requestValue");
- session.setAttribute("sessionKey", "sessionValue");
- application.setAttribute("applicationKey", "applicationValue");
- return "success";
- }
- /*
- * 实现ServletRequestAware接口中的方法
- */
- @Override
- public void setServletRequest(HttpServletRequest request) {
- this.request = request;
- this.session = request.getSession();
- this.application = session.getServletContext();
- }
- }
上面的action配套的struts.xml及jsp页面
struts.xml
- <struts>
- <!-- 配置开发模式:修改不用重启服务器 -->
- <constant name="struts.devMode" value="true"/>
- <package name="" namespace="/login" extends="struts-default">
- <action name="login*" class="com.wj.struts2.action.UserAction{1}">
- <result name="success">/success.jsp</result>
- <result name="failure">/failure.jsp</result>
- </action>
- </package>
- </struts>
- <struts>
- <!-- 配置开发模式:修改不用重启服务器 -->
- <constant name="struts.devMode" value="true"/>
- <package name="" namespace="/login" extends="struts-default">
- <action name="login*" class="com.wj.struts2.action.UserAction{1}">
- <result name="success">/success.jsp</result>
- <result name="failure">/failure.jsp</result>
- </action>
- </package>
- </struts>
index.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@taglib uri="/struts-tags" prefix="s" %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>Struts2_AccessWebElements</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
- -->
- <mce:script language="javascript"><!--
- function sub(str){
- document.form1.action = str;
- document.form1.submit();
- }
- // --></mce:script>
- </head>
- <body>
- <form name="form1">
- <div>
- Struts2中访问web元素的四种方式<br>
- 方式一:<input type="button" value="submit1" onclick="sub('<%=basePath%>login/login1')"><br>
- 方式二:<input type="button" value="submit2" onclick="sub('<%=basePath%>login/login2')"><br>
- 方式三:<input type="button" value="submit3" onclick="sub('<%=basePath%>login/login3')"><br>
- 方式四:<input type="button" value="submit4" onclick="sub('<%=basePath%>login/login4')"><br>
- </div>
- </form>
- </body>
- </html>
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@taglib uri="/struts-tags" prefix="s" %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>Struts2_AccessWebElements</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
- -->
- <mce:script language="javascript"><!--
- function sub(str){
- document.form1.action = str;
- document.form1.submit();
- }
- // --></mce:script>
- </head>
- <body>
- <form name="form1">
- <div>
- Struts2中访问web元素的四种方式<br>
- 方式一:<input type="button" value="submit1" onclick="sub('<%=basePath%>login/login1')"><br>
- 方式二:<input type="button" value="submit2" onclick="sub('<%=basePath%>login/login2')"><br>
- 方式三:<input type="button" value="submit3" onclick="sub('<%=basePath%>login/login3')"><br>
- 方式四:<input type="button" value="submit4" onclick="sub('<%=basePath%>login/login4')"><br>
- </div>
- </form>
- </body>
- </html>
success.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@taglib uri="/struts-tags" prefix="s" %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>Struts2_AccessWebElements</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
- -->
- </head>
- <body>
- requestKey---<s:property value="#request.requestKey"/>|<%=request.getAttribute("requestKey")%><br>
- sessionKey---<s:property value="#session.sessionKey"/>|<%=session.getAttribute("sessionKey")%><br>
- applicationKey---<s:property value="#application.applicationKey"/>|<%=application.getAttribute("applicationKey")%><br>
- --------------------------------------------
- <s:debug></s:debug>
- </body>
- </html>
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@taglib uri="/struts-tags" prefix="s" %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>Struts2_AccessWebElements</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
- -->
- </head>
- <body>
- requestKey---<s:property value="#request.requestKey"/>|<%=request.getAttribute("requestKey")%><br>
- sessionKey---<s:property value="#session.sessionKey"/>|<%=session.getAttribute("sessionKey")%><br>
- applicationKey---<s:property value="#application.applicationKey"/>|<%=application.getAttribute("applicationKey")%><br>
- --------------------------------------------
- <s:debug></s:debug>
- </body>
- </html>
前台jsp页面获取后台值的方式
<s:property value="#request.requestKey"/>|<%=request.getAttribute("requestKey")%>
<s:property value="#session.sessionKey"/>|<%=session.getAttribute("sessionKey")%>
<s:property value="#application.applicationKey"/>|<%=application.getAttribute("applicationKey")%>