第16讲 struts2重定向到action

重定向Action指,struts.xml中,在一个result结果直接跳转到另外一个action中,数据不可以共享,
1在HeadFirstStruts2chapter02_07中,新建HelloAction2,name2属性,get() set()方法,在默认的execute()方法中给 name2赋值,

package com.cruise.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction2 extends ActionSupport{
    
    private String name2 ;
    
    public String getName2() {
       return name2;
    }

    public void setName2(String name2) {
       this.name2 = name2;
    }

    @Override
    public String execute() throws Exception {
       this.name2="love Ashley";
       return SUCCESS;
    }
}
2修改HelloAction,写方法redirectAction()方法,返回字符串"redirectAction2"
package com.cruise.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport{
    
    private String name ;
    

    public String getName() {
       return name;
    }

    public void setName(String name) {
       this.name = name;
    }
    public String redirectAction(){
       return "redirectAction2";
    }
}
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.enable.DynamicMethodInvocation" value="true"/>
<package name="manage" namespace="/" extends="struts-default">
    <action name="hello" class="com.cruise.action.HelloAction" >
       <result name="redirectAction2" type="chain" >hello2result>
    action>
    <action name="hello2" class="com.cruise.action.HelloAction2" >
       <result name="success" type="dispatcher" >success.jspresult>
    action>
package>
struts>
4修改success.jsp
<%@ 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 heretitle>
head>
<body>
name:${name}
naem2:${name2}
body>
html>
5index.jsp文件
<%@ 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 heretitle>
head>
<body>
<a href="hello!redirectAction2?name=cruise" target="_blank">重定向Action</a>
body>
html>
5测试-运行,查看${name}和${name2}是否取到值-数据共享
http://localhost:8080/HeadFirstStruts2chapter02_07/

猜你喜欢

转载自blog.csdn.net/u010393325/article/details/83928665