天天看點

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)分别定義4個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 '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/css" href="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/css" href="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/css" href="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/css" href="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/css" href="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/css" href="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/css" href="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/css" href="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/css" href="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是用戶端跳轉,将産生2個request,是以這兩個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/css" href="styles.css">

-->

</head>

<body>

<a href="user/user?type=1">1. 向結果傳遞參數</a>

</body>

</html>

(2)配置struts.xml,redirect請求到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/css" href="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

繼續閱讀