Struts2自定义Result

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就差不多实现了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值