Struts2之Result

Result类型
  1. dispatcher (默认的)
  2. redirect
  3. chain
  4. redirectAction
  5. freemarker
  6. httpheader
  7. stream
  8. velocity
  9. xslt
  10. plaintext
  11. tiles
先上index.jsp代码:
<body>
Result类型
<ol>
	<li><a href="r/r1">dispatcher</a></li>
	<li><a href="r/r2">redirect</a></li>
	<li><a href="r/r3">chain</a></li>
	<li><a href="r/r4">redirectAction</a></li>
	<li>freemarker</li>
	<li>httpheader</li>
	<li>stream</li>
	<li>velocity</li>
	<li>xslt</li>
	<li>plaintext</li>
	<li>tiles</li>
</ol>
	
</body>

再上struts.xml:

<struts>
    <constant name="struts.devMode" value="true" />
    <package name="resultTypes" namespace="/r" extends="struts-default">
	    <action name="r1">
	    	<result type="dispatcher">/r1.jsp</result><!-- 服务器端跳转dispatcher只能到视图,不能到Action,就是servlet -->
	    </action>
	    
	    <action name="r2">
	    	<result type="redirect">/r2.jsp</result><!-- 客户端跳转。同上 -->
	    </action>
	    
	    <action name="r3">
	    	<result type="chain">r1</result>
	    </action>
	    
	    <action name="r4">
	    	<result type="redirectAction">r2</result>
	    </action>
	    
    </package>
</struts>


GlobalResult-

index.htnl代码:

<body>
Result类型
<ol>
	<li><a href="user/user?type=1">返回success</a></li>
	<li><a href="user/user?type=2">返回error</a></li>
	<li><a href="user/user?type=3">返回global result</a></li>
	<li><a href="admin/admin">admin,继承user包</a></li>
</ol>
	
</body>

Struts2.xml代码:

<struts>
    <constant name="struts.devMode" value="true" />
    <package name="user" namespace="/user" extends="struts-default">
    	
    	
    	<global-results>
    		<result name="mainpage">/main.jsp</result>
    	</global-results>
    	
    	<action name="index">
    		<result>/index.jsp</result>
    	</action>
    	
	    <action name="user" class="com.china.UserAction">
	    	<result>/user_success.jsp</result>
	    	<result name="error">/user_error.jsp</result>
	    </action>	    
    </package>
    
    <package name="admin" namespace="/admin" extends="user">
    	<action name="admin" class="com.china.AdminAction">
    		<result>/admin.jsp</result>
    	</action>
    </package>
</struts>

几个用到的java程序:

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	private int type;

	

	public int getType() {
		return type;
	}



	public void setType(int type) {
		this.type = type;
	}



	@Override
	public String execute() throws Exception {
		if(type == 1) return "success";
		else if (type == 2) return "error";
		else return "mainpage";
	}

}

还有一个:

import com.opensymphony.xwork2.ActionSupport;


public class AdminAction extends ActionSupport {


	@Override
	public String execute() throws Exception {
		return "mainpage";
	}


}


DynamicResult动态结果集:(了解的级别)

first index.jsp:

<body>
动态结果一定不要忘了为动态结果的保存值设置set get方法
<ol>
<li><a href="user/user?type=1">返回success</a></li>
<li><a href="user/user?type=2">返回error</a></li>
</ol>
</body>



then UserAction.java:

mport com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	private int type;
	
	private String r;

	public String getR() {
		return r;
	}

	public void setR(String r) {
		this.r = r;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	@Override
	public String execute() throws Exception {
		if(type == 1) r="/user_success.jsp";
		else if (type == 2) r="/user_error.jsp";
		return "success";
	}

}


last struts2.xml:

<struts>
    <constant name="struts.devMode" value="true" />
    <package name="user" namespace="/user" extends="struts-default">
    	
	    <action name="user" class="com.china.UserAction">
	    	<result>${r}</result>
	    </action>	    
    </package>
    	
</struts>




ResultWithParams:

frist index.jsp:

<body>
向结果传参数
<ol>
	<li><a href="user/user?type=1">传参数</a></li>
</ol>
	
</body>

then UserAction.java:

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	private int type;
	
	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	@Override
	public String execute() throws Exception {
		return "success";
	}

}

and then struts.xml:

<struts>
    <constant name="struts.devMode" value="true" />
    <package name="user" namespace="/user" extends="struts-default">
    	
	    <action name="user" class="com.china.UserAction">
	    	<result type="redirect">/user_success.jsp?t=${type}</result>
	    </action>	    
    </package>
    	
</struts>

last user_success.jsp:

<body>
	User Success!</br>
	from valuestack: <s:property value="t"/><br/><!--this is wrong,can't receiver-->
	from actioncontext: <s:property value="#parameters.t"/>
	<s:debug></s:debug>
</body>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值