当接受表单参数,向页面保持数据时。要用到Struts访问Servlet 的API 。下面只做参考,有错误或不同意见可以发送邮箱2440867831@qq.com 。建议大家看struts文档,源代码
1.struts2通过ActionContext访问
com.opensymphony.xwork2.ActionContext;类有很多方法这里只列举几个常用的方法;
列举个小例子
Struts2的核心配置web.xml 就不做说明了 。前面已经操作过;
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> <!-- 2.变量设置 在Struts.xml文件里面配置 --> <!--i18n :国际化 解决post提交乱码。输出中文乱码 省去过滤器字节编码--> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <constant name="struts.devMode " value="true"></constant> <constant name="struts.action.extension" value="action,,"></constant> <package name="hello" namespace="/" extends="struts-default"> <!-- 3注意 这里没有method属性 默认对应的是Action类的execute方法 --> <action name="Demo1" class="com.sturts2.day02.action.RequestDemo1"> <result name="success">/demo2.jsp</result> </action> </package> </struts>
RequestDemo1.java Action类
1 public class RequestDemo1 extends ActionSupport{ 2 @Override 3 public String execute() throws Exception { 4 //ac 相当于Servlet 的request 5 ActionContext ac=ActionContext.getContext(); 6 Map<String ,Object> paramMap=ac.getParameters(); 7 for(String key: paramMap.keySet()){ 8 String [] value=(String[]) paramMap.get(key); 9 System.out.println(key+" "+value[0]); 10 } 11 12 ac.put("request1", "张三"); 13 ac.getSession().put("session1","李四"); 14 ac.getApplication().put("application1", "王五"); 15 return SUCCESS; 16 } 17 }
demo1.jsp
<body> <h1>访问Servlet API 方式之一</h1> <form action="${pageContext.request.contextPath}/Demo1.action" method="post"> 姓名:<input type="text" name="name"><br/> 年龄:<input type="text" name="age"><br/> <input type="submit" value="提交"> </form> </body>
demo2.jsp
<body> <h1>获取数据</h1> requet: ${request1 } <br/><br/> session: ${session1 } <br/><br/> application: ${application1 } <br/><br/> </body>
在服务器上运行 ,打开demo1 :
输入数据提交表单: 控制台显示
浏览器跳转页面:
2.通过特定接口访问
Struts2 的ActionContext访问Servlet API 无法直接获得Servlet API 实例。以下接口可以直接访问Servlet API ;
- ServletRequestAware :实现该接口的Action可以直接访问web应用的HttpServletRequest
- ServletResponseAware :实现该接口的Action可以直接访问web应用的HttpServletResponse
- SessionAware :实现该接口的Action可以直接访问web应用的TttpSession
- ServletContextAware :实现该接口的Action可以直接访问web应用的ServletContex实例
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <package name="api" namespace="/" extends="struts-default"> 7 8 <action name="Demo5Action" class="com.sturts2.day02.b_api.Demo5Action"> 9 <result name="success" >/api.jsp</result> 10 </action> 11 12 </package> 13 </struts>
3.通过ServletActionContext访问
借助ServletActionContext 类,避免action实现ServletRequestAware ,ServletResponseAware、SessionAware 和ServletContextAware 等 XxxAware接口。但Action直接与ServletAPI耦合,不利于程序的解耦。建议使用ActionContext
代码如下:
- static HttpServletResquest getRequest(); 获取Web应用HttpServletResquest对象
- static HttpServletResponse getResponse(); 获取Web应用HttpServletResponse对象
- static ServletContext getServletContext(); 获取Web应用ServletContext对象
- static PageContext getPageContext(); 获取Web应用pageContext对象
struts.xml配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <package name="api" namespace="/" extends="struts-default"> 7 8 <action name="Demo5Action" class="com.sturts2.day02.b_api.Demo5Action"> 9 <result name="success" >/api.jsp</result> 10 </action> 11 12 13 14 </package> 15 </struts>
Action类
package com.sturts2.day02.b_api; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.interceptor.ServletRequestAware; import com.opensymphony.xwork2.ActionSupport; public class Demo5Action extends ActionSupport implements ServletRequestAware { private HttpServletRequest request; @Override public String execute() throws Exception { System.out.println("原生request:"+request); return SUCCESS; } @Override public void setServletRequest(HttpServletRequest request) { this.request=request; } }
api.jsp
1 <body> 2 request:${request1 } <br/> 3 session:${session1 } <br/> 4 application:${application1 } <br/> 5 6 </body>
页面显示:
控制台输出