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常用的值 : chain,dispatcher,redirect,redirectAction,stream 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
- struts2 (二)单元测试/通过耦合和非耦合的方式获取和设置值/返回值类型/异常映射
- struts2耦合和非耦合设置值和获取的方式
- Struts2-耦合方式和非耦合方式获取WEB资源
- (08)通过和 ServletAPI 耦合的方式获取 WEB 资源
- 8.通过和ServletAPI耦合的方式获取WEB资源
- 9. Struts2_通过和 ServletAPI 耦合的方式获取 WEB 资源
- 内聚性和耦合性的类型
- struts 和servlet APi的耦合方式
- 5.Struts2_通过ServletAPI耦合的方式获取Web资源
- 《Modern C++ Design》源码Loki库读解随感二:类型间耦合检测和去耦合
- 松耦合和紧耦合
- Loki库:类型间耦合检测和去耦合
- 通过Java模拟各种类型的耦合
- 关于松耦合和紧耦合的理解
- 【转帖】Passthru和Application耦合方式
- 电路的耦合方式
- 干扰的耦合方式
- Rhyme/ Struts2 解耦以及耦合方式获取ServletApi
- CC2540 CC2541 CC2530脱机烧录器量产批量生产设备 2路并行烧录
- Struts2 (一)
- C语言的atan()和atan2()函数
- Hadoop Yarn 集群搭建
- Win10+VS2013编译OSG3.4
- struts2 (二)单元测试/通过耦合和非耦合的方式获取和设置值/返回值类型/异常映射
- C#面向对象概念总结
- 携程DynamicAPK插件化框架源码分析
- word2010公式排版、mathtype公式编辑和自动编号
- 记一次ubutnu14.10下安装编译Apache
- hadoop Yarn 搭建集群时错误 主节点 NameNode 没有启动成功
- 农夫过河(数据结构)之C语言
- Node.js_Buffer缓冲区
- Sublime Text2.0.2注册码