redirectAction 重定向

-------------------

redirectAction   重定向到另一个Actoin[分包内和包外]

-------------------
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>
    <package name="blog" namespace="/control/employee" extends="struts-default">
        <action name="add" class="blog.action.HelloWorld" method="execute">
            <result name="success" type="redirect">/addEmployee.jsp?username=${uname}</result>
        </action>
       

       <!--包外-->

        <action name="delDept">
            <result name="success" type="redirectAction">
                <param name="actionName">delete</param>
                <param name="namespace">/control/department</param>
            </result>
        </action>
       

       <!--包内-->

        <action name="addRedirect">
            <result name="success" type="redirectAction">add</result>
        </action>
    </package>

    
    <package name="other" namespace="/control/department" extends="struts-default">
        <action name="delete">
            <result>/WEB-INF/page/delDept.jsp</result>

           <!--缺少/WEB-INF/page/将无法访问-->

       </action>
    </package>    
</struts>



对于包外的情况,<result/>节点下的<param/>两个参数的name的值可以在struts-corexxxx.jar包中找到struts-defaul.xml中找到actionRedirect的实现类org.apache.struts2.dispatcher.ServletActionRedirectResult

这个类中有两个方法setActionName和setNamespace,这就是那两个参数的来历。


-------------------

plainText  显示一个jsp页面的源码

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

            </result>

             <!--在无需转码的情况下:
             <result type="plainText">/addEmployee.jsp</result>
             -->
</action>

-------------------

redirect 浏览器重定向

-------------------


HelloWorld.java:

package blog.action;

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



public class HelloWorld {
    private String msg;
    
    private String uname;
    
    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getMessage() {
        return msg;
    }
    
    public String execute(){
            try {
                uname = URLEncoder.encode("张三","UTF-8");//ISO8859-1
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        
        msg = "This is my first struts2 application!!!";
        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>
    <package name="blog" namespace="/control/employee" extends="struts-default">
        <action name="add" class="blog.action.HelloWorld" method="execute">
            <result name="success" type="redirect">/addEmployee.jsp?username=${uname}</result>
        </action>
    </package>
 
</struts>




addEmployee.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.net.URLDecoder"%>
<%
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 'addEmployee.jsp' starting page</title>
  </head>
 
  <body>
  ${param.username }<br/>
  <%=URLDecoder.decode( new String(request.getParameter("username").getBytes("ISO8859-1"),"UTF-8"),"UTF-8" )%>
   姓名: <input type="text" name="name">
   <BR/>
   
   uname:${uname }<!-- 如果采用的是浏览器重定向的方式访问那么就无法得到值 -->
  </body>
</html>

你可能感兴趣的:(struts2)