struts2 result type= redirect redirectAction chain dispatcher等类型

struts.xml



struts2 result type= redirect redirectAction chain dispatcher等类型_第1张图片

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


<!-- 
总结
dispatcher 可以转发到web-inf目录(默认)
redirect 不能重定向到web-inf目录
chain链式转发  不用写param传递参数,系统会自动把上次的参数保存传递
redirect只能重定向到本namspace的action ,这里不能使用param属性
redirectAction可重定向到其他namspace的action ,这里必须用type="redirectAction”
plainText源码显示
 -->

<struts>
 
    <constant name="struts.devMode" value="true" />
<package name="struts1" namespace="/" extends="struts-default" >

<!-- dispatcher 可以转发到web-inf目录(默认) -->
  <action name="dispatcher_to_webinf" class="action.TiaoZhuan"  method="dispatcher">
<result name="dispatcher">
 /WEB-INF/web-inf.jsp
</result>
</action>
<!-- redirect 不能重定向到web-inf目录 -->
 <action name="redirect_to_webinf" class="action.TiaoZhuan"  method="redirect">
<result name="redirect" type="redirect">
 /WEB-INF/web-inf.jsp
</result>
</action>

<!--第一种  带参数重定向  直接写在链接里-->
 <!-- <action name="param_redirect" class="action.TiaoZhuan"  method="param_redirect">
<result name="param_redirect" type="redirect">
  /index1.jsp?id=${id}
 <param name="userName1">${userName1}</param>
</result>
</action>
-->
<!--第二种 带参数重定向  在param中写  -->
<action name="param_redirect" class="action.TiaoZhuan"  method="param_redirect">
<result name="param_redirect" type="redirect">
 <param name="location">/index1.jsp</param>
 <param name="id">${id}</param>
</result>
</action>

 <!-- chain链式转发  不用写param传递参数,系统会自动把上次的参数保存传递-->
<action name="chain" class="action.TiaoZhuan"  method="chain">
<result name="chain" type="chain">
 <param name="actionName">chain1</param>
<!--  <param name="userName">${userName}</param>
      <param name="userName1">${userName1}</param>
  -->
</result>
</action>
<action name="chain1" class="action.TiaoZhuan"  method="chain1">
<result name="chain1">
 /index1.jsp
</result>
</action>

<!-- redirect只能重定向到本namspace的action ,这里不能使用param属性-->
<action name="redirectAction" class="action.TiaoZhuan"  method="redirectAction">
<result name="redirectAction" type="redirect">
   dispatcher_to_webinf
</result>
</action>


<!-- redirectAction可重定向到其他namspace的action ,这里必须用type="redirectAction"-->
<action name="redirectOtherAction" class="action.TiaoZhuan"  method="redirectOtherAction">
<result name="redirectOtherAction" type="redirectAction">
   <param name="actionName">other_namespace</param>
   <param name="namespace">/lhy</param>
</result>
</action>

<!-- plainText源码显示 -->
<action name="plainText" class="action.TiaoZhuan" method="plainText">
<result name="plainText" type="plainText">
<param name="location">/index1.jsp</param>
<param name="charSet">UTF-8</param>
</result>
</action>
</package>

  <package name="struts2" namespace="/lhy" extends="struts-default" >
 <action  name="other_namespace" class="action.TiaoZhuan"  method="other_namespace">
     <result name="other_namespace">/index1.jsp</result>
  </action>
</package>
</struts>



jsp页面

<body>
    <a href="dispatcher_to_webinf">转发到webinf</a><br/>
    <a href="redirect_to_webinf">重定向到webinf</a><br/>
    <a href="chain">chain链式转发</a><br/>
    <a href="redirectAction">redirectAction重定向到action</a><br/>
     <a href="redirectOtherAction">redirectOtherAction重定向到其他namespace的action</a><br/>
      <a href="plainText">plainText源码</a><br/>
      <a href="lhy/other_namespace">lhy namespace下的action</a><br/>
       <a href="param_redirect?id=123">带参数的重定向</a><br/>
  </body>


java action代码


package action;


import com.opensymphony.xwork2.ActionSupport;


public class TiaoZhuan extends ActionSupport{

private String userName;
private String userName1;
private String id;


public String dispatcher()
{
return "dispatcher";
}

public String redirect()
{
System.out.println("redirect重定向jsp");
return "redirect";
}
public String redirectAction()
{
System.out.println("redirectAction重定向action");
return "redirectAction";
}

public String chain()
{
System.out.println("chain开始链式转发");
userName="lhy";
userName1="wxl";
return "chain";
}


public String chain1()
{
System.out.println("chain转发后的值"+userName+" "+userName1);
return "chain1";
}


public String plainText()
{
System.out.println("plainText源程序");
return "plainText";
}
public String plaintext()
{
System.out.println("源码显示");
return "plaintext";
}

public String redirectOtherAction()
{
System.out.println("开始重定向到其他的的namespace");
return "redirectOtherAction";
}

public String other_namespace()
{
System.out.println("已经重定向到其他的namespace");
return "other_namespace";
}

public String param_redirect()
{
id="456";
System.out.println("带参数的重定向");
return "param_redirect";
}

public String getUserName() {
return userName;
}


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


public String getUserName1() {
return userName1;
}


public void setUserName1(String userName1) {
this.userName1 = userName1;
}


public String getId() {
return id;
}


public void setId(String id) {
this.id = id;
}



}

你可能感兴趣的:(struts2 result type= redirect redirectAction chain dispatcher等类型)