Struts2 语法--result type

  • result type:

dispatcher,redirect:只能跳转到jsp,html之类的页面,dispatcher属于服务器跳转, redirect属于客户端跳转

chain: 等同于forwardaction, 

redirectAction: 客户端跳转到另一个action

还有freemarker,httpheader,stream(下载),volocity(类似freemarker), xslt, plaintext,tiles

<?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="resultTypes" namespace="/r" extends="struts-default">
	    <action name="r1">
	    	<result type="dispatcher">/r1.jsp</result>
	    </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>

对于以下dispatcher:

URL: r/r1

显示:r1

 <action name="r1">
   	<result type="dispatcher">/r1.jsp</result>
</action>

  

对于以下redirect:

URL: r/r2.jsp

显示:r2

 <action name="r1">
  <result type="redirect">/r2.jsp</result>
</action>

对于以下chain:

URL: r/r3

显示:r1

 <action name="r1">
  
<result type="chain">r1</result>
</action>

注意: 如果forward到别的package的action的话, 需要用这个命令:\

<result type="chain">

  <param name="actionname">dashboard</param>

  <param name="namespace">/secure</param>

</result>

  

对于以下redirectAction:

URL: r/r2.jsp

显示:r2

 <action name="r1">
  
<result type="redirectAction">r2</result>
</action>

 

 

  • 全局结果集:

jsp:

<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>

useraction:

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

如果很多action都有一个共同的结果页面, 比如上述的mainpage, 就可以单独提出来作为全局结果集:

如下struts.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.bjsxt.struts2.user.action.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.bjsxt.struts2.user.action.AdminAction">
    		<result>/admin.jsp</result>
    	</action>
    </package>
</struts>

如果admin的package要想使用全局结果局, 只需要继承user包即可:

 extends="user"

 

  • 动态结果集:dynamic result:

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>

userAction:

package com.bjsxt.struts2.user.action;

import 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";
	}
}

struts.xml:

<?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="user" namespace="/user" extends="struts-default">
    	
	    <action name="user" class="com.bjsxt.struts2.user.action.UserAction">
	    	<result>${r}</result>
	    </action>	    
    </package>    	
</struts> 

${r}用于从值栈 value stack里取值,每个action都有一个值栈

r在userAction里是动态确定的.所以叫做动态调用

此符号是xml配置文件里的Ognl表达式, 专门用于从action值栈里取出内容的.

 

  • 带参数的结果集:

jsp:

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

userAction:

package com.bjsxt.struts2.user.action;

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";
	}
}

struts.xml:

<?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="user" namespace="/user" extends="struts-default">
    	
	    <action name="user" class="com.bjsxt.struts2.user.action.UserAction">
	    	<result type="redirect">/user_success.jsp?t=${type}</result>
	    </action>	    
    </package>    	
</struts>

user_success.jsp:

因为是redirect客户端跳转, 所以不能共享一个值栈,即不能使用type,  所以要用t=${type}

也可以说一次request只有一个值栈, 

<body>
	User Success!
	from valuestack: <s:property value="t"/><br/>  //取不到
	from actioncontext: <s:property value="#parameters.t"/>
	<s:debug></s:debug>
</body>

<s:property value="t"/>格式属于从值栈取值, 因为是第二次request,当前没有action,所以没有值栈,也不能共用之前的值栈的值,

所以应该用<s:property value="#parameters.t"/>来取actionContext的值

 

 

  

 

  

 

 

  

你可能感兴趣的:(Struts2 语法--result type)