第一种方式:
在index.jsp里编写:
<a href="test1Action!test">解耦合的方式1</a><br>
在struts.xml里编写对应关系:
<action name="test1Action" class="com.xsl.action.Test1Action">
<result name="success">/success.jsp</result>
</action>
编写对应的Action类:(继承ActionSupport类;request、session、application用Map集合表示)
1 package com.xsl.action; 2 3 import java.util.Map; 4 5 import com.opensymphony.xwork2.ActionContext; 6 import com.opensymphony.xwork2.ActionSupport; 7 8 public class Test1Action extends ActionSupport { 9 private Map<String,Object> request; 10 private Map<String,Object> session; 11 private Map<String,Object> application; 12 13 public String test(){ 14 System.out.println("Test1Action测试..."); 15 16 ActionContext context = ActionContext.getContext(); 17 request = (Map<String, Object>) context.get("request"); 18 session = context.getSession(); 19 application = context.getApplication(); 20 21 request.put("a1", "b1"); 22 session.put("a2", "b2"); 23 application.put("a3", "b3"); 24 25 return SUCCESS; 26 } 27 28 }
在success.jsp中显示request、session、application绑定的数据:
This is success page. <br>
${a1 } --${a2 }--${a3 } <br><!--第一种方式显示的数据-->
${c1 } --${c2 }--${c3 } <br><!--第二种方式显示的数据-->
${e1 } --${e2 }--${e3 } <br><!--第三种方式显示的数据-->
${g1 } --${g2 }--${g3 } <br><!--第四种方式显示的数据-->
第二种方式:
在index.jsp里编写:
<a href="test2Action!test">解耦合的方式2</a><br>
在struts.xml里编写对应关系:
<action name="test2Action" class="com.xsl.action.Test2Action">
<result name="success">/success.jsp</result>
</action>
编写对应的Action类:(继承ActionSupport类,实现RequestAware、SessionAware、ApplicationAware接口;request、session、application用Map集合表示)
1 package com.xsl.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.ActionSupport; 10 11 public class Test2Action extends ActionSupport implements RequestAware,SessionAware,ApplicationAware { 12 13 private Map<String,Object> request; 14 private Map<String,Object> session; 15 private Map<String,Object> application; 16 17 public String test(){ 18 System.out.println("Test2Action测试..."); 19 request.put("c1", "d1"); 20 session.put("c2", "d2"); 21 application.put("c3", "d3"); 22 return SUCCESS; 23 } 24 25 public void setRequest(Map<String, Object> request) { 26 this.request = request; 27 } 28 29 public void setSession(Map<String, Object> session) { 30 this.session = session; 31 } 32 33 public void setApplication(Map<String, Object> application) { 34 this.application = application; 35 } 36 37 38 }
在success.jsp中显示request、session、application绑定的数据:
This is success page. <br>
${a1 } --${a2 }--${a3 } <br><!--第一种方式显示的数据-->
${c1 } --${c2 }--${c3 } <br><!--第二种方式显示的数据-->
${e1 } --${e2 }--${e3 } <br><!--第三种方式显示的数据-->
${g1 } --${g2 }--${g3 } <br><!--第四种方式显示的数据-->
第三种方式:
在index.jsp里编写:
<a href="test3Action!test">耦合的方式1</a><br>
在struts.xml里编写对应关系:
<action name="test3Action" class="com.xsl.action.Test3Action">
<result name="success">/success.jsp</result>
</action>
编写对应的Action类:(继承ActionSupport类;request、session、application分别用HttpServletRequest、HttpSession、ServletContext表示)
1 package com.xsl.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 public class Test3Action extends ActionSupport { 12 13 private HttpServletRequest request; 14 private HttpSession session; 15 private ServletContext application; 16 17 public String test(){ 18 19 request = ServletActionContext.getRequest(); 20 session = request.getSession(); 21 application = ServletActionContext.getServletContext(); 22 23 request.setAttribute("e1", "f1"); 24 session.setAttribute("e1", "f1"); 25 application.setAttribute("e1", "f1"); 26 27 return SUCCESS; 28 } 29 }
在success.jsp中显示request、session、application绑定的数据:
This is success page. <br>
${a1 } --${a2 }--${a3 } <br><!--第一种方式显示的数据-->
${c1 } --${c2 }--${c3 } <br><!--第二种方式显示的数据-->
${e1 } --${e2 }--${e3 } <br><!--第三种方式显示的数据-->
${g1 } --${g2 }--${g3 } <br><!--第四种方式显示的数据-->
第四种方式:
在index.jsp里编写:
<a href="test4Action!test">耦合的方式2</a><br>
在struts.xml里编写对应关系:
<action name="test4Action" class="com.xsl.action.Test4Action">
<result name="success">/success.jsp</result>
</action>
编写对应的Action类:(继承ActionSupport类,实现RequestAware、SessionAware、ApplicationAware接口;request、session、application分别用HttpServletRequest、HttpSession、ServletContext表示)
1 package com.xsl.action; 2 3 import javax.servlet.ServletContext; 4 import javax.servlet.http.HttpServletRequest; 5 import javax.servlet.http.HttpServletResponse; 6 import javax.servlet.http.HttpSession; 7 8 import org.apache.struts2.interceptor.ServletRequestAware; 9 import org.apache.struts2.interceptor.ServletResponseAware; 10 import org.apache.struts2.util.ServletContextAware; 11 12 import com.opensymphony.xwork2.ActionSupport; 13 14 public class Test4Action extends ActionSupport implements ServletRequestAware,ServletContextAware,ServletResponseAware { 15 16 private HttpServletRequest request; 17 private HttpSession session; 18 private ServletContext application; 19 private HttpServletResponse response; 20 21 public String test(){ 22 request.setAttribute("g1", "h1"); 23 session = request.getSession(); 24 session.setAttribute("g2", "h2"); 25 application.setAttribute("g3", "h3"); 26 System.out.println(response); 27 return SUCCESS; 28 } 29 30 public void setServletRequest(HttpServletRequest request) { 31 this.request = request; 32 } 33 34 public void setServletContext(ServletContext context) { 35 this.application = context; 36 } 37 38 public void setServletResponse(HttpServletResponse response) { 39 this.response = response; 40 } 41 }
在success.jsp中显示request、session、application绑定的数据:
This is success page. <br>
${a1 } --${a2 }--${a3 } <br><!--第一种方式显示的数据-->
${c1 } --${c2 }--${c3 } <br><!--第二种方式显示的数据-->
${e1 } --${e2 }--${e3 } <br><!--第三种方式显示的数据-->
${g1 } --${g2 }--${g3 } <br><!--第四种方式显示的数据-->
第一、二、四种方式在调用当前Action时,能显示request、session、application绑定的键值对,在其它时候不能显示request,但也能显示session、application绑定的键值对。
第三种方式不管在什么时候都只能显示request绑定的键值对。