struts2 (二)单元测试/通过耦合和非耦合的方式获取和设置值/返回值类型/异常映射

来源:互联网 发布:淘宝怎么清仓 编辑:程序博客网 时间:2024/06/07 08:13

单元测试的类必须继承StrutsTestCase

package com.etop.struts2.test;import org.apache.struts2.StrutsTestCase;import org.junit.Test;import com.opensymphony.xwork2.ActionProxy;/** * 单元测试的类必须继承StrutsTestCase * 测试的action 必须在struts.xml文件预先配置好 * @author teacher * */public class TestStruts2 extends StrutsTestCase{   @Test   public void testStruts2(){       //通过路径获取action的代理       ActionProxy proxy=getActionProxy("/test.action");       //获取实际的action       TestAction testAction=(TestAction)proxy.getAction();       //测试action对应的类对应的方法       testAction.execute();   }}

通过耦合和非耦合的方式获取和设置值

package com.etop.struts2.param;import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import org.apache.struts2.interceptor.ParameterAware;import org.apache.struts2.interceptor.RequestAware;import org.apache.struts2.interceptor.ServletRequestAware;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;/** * 装配模式(非耦合模式):在action中实现某一些接口  然后实现接口些方法, * struts2判断action是否实现了某一些接口接口 然后自动调用接口的方法 * 并且传入需要的参数 * ParameterAware自动填充参数 类型为 Map<String, String[]> parameters * ServletRequestAware, ServletActionContext.getRequest()(耦合) */public class ParamAction extends ActionSupport implements ParameterAware,RequestAware,ServletRequestAware{   public String execute(){          //耦合方式获取原生态request对象       HttpServletRequest request=ServletActionContext.getRequest();       String connection=request.getHeader("Connection");       String connection1=crequest.getHeader("Connection");       //每一个对应的action 都有一个ActionContext        //通过ActionContext对象获取参数集合(非耦合模式)       Map map=ActionContext.getContext().getParameters();       //获取请求作用的值 可以将值设置到非耦合的map中 并且可以使用el表达式获取       Map obj=(Map)ActionContext.getContext().get("request");       obj.put("test1", "test1");       return "success";   }   private Map<String, String[]> parameters;   private Map<String, Object> requestParam;   HttpServletRequest crequest;   public void setParameters(Map<String, String[]> parameters) {       this.parameters=parameters;   }   public void setRequest(Map<String, Object> requestParam) {       // TODO Auto-generated method stub       this.requestParam=requestParam;   }   public void setServletRequest(HttpServletRequest request) {       // TODO Auto-generated method stub       this.crequest=request;   }}

返回值类型

result用于控制action的跳转  name为action返回的名称               type常用的值 : chaindispatcherredirectredirectActionstream               type 是转发的类型  dispatcher:请求转发,                                chain 请求转发 跳转action                                redirect请求重定向                                redirectAction 请求重定向到action                                stream 文件下载 

异常映射

<package name="ognlPakage" namespace="/" extends="struts-default">      <!-- 全部的result 作用域当前包下的所有action -->      <global-results>              <result name="error">/ognl/error.jsp</result>      </global-results>      <!-- 全局异常映射 到包下的所有的action出现 exception配置的异常时 会自动跳转到全局定义的result           如果需要所有的action都需要进行异常映射  需要建立一个公共的package 其他的package集成定义异常映射的package      -->      <global-exception-mappings>              <exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>      </global-exception-mappings>      <action name="ognl" class="com.etop.struts2.ognl.OgnlAction"  >           <result name="success">/ognl/ognl.jsp</result>      </action>   </package>
0 0
原创粉丝点击