首页 > 代码库 > Struts2——Result

Struts2——Result

十五、结果类型

result类型

1、dispatcher(默认类型)

2、redirect(客户端跳转)

3、chain(action)

4、rediretAction(action)

5、freemarker

6、httpHeader

7、stream

8、velocity

9、xslt

10、plaintext

11、tiles

范例:

(1)配置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="resultType" extends="struts-default" namespace="/">

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

 

 

(2)分别定义4jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP ‘r1.jsp‘ starting page</title>

    

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/csshref="styles.css">

-->

 

  </head>

  

  <body>

    r1 <br>

  </body>

</html>

 

 

 

 

 

 

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP ‘r2.jsp‘ starting page</title>

    

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/csshref="styles.css">

-->

 

  </head>

  

  <body>

    r2 <br>

  </body>

</html>

 

 

 

 

 

 

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP ‘r1.jsp‘ starting page</title>

    

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/csshref="styles.css">

-->

 

  </head>

  

  <body>

    r3 <br>

  </body>

</html>

 

 

 

 

 

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP ‘r1.jsp‘ starting page</title>

    

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/csshref="styles.css">

-->

 

  </head>

  

  <body>

    r4 <br>

  </body>

</html>

 

 

(3)浏览器端访问

http://localhost:8080/ResultType/r1(浏览器端不会跳转到对应的action下的jsp页面)

 

http://localhost:8080/ResultTyppe/r2(客户端跳转,会跳转到对应action下的jsp页面)

 

http://localhost:8080/ResultTyppe/r3(forword到别的action,当需要跳转到别的包下的action时,需要配置<param name=”actionName”><param name=”namespace”>)

 

http://localhost:8080/ResultTyppe/r4

 

 

 

 

十六、全局结果GlobalResult

(1)编写index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP ‘index.jsp‘ starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/csshref="styles.css">

-->

  </head>

  

  <body>

    <a href="user/index?type=1">1.type=1</a><br />

    <a href="user/index?type=2">2.type=2</a><br />

    <a href="user/index?type=3">3.type=3</a>

  </body>

</html>

(2)编写UserAction.jsp

package com.zgy.result;

 

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;

}

public String execute(){

if(type == 1){

return SUCCESS;

}

else if(type == 2){

return ERROR;

}

else{

return "mainpage";

}

}

}

 

(3)配置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" extends="struts-default" namespace="/user">

<global-results>

<result name="mainpage">/mainpage.jsp</result>

</global-results>

<action name="index" class="com.zgy.result.UserAction">

<result>/user.jsp</result>

<result name="error">/error.jsp</result>

</action>

 

</package>

</struts>

(4)分别编写user.jsp/error.jsp/mainpage.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP ‘user.jsp‘ starting page</title>

    

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/csshref="styles.css">

-->

 

  </head>

  

  <body>

    user jsp <br>

  </body>

</html>

 

 

 

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP ‘error.jsp‘ starting page</title>

    

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/csshref="styles.css">

-->

 

  </head>

  

  <body>

    error jsp <br>

  </body>

</html>

 

 

 

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP ‘MainPage.jsp‘ starting page</title>

    

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/csshref="styles.css">

-->

 

  </head>

  

  <body>

    main page <br>

  </body>

</html>

 

(5)浏览器端访问

http://localhost:8080/GlobleResult/

当点击3.type=3时,将会跳转到<global-results>下的mainpage.jsp


十七、动态结果集

struts.xml中使用如下方式动态指定result的值:

${}

(1)创建index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP ‘index.jsp‘ starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/csshref="styles.css">

-->

  </head>

  

  <body>

    <a href="user/user?type=1">1.type=1</a><br />

    <a href="user/user?type=2">2.type=2</a><br />

  </body>

</html>

(2)配置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" extends="struts-default" namespace="/user">

<action name="user" class="com.zgy.result.UserAction">

<result>${r}</result>

</action>

</package>

</struts>

package com.zgy.result;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class UserAction extends ActionSupport{

private int type;

private String r;

public int getType() {

return type;

}

public void setType(int type) {

this.type = type;

}

public String getR() {

return r;

}

public void setR(String r) {

this.r = r;

}

public String execute(){

if(type == 1){

r = "/user_success.jsp";

}

else if(type == 2){

r = "/user_error.jsp";

}

return "success";

}

}

(3)浏览器端访问

 

 

十八、带参数的结果集

当一个action将一个请求forward到另一个action时,不需要传递参数。即:forward是服务器端跳转,只产生一个request,一次request只有一个值栈。

当一个action将一个请求redirect到另一个action时,需要传递参数。即:redirect是客户端跳转,将产生2request,所以这两个action不能共享同一个值栈。

范例:

(1)创建index.jsp页面,传递参数为type=1

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme() + "://"

+ request.getServerName() + ":" + request.getServerPort()

+ path + "/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

 

<title>My JSP ‘index.jsp‘ starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/csshref="styles.css">

-->

</head>

 

<body>

<a href="user/user?type=1">1. 向结果传递参数</a>

</body>

</html>

(2)配置struts.xmlredirect请求到user_success.jsp

<?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="action" extends="struts-default" namespace="/user">

        <action name="user" class="com.zgy.result.UserAction">

            <result type="redirect">/user_success.jsp?t=${type}</result>

        </action>

    </package>

</struts>

(3)编写UserAction.java接受参数值

package com.zgy.result;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class UserAction extends ActionSupport {

private String type;

 

public String getType() {

return type;

}

 

public void setType(String type) {

this.type = type;

}

public String execute(){

return SUCCESS;

}

}

(4)编写user_success.jsp,获取参数值

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%@taglib uri="/struts-tags" prefix="s" %>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP ‘user_success.jsp‘ starting page</title>

    

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/csshref="styles.css">

-->

 

  </head>

  

  <body>

    user success <br>

    from ValueStack:<s:property value="t"></s:property><br />

    from ActionContext:<s:property value="#parameters.t"></s:property>

    <s:debug></s:debug>

  </body>

</html>

(5)浏览器端访问

 

十九、Result总结

1、常用四种类型

A)dispatcher(默认)

B)rediect

C)chain

D)redirectAction

2、全局结果集

A)global-results | extends

3、动态结果(了解)

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

4、传递参数

A)客户端跳转才需要传递

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



Struts2——Result