struts2的框架解析第5篇action中result的各种转发类型

result配置类似于struts1中的forward,但struts2中提供了多种结果类型,常用的类型有:dispatcher(默认值,内部转发)、redirect(重定向到某个路径)、redirectAction(重定向到某个action)、plainText(输出页面的源代码)。

在result中可以用${属性名}表达式【ognl表达式】访问action中的属性,表达式中的属性名对应action中的属性,如下:

<result type = "redirect">/view.jsp?id=${id}</result>

下面是redirectAction结果类型的例子,如果重定向的action中同一个包下:

<result type = "redirectAction">
        <param name = "actionName">hello world</param>
        <param name = "namespace">/test</param>
</result>

plaintText:显示原始文件内容,例如:当我们需要原样显示jsp文件源代码的时候,就可以使用此类型。

<result name = "source" type = "plainText">
            <param name = "localtion">/xxx.jsp</param>
            <param name = "charSet">UTF-8</param><!--指定读取文件的编码-->
</result>

代码如下:

package com.gz.action;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class HelloWorldAction {

    private String mes;
    private String userName;

    public String getMessage() {
        return mes;
    }

    public String execute() throws UnsupportedEncodingException {
        mes = "hello world!";
        this.userName = URLEncoder.encode("你好,世界!","UTF-8");//进行编码
        return "success";
    }

    public String addGlob() {
        return "message";
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

}

adds.jsp的代码如下:

<%@page import="java.util.*,java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${param.userName}
<%-- <%= URLDecoder.decode(new String(request.getParameter("userName").getBytes("ISO8859-1"),"UTF-8"),"UTF-8") %> --%>
</body>
</html>

struts.xml文件的代码版本一:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

<!-- START SNIPPET: xworkSample -->
<struts>

    <!-- http://localhost:8080/struts/test/helloworld -->
    <package name="hello" extends="struts-default" namespace="/test">
        <action name="helloworld" class="com.gz.action.HelloWorldAction" method="execute">
            <!-- <result name = "success">/WEB-INF/test/hello.jsp</result> -->
            <!-- 重定向到WEB-INF外面到jsp页面 -->
            <result name = "success" type = "redirect">/adds.jsp?userName=${userName}</result>
        </action>
        <action name = "add">
            <result>/WEB-INF/test/add.jsp</result>
        </action>
        <!--  http://localhost:8080/struts/test/redirect.action -->
        <action name="redirect">
            <result type="redirect">/adds.jsp</result>
        </action>

        <!-- 重定向到其他action -->
        <!--  http://localhost:8080/struts/test/redirectAction.action -->
        <action name="redirectAction">
            <result type = "redirectAction">helloworld</result>
        </action>

        <!--  http://localhost:8080/struts/test/plainText.action -->
        <action name="plainText">
            <result type="plainText">
                <param name="location">/adds.jsp</param>
                <param name="charset">UTF-8</param><!-- 指定读取文件的编码 -->
            </result>
        </action>

        <!-- 重定向到其他包到action -->
        <!--  http://localhost:8080/struts/test/redirectOtherAtion.action -->
        <action name="redirectOtherAtion">
            <result type = "redirectAction">
                <param name="actionName">xx</param>
                <param name="namespace">/other</param>
            </result>
        </action>
    </package>

    <package name="other" namespace="/other" extends="struts-default">

        <!-- 包内的全局视图 -->
        <!--  http://localhost:8080/struts/other/mange.action -->
        <global-results>
            <result name = "message">/WEB-INF/test/message.jsp</result>
        </global-results>
        <action name="mange" class = "com.gz.action.HelloWorldAction" method="addGlob"></action>

        <action name="xx">
            <result>/WEB-INF/test/hello.jsp</result>
        </action>
    </package>

</struts>

<!-- END SNIPPET: xworkSample -->

struts.xml文件的代码版本二:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

<!-- START SNIPPET: xworkSample -->
<struts>

    <package name="base" extends="struts-default">
        <global-results>
            <result name = "message">/WEB-INF/test/message.jsp</result>
        </global-results>
    </package>

    <!-- http://localhost:8080/struts/test/helloworld -->
    <package name="hello" extends="base" namespace="/test">
        <action name="helloworld" class="com.gz.action.HelloWorldAction" method="execute">
            <!-- <result name = "success">/WEB-INF/test/hello.jsp</result> -->
            <!-- 重定向到WEB-INF外面到jsp页面 -->
            <result name = "success" type = "redirect">/adds.jsp?userName=${userName}</result>
        </action>
        <action name = "add">
            <result>/WEB-INF/test/add.jsp</result>
        </action>
        <!--  http://localhost:8080/struts/test/redirect.action -->
        <action name="redirect">
            <result type="redirect">/adds.jsp</result>
        </action>

        <!-- 重定向到其他action -->
        <!--  http://localhost:8080/struts/test/redirectAction.action -->
        <action name="redirectAction">
            <result type = "redirectAction">helloworld</result>
        </action>

        <!--  http://localhost:8080/struts/test/plainText.action -->
        <action name="plainText">
            <result type="plainText">
                <param name="location">/adds.jsp</param>
                <param name="charset">UTF-8</param><!-- 指定读取文件的编码 -->
            </result>
        </action>

        <!-- 重定向到其他包到action -->
        <!--  http://localhost:8080/struts/test/redirectOtherAtion.action -->
        <action name="redirectOtherAtion">
            <result type = "redirectAction">
                <param name="actionName">xx</param>
                <param name="namespace">/other</param>
            </result>
        </action>
    </package>

    <package name="other" namespace="/other" extends="base">

        <!-- 内部转发到整个struts.xml的全局视图 -->
        <!--  http://localhost:8080/struts/other/mange.action -->
        <action name="mange" class = "com.gz.action.HelloWorldAction" method="addGlob"></action>

        <action name="xx">
            <result>/WEB-INF/test/hello.jsp</result>
        </action>
    </package>

</struts>

<!-- END SNIPPET: xworkSample -->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值