标签:des style class blog code java
完整代码 :Struts12AccessWebElement.rar
第一种也是最常用的一种方法实现这几个接口
RequestAware,SessionAware,ApplicationAware
struts以依赖注入方式把request,session和application赋上值,看一下完整的代码
package com.pengli.struts.parampack; import java.util.Map; import org.apache.struts2.interceptor.ApplicationAware; import org.apache.struts2.interceptor.RequestAware; import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.ActionSupport; public class MyWinAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{ static int actionCount=0; private Map<String, Object> my_Request; private Map<String, Object> session; private Map<String, Object> application; public String add() { AddKey(); return "add"; } @Override public void setRequest(Map<String, Object> arg0) { // TODO Auto-generated method stub my_Request=arg0; } @Override public void setApplication(Map<String, Object> arg0) { // TODO Auto-generated method stub application=arg0; } @Override public void setSession(Map<String, Object> arg0) { // TODO Auto-generated method stub session = arg0; } private void AddKey() { ++actionCount; my_Request.put("a1", "Two"+actionCount); ++actionCount; session.put("a2", "Two"+actionCount); ++actionCount; application.put("a3", "Two"+actionCount); } }
第二种方式是实现ServletRequestAware接口
得到request接口后,再根据 request的方法去得到session和application
package com.pengli.struts.parampack; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.util.ServletContextAware; import com.opensymphony.xwork2.ActionSupport; public class MyWinAction3 extends ActionSupport implements ServletRequestAware { private HttpServletRequest request; private HttpSession session; private ServletContext application; public String add() { setAttribute(); return "add"; } @Override public void setServletRequest(HttpServletRequest arg0) { // TODO Auto-generated method stub request=arg0; session=request.getSession(); application=session.getServletContext(); } private void setAttribute() { request.setAttribute("a1", "ThreeA1"); session.setAttribute("a2", "ThreeA2"); application.setAttribute("a3", "ThreeA3"); } }
第三种方式和第二种有点像,是用ServletActionContext得到request再得到sesion和application
package com.pengli.struts.parampack; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class MyWinAction4 extends ActionSupport{ private HttpServletRequest request; private HttpSession session; private ServletContext application; public String add(){ getInfo(); setAttribute(); return "add"; } private void getInfo() { request = ServletActionContext.getRequest(); session = request.getSession(); application = session.getServletContext(); } private void setAttribute() { request.setAttribute("a1", "FourA1"); session.setAttribute("a2", "FourA2"); application.setAttribute("a3", "FourA3"); } }
最后一种方法是通过ActionContext得到相应的参数
package com.pengli.struts.parampack; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class MyWiAction extends ActionSupport { private UserModel user; public String add1() { if(user == null) { this.addFieldError("user", "user object is null"); return ERROR; } if(user.getName()== null||user.getAge() == null) { this.addFieldError("name", "name is null"); this.addFieldError("age", "age is null"); return ERROR; } System.out.println(user.getAge()); System.out.println(user.getName()); return "add1"; } static int actionCount=0; private Map request1; private Map session1; private Map application1; public String add() { request1 = (Map)ActionContext.getContext().get("request"); session1=ActionContext.getContext().getSession(); application1 = ActionContext.getContext().getApplication(); AddKey(); return "add"; } @Override public String execute() throws Exception { // TODO Auto-generated method stub return super.execute(); } public String add2() { return "add2"; } public String add3() { return "add3"; } private void AddKey() { ++actionCount; request1.put("a1", "a1haha"+actionCount); ++actionCount; session1.put("a2", "a2haha"+actionCount); ++actionCount; application1.put("a3", "a3haha"+actionCount); } public String edit() { return "edit"; } public UserModel getUser() { return user; } public void setUser(UserModel user) { this.user = user; } }
再看一下web页面如何访问这些元素
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘Oneaddok.jsp‘ starting page</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"> --> </head> <body> This is my JSP page. <br> <s:property value="#request.a1" />|<%= request.getAttribute("a1") %> <br/> <s:property value="#session.a2" />|<%= session.getAttribute("a2") %> <br/> <s:property value="#application.a3" />|<%= application.getAttribute("a3") %> <br/> </body> </html>
再看一下struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- Add packages here --> <constant name="struts.devMode" value="true" /> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <package name="default1" namespace="/" extends="struts-default"> <action name="One*1" class="com.pengli.struts.parampack.MyWiAction" method="{1}"> <result name ="{1}"> /Oneaddok.jsp </result> <result name="error"> /errorpage.jsp </result> </action> <action name="One*2" class="com.pengli.struts.parampack.MyWinAction2" method="{1}"> <result name ="{1}"> /Oneaddok.jsp </result> <result name="error"> /errorpage.jsp </result> </action> <action name="One*3" class="com.pengli.struts.parampack.MyWinAction3" method="{1}"> <result name ="{1}"> /Oneaddok.jsp </result> <result name="error"> /errorpage.jsp </result> </action> <action name="One*4" class="com.pengli.struts.parampack.MyWinAction4" method="{1}"> <result name ="{1}"> /Oneaddok.jsp </result> <result name="error"> /errorpage.jsp </result> </action> </package> </struts>
完整代码 :Struts12AccessWebElement.rar
struts2 访问Web元素的4种方法,布布扣,bubuko.com
标签:des style class blog code java
原文地址:http://www.cnblogs.com/li-peng/p/3787603.html