//index.jsp
1 <?xml version="1.0" encoding="GB18030" ?> 2 <%@ page language="java" contentType="text/html; charset=GB18030" 3 pageEncoding="GB18030"%> 4 5 <% String context = request.getContextPath(); %> 6 7 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 8 <html xmlns="http://www.w3.org/1999/xhtml"> 9 <head> 10 <meta http-equiv="Content-Type" content="text/html; charset=GB18030" /> 11 <title>Insert title here</title> 12 </head> 13 <body> 14 取得Map类型request,session,application,真实类型 HttpServletRequest, HttpSession, ServletContext的引用: 15 <ol> 16 <li>前三者:依赖于容器</li> 17 <li>前三者:IOC</li> (只用这种) 18 <li>后三者:依赖于容器</li> 19 <li>后三者:IOC</li> 20 </ol> 21 <br /> 22 23 <!-- 定义一个表单 提交用户名和密码 --> 24 25 <form name="f" action="" method="post"> 26 用户名:<input type="text" name="name"/> 27 密码:<input type="text" name="password"/> 28 <br /> 29 30 <!-- 通过javascript:document.f.action动态提交到不同的action中进行不同的处理 --> 31 <input type="button" value="submit1" onclick="javascript:document.f.action='login/login1';document.f.submit();" /> 32 <input type="button" value="submit2" onclick="javascript:document.f.action='login/login2';document.f.submit();" /> 33 <input type="button" value="submit3" onclick="javascript:document.f.action='login/login3';document.f.submit();" /> 34 <input type="button" value="submit4" onclick="javascript:document.f.action='login/login4';document.f.submit();" /> 35 </form> 36 37 </body> 38 </html>
//struts.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 <constant name="struts.devMode" value="true" /> 8 <package name="login" extends="struts-default" namespace="/login"> 9 <!-- 使用通配符分别对应传入的login1,2,3,4 并调用不同的class --> 10 <action name="login*" class="com.bjsxt.struts2.user.action.LoginAction{1}"> 11 <result>/user_login_success.jsp</result> 12 </action> 13 </package> 14 </struts>
//方法1
1 package com.bjsxt.struts2.user.action; 2 3 import java.util.Map; 4 5 import com.opensymphony.xwork2.ActionContext; 6 import com.opensymphony.xwork2.ActionSupport; 7 8 9 //第一种可以取得request的方式 10 //称之为 11 public class LoginAction1 extends ActionSupport { 12 13 private Map request; 14 private Map session; 15 private Map application; 16 17 public LoginAction1() { 18 //ActionContext.getContext取得action中的上下文 对应JSP的为Application 19 //取得request的方法 20 request = (Map)ActionContext.getContext().get("request"); 21 //取得Session的方法 22 session = ActionContext.getContext().getSession(); 23 //取得Application的方法 24 application = ActionContext.getContext().getApplication(); 25 } 26 27 public String execute() { 28 //在得到的Map中放入值 查看前台是否能够访问 29 //这种处理方法即将这里设置的值全部复制到http内容中去 30 request.put("r1", "r1"); 31 session.put("s1", "s1"); 32 application.put("a1", "a1"); 33 return SUCCESS; 34 } 35 36 37 }
//方法2
1 package com.bjsxt.struts2.user.action; 2 3 import java.util.Map; 4 5 import org.apache.struts2.interceptor.ApplicationAware; 6 import org.apache.struts2.interceptor.RequestAware; 7 import org.apache.struts2.interceptor.SessionAware; 8 9 import com.opensymphony.xwork2.ActionContext; 10 import com.opensymphony.xwork2.ActionSupport; 11 12 13 //实现了3个接口,必须复写3个方法 14 //Aware:得知,获得. 15 //这是最常用的方式 16 public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware, ApplicationAware { 17 private Map<String, Object> request; 18 private Map<String, Object> session; 19 private Map<String, Object> application; 20 //DI dependency injection 依赖注入 21 //IoC inverse of control 控制翻转 22 public String execute() { 23 request.put("r1", "r1"); 24 session.put("s1", "s1"); 25 application.put("a1", "a1"); 26 return SUCCESS; 27 } 28 29 //实现了三个Aware接口后,Struts2将会将request,session和Application传来 30 @Override 31 public void setRequest(Map<String, Object> request) { 32 this.request = request; 33 } 34 35 @Override 36 public void setSession(Map<String, Object> session) { 37 this.session = session; 38 } 39 40 @Override 41 public void setApplication(Map<String, Object> application) { 42 this.application = application; 43 } 44 45 46 }
//方法3
1 package com.bjsxt.struts2.user.action; 2 3 import javax.servlet.ServletContext; 4 import javax.servlet.http.HttpServletRequest; 5 import javax.servlet.http.HttpSession; 6 7 import org.apache.struts2.ServletActionContext; 8 9 import com.opensymphony.xwork2.ActionSupport; 10 11 //基本不用 12 public class LoginAction3 extends ActionSupport { 13 14 private HttpServletRequest request; 15 private HttpSession session; 16 private ServletContext application; 17 18 public LoginAction3() { 19 request = ServletActionContext.getRequest(); 20 session = request.getSession(); 21 application = session.getServletContext(); 22 } 23 24 public String execute() { 25 request.setAttribute("r1", "r1"); 26 session.setAttribute("s1", "s1"); 27 application.setAttribute("a1", "a1"); 28 return SUCCESS; 29 } 30 31 }
//方法4
1 package com.bjsxt.struts2.user.action; 2 3 import javax.servlet.ServletContext; 4 import javax.servlet.http.HttpServletRequest; 5 import javax.servlet.http.HttpSession; 6 7 import org.apache.struts2.interceptor.ServletRequestAware; 8 9 import com.opensymphony.xwork2.ActionSupport; 10 11 //基本不用 12 public class LoginAction4 extends ActionSupport implements ServletRequestAware { 13 14 private HttpServletRequest request; 15 private HttpSession session; 16 private ServletContext application; 17 18 19 20 public String execute() { 21 request.setAttribute("r1", "r1"); 22 session.setAttribute("s1", "s1"); 23 application.setAttribute("a1", "a1"); 24 return SUCCESS; 25 } 26 27 28 29 @Override 30 public void setServletRequest(HttpServletRequest request) { 31 this.request = request; 32 this.session = request.getSession(); 33 this.application = session.getServletContext(); 34 } 35 36 }
//显示取出的值
1 <?xml version="1.0" encoding="GB18030" ?> 2 <%@ page language="java" contentType="text/html; charset=GB18030" 3 pageEncoding="GB18030"%> 4 <%@taglib uri="/struts-tags" prefix="s" %> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 6 <html xmlns="http://www.w3.org/1999/xhtml"> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=GB18030" /> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 User Login Success! 13 <br /> 14 15 <!-- 用标签访问request中的值 --> 16 <s:property value="#request.r1"/> | <%=request.getAttribute("r1") %> <br /> 17 <s:property value="#session.s1"/> | <%=session.getAttribute("s1") %> <br /> 18 <s:property value="#application.a1"/> | <%=application.getAttribute("a1") %> <br /> 19 <s:property value="#attr.a1"/><br /> 20 <s:property value="#attr.s1"/><br /> 21 <s:property value="#attr.r1"/><br /> 22 <s:debug></s:debug> 23 <br /> 24 </body> 25 </html>