Struts2 Result 实例

 

1:Action result type

常用的四种类型

dispatcher,服务器端跳转

redirect,客户端跳转

chain,动作跳转,服务器端形式

redirectAction,动作跳转,客户端形式

 

2:Action result global results

 

struts.xml

 

 

 <package name="web_result" namespace="/web" extends="struts-default">	
<!-- 全局结果集 -->
		<global-results>
			<result name="globals">/result_file/result.jsp</result>
		</global-results>
		<!-- dispathcher 是服务器端跳转 -->
		<action name="web_result" class="com.result.action.ResultActionDemo01" method="add">
			<result name="success" type="dispatcher">/result_file/result1.jsp</result>
		</action> 
		<!--  redirect 是客户端跳转 -->
		<action name="web_forward" class="com.result.action.ResultActionDemo01" method="add">
			<result name="success" type="redirect">/result_file/result2.jsp</result>
		</action> 
		
		<!-- 客户端跳转  Action的跳转,如果有包则是包之间的跳转 -->
		 
		<action name="web_chain_namespace" class="com.result.action.ResultActionDemo01" method="add">
			
			<!--<result name="success" type="chain">web_action/web</result> -->
			<result name="success" type="chain">
				<param name="namespace">/web_action</param>
		   		<param name="actionName">web</param>
			</result>
		</action> 
		<action name="web_redirect_action" class="com.result.action.ResultActionDemo01" method="add">
			<result name="success" type="redirectAction">web_forward</result>
		</action>  
	</package>
	
	 <package name="extends_action" namespace="/web_extends" extends="web_result">
		<action name="web_extends" class="com.result.action.ResultActionDemo01" >
			<result name="success">/result_file/result4.jsp</result>
		</action> 
	</package>
 

 

java文件

 

 

index.jsp

<a href="/web/web_redirect_action.action?type=3">全局结果集</a><br/>

<a href="/web_extends/web_extends.action?type=3">继承全局结果集</a>

 

 

全局结果集扩展:

 

<global-results>
    <result name="error">/Error.jsp</result>
    <result name="invalid.token">/Error.jsp</result>
    <result name="login" type="redirectAction">Logon!input</result>
</global-results>
 

使用另外一个包中的动态结果使用extends,继承动态结果集的包名。

 

3:Action result dynamic results

 

struts.xml

 

 

 <package name="dynamic" namespace="/dynamic" extends="struts-default">
		<action name="dynamic_action" class="com.result.action.ResultActionDemo01" method="modify">
			<result  name="success">${to_path}</result>
			<result  name="back">/result_file/result.jsp?type=${type}</result>
		</action> 
	</package>
 

 

java 文件

 

package com.result.action;

import com.opensymphony.xwork2.ActionSupport;

public class ResultActionDemo01 extends ActionSupport {
	
	private String type;
	
	public String add() {
		System.out.println("============="+this.type);
		
		if("1".equals(type.toString())) {
			return "success";
		}
		else if("2".equals(type.toString())) {
			return "failure";
		}
		else if("3".equals(type.toString())) {
			return "globals";
		}
		else {
			return SUCCESS; 
		}
	}

	public String getType() {
		return type;
	}

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

 

 

package com.result.action;

import com.opensymphony.xwork2.ActionSupport;

public class ResultActionDemo01 extends ActionSupport {
	
	private String type;
	private String to_path;
	
	public String add() {
		System.out.println("============="+this.type);
		
		if("1".equals(type.toString())) {
			return "success";
		}
		else if("2".equals(type.toString())) {
			return "failure";
		}
		else if("3".equals(type.toString())) {
			return "globals";
		}
		else {
			return SUCCESS; 
		}
	}
	
	
	public String modify() {
		
		if("home".equals(type.toString())) {
			this.to_path="/result_file/home.jsp";
		}
		else if("details".equals(type.toString())) {
			this.to_path="/result_file/details.jsp";
		}
		else if("contents".equals(type.toString())) {
			this.to_path="/result_file/contents.jsp";		
		}
		else if("list".equals(type.toString())) {
			this.to_path="/result_file/list.jsp";
		} 
		
		if("redirectAction".equals(type.toString())) {
			this.type=type;
			return "back";
		}
		
		return "success"; 
	}
	
	
	
	
	
	
	public String getType() {
		return type;
	}

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


	public String getTo_path() {
		return to_path;
	}


	public void setTo_path(String to_path) {
		this.to_path = to_path;
	} 
}
 

 

 

index.jsp

 

	<a href="/dynamic/dynamic_action.action?type=home">动态结果集</a>
	<a href="/dynamic/dynamic_action.action?type=redirectAction">结果集带参数</a>

<result>${xxxx}</result>
 

 

在action中保存一个属性,存储具体的结果location

4:带结果参数

   <result>/xx/xx.jsp?xx=xxx</result>

forward:在struts2中只有一个请求在服务器端是可以共享的。

redirect:在strts2中只有一种情况的客户端请求传递参数使用<s:property value="#key.value"/>获取传递过来的参数。

 

 

例如:

 

${}表达式,(不是EL)

 

实验实验,分析分析

 

作业1:

提升能力:

先查Struts2 API 文档,在查GOOGLE

 

作业2:

一手鞋:查看OGNL 标签

 

系统分析设计

 

站在项目经理的角度,

考虑第一问题:界面原型,考虑什么样的架构;

第二问题:设计数据库,

第三问题:确定使用什么架构

第四问题:认真考虑使用什么样子的约定(如:配置,规定,命名等等)

你可能感兴趣的:(struts2)