Struts2自定义Result
来源:互联网 发布:php 实现模板切换代码 编辑:程序博客网 时间:2024/05/16 19:33
Struts2的Result有很多的类型,已经够用了:
但是,有的时候也可能会用到,当然,即使不用,了解一下,也是可以的
1.准备好Struts2环境:
这里就不赘述了
2.实现Result接口
如上面的截图所示,要自定义Result,只需要实现Result接口就可以了:
package org.ygy.demo.result;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.Result;/** * * @author yuguiyang * @description 自定义Result * @time 2013-9-4 * @version V1.0 */public class SimpleResult implements Result {private static final long serialVersionUID = 4606456946074955278L;public void execute(ActionInvocation invocation) throws Exception {System.out.println("要处理的Result字符串是="+invocation.getResultCode());}}这里,我们只输出了一句话,其余的什么都没干
3.配置
定义好了Result,接下来配置一下就可以了
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <constant name="struts.devMode" value="true" /> <package name="p_hello" namespace="/" extends="struts-default"><result-types><result-type name="simple" class="org.ygy.demo.result.SimpleResult"></result-type><result-type name="json" class="org.ygy.demo.result.JsonResult"></result-type></result-types><action name="say" class="org.ygy.demo.result.HelloAction"><result name="success">/hello.jsp</result></action><action name="go" class="org.ygy.demo.result.HelloAction" method="go"><result name="success" type="simple">/hello.jsp</result></action><action name="ha" class="org.ygy.demo.result.HelloAction" method="ha"><result name="success" type="json" >/hello.jsp</result></action><action name="haha" class="org.ygy.demo.result.HelloAction" method="haha"><result name="success" type="json">/hello.jsp</result></action></package></struts>
我们的Action是这样的:
package org.ygy.demo.result;import java.util.ArrayList;import java.util.List;import com.opensymphony.xwork2.ActionSupport;public class HelloAction extends ActionSupport {private static final long serialVersionUID = 6422231025855671997L;public String say() {System.out.println("--from say().");return SUCCESS;}public String go() {System.out.println("--from go().");return SUCCESS;}public JsonResult ha() {System.out.println("--from ha().");List<String> msgs = new ArrayList<String>();msgs.add("one");msgs.add("two");msgs.add("three");JsonResult result = new JsonResult();result.setResult(msgs);return result;}public String haha() {System.out.println("--from haha().");List<String> msgs = new ArrayList<String>();msgs.add("one");msgs.add("two");msgs.add("three");JsonResult result = new JsonResult();result.setResult(msgs);return SUCCESS;}}
最后,访问网址:http://localhost:8080/result/go.action
出现如下结果:
到这里,最简单的自定义Result就差不多实现了。
- Struts2 自定义Result类型
- Struts2 自定义Result类型
- Struts2自定义Result
- Struts2 自定义Result
- Struts2 自定义Result
- Struts2自定义Result处理JSON
- Struts2 自定义Result返回类型
- struts2自定义result中的type
- struts2 自定义result 处理JSON
- Struts2中自定义的Result
- 《研磨struts2》第五章 Result 之 5.7 自定义Result
- Struts2自定义Result(二)- StrutsResultSupport
- Struts2---->result
- Struts2----Result
- struts2-result
- Struts2 Result
- Struts2-result
- 扩展Struts2--自定义String和XML格式的Result
- hdu 2191 多重背包
- view matrix 计算
- java基础--Jar包添加到build path方式说明--01
- Oracle 中关于database的read only状态和read write状态下普通用户对数据的操作
- 2013/9/4学习小记
- Struts2自定义Result
- mongoDB——GridFS存储机制
- Java内部类,静态内部类
- vc/mfc 发布程序时要注意的一些问题
- Cortex-M3与Cortex-M4的比较
- ora-00020
- Android有关Please execute 'adb uninstall my.test' in a shell报错处理
- C#中字符串的处理方法
- 项目报错一