第15讲 struts2标签嵌套

链条方式指,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,写方法chain()方法,返回字符串"chain2"
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 chain(){
       return "chain2";
    }
}
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="chain2" type="chain" >hello2</result>
    </action>
    <action name="hello2" class="com.cruise.action.HelloAction2" >
       <result name="success" type="dispatcher" >success.jsp</result>
    </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 here</title>
</head>
<body>
name:${name}<br>
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 here</title>
</head>
<body>
<a href="hello!chain?name=cruise" target="_blank">内部转发</a>
</body>
</html>
5测试-运行,查看${name}和${name2}是否取到值-数据共享
http://localhost:8080/HeadFirstStruts2chapter02_07/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值