一、自定义结果处理类,structs.xml中package节点下加result-types节点,在result-types节点下配置result-type的属性。然后在配置的action中的result属性type为resul-type的name属性值
如,自定义服务端重定向Dispatcher类
structs.xml配置:
<structs> <package name="resulttype" namespace="/resulttype" extends="structs-default"> <result-types> <result-type name="MyDispathcer" class="cn.hjp.resulttypes.MyDispatcher"></result-type> </result-types> <action name="HelloAction" class="cn.hjp.action.HelloAction"> <result name="Success" type="MyDispathcer">/Index.jsp</result> </action> </package> </structs>
java中的实现代码为:
//自定义服务端页面跳转类 public class MyDispatcher extends StrutsResultSupport { @Override protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception { // TODO Auto-generated method stub HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); request.getRequestDispatcher(finalLocation).forward(request, response); } } //cn.hjp.action下的类 public class HelloAction implements Action { @Override public String execute() throws Exception { System.out.println("自定义服务端页面重定向测试"); return "Success"; } }
二、配置result节点type属性为dispatcher,实现页面在服务端重定向
<structs> <package name="hello" namespace="/aaa" extends="structs-default"> <action name="HelloAction" class="cn.hjp.action.HelloAction"> <result name="success" type="dispatcher">/Index.jsp</result> </action> </package> </structs>
java代码:
//cn.hjp.action下的类 public class HelloAction implements Action { @Override public String execute() throws Exception { System.out.println("服务端页面重定向测试"); return SUCCESS; } }
三、配置result节点type属性为redirect,实现页面在客户端重定向
<structs> <package name="hello" namespace="/aaa" extends="structs-default"> <action name="HelloAction" class="cn.hjp.action.HelloAction"> <result name="success" type="redirect">/Index.jsp</result> </action> </package> </structs>
java代码:
//cn.hjp.action下的类 public class HelloAction implements Action { @Override public String execute() throws Exception { System.out.println("客户端页面重定向测试"); return SUCCESS; } }
四、配置result节点type属性为plainText,显示纯文本,及参数注入
<structs> <package name="hello" namespace="/aaa" extends="structs-default"> <action name="HelloAction" class="cn.hjp.action.HelloAction"> <result name="success" type="plainText"> <!--参数注入,其中的参数属性为plainText对应的类及其父类中属性--> <param name="location">/Index.jsp</param> <!--编码不一致会乱码--> <param name="charSet">UTF-8</param> </result> </action> </package> </structs>
java代码:
//cn.hjp.action下的类 public class HelloAction implements Action { @Override public String execute() throws Exception { System.out.println("纯文本测试"); return SUCCESS; } }
五、配置result节点type属性为stream,实现文件下载
<structs> <package name="hello" namespace="/aaa" extends="structs-default"> <action name="HelloAction" class="cn.hjp.action.HelloAction"> <result name="success" type="stream"> <param name="contentType">application/zip</param> <!--如下zip,此处大小写不区分,但是对应action中的getZip()方法,返回InputStream类型--> <param name="inputName">zip</param> <!--filename自定义下载文件名--> <param name="contentDisposition">attachment;filename="Mapping123.zip"</param> <param name="bufferSize">1024</param> </result> </action> </package> </structs>
java代码:
public class HelloAction extends ActionSupport { public InputStream getZip() { // 读取文件流 ServletContext context = ServletActionContext.getServletContext(); //在WebContent下新建文件夹file,里面有Mapping.zip文件 InputStream inputStream = context.getResourceAsStream("/file/Mapping.zip"); return inputStream; } }
调用方式:http://localhost:8080/structs2-demo1/aaa/HelloAction.action。直接下载文件
六、配置result节点type属性为redirectAction,实现重定向到指定Action。其实该方式是对type="redirect"变相重写,通过查源码可知,ServletActionRedirectResult继承了ServletRedirectResult类,通过ServletActionRedirectResult中的execute方法执行到ServletRedirectResult中的doexecute方法,实现跳转
<structs> <package name="hello" namespace="/aaa" extends="structs-default"> <action name="HelloAction1" class="cn.hjp.action.HelloAction1"> <result name="success" type="redirectAction"> <param name="actionName">HelloAction</param> <!--如果不配置namespace,则默认当前namespace--> <param name="namespace">/aaa</param> </result> </action> </package> </structs>
java代码:
public class HelloAction1 extends ActionSupport { @Override public String execute() throws Exception { // TODO Auto-generated method stub System.out.println("重定向到指定Action测试"); return SUCCESS; } }
访问路径:http://localhost:8080/structs2-demo1/aa/HelloAction1.action
时间: 2024-11-05 21:55:42