欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

struts2分模块与action生命周期

程序员文章站 2022-07-12 16:13:54
...
1. 分模块

struts.xml
<include file=""></include>

新建项目HeadFirstStruts2Chap02_04


CheLiangAction.java

package com.andrew.action;
import com.opensymphony.xwork2.ActionSupport;
public class CheLiangAction extends ActionSupport {
    @Override
    public String execute() throws Exception {
        System.out.println("执行了CheLiangAction Action的默认方法");
        return SUCCESS;
    }
}

ZiChanAction.java

package com.andrew.action;
import com.opensymphony.xwork2.ActionSupport;
public class ZiChanAction extends ActionSupport {
    @Override
    public String execute() throws Exception {
        System.out.println("执行了ZiChanAction Action的默认方法");
        return SUCCESS;
    }
}

cheliang.xml

<package name="cheliang" namespace="/cheliang" extends="struts-default">
    <action name="cheliang" class="com.andrew.action.CheLiangAction">
        <result name="success">${pageContext.request.contextPath}/success.jsp</result>
    </action>
</package>

zichan.xml

<package name="zichan" namespace="/zichan" extends="struts-default">
    <action name="zichan" class="com.andrew.action.ZiChanAction">
        <result name="success">${pageContext.request.contextPath}/success.jsp</result>
    </action>
</package>

struts.xml

<struts>
    <include file="cheliang.xml"/>
    <include file="zichan.xml"/>
</struts>

success.jsp

Ok!

http://localhost:8080/HeadFirstStruts2Chap02_04/cheliang/cheliang
Ok!
执行了CheLiangAction Action的默认方法
http://localhost:8080/HeadFirstStruts2Chap02_04/zichan/zichan
Ok!
执行了ZiChanAction Action的默认方法


2. 生命周期

新建项目HeadFirstStruts2Chap02_06


HelloAction.java

package com.andrew.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport {
    public HelloAction() {
        System.out.println(this);
    }
    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }
}

struts.xml

<package name="manage" namespace="/" extends="struts-default">
    <action name="hello" class="com.andrew.action.HelloAction">
        <result name="success">success.jsp</result>
    </action>
</package>

success.jsp

Ok!

http://localhost:8080/HeadFirstStruts2Chap02_06/hello
com.andrew.action.HelloAction@1e3ec646
http://localhost:8080/HeadFirstStruts2Chap02_06/hello
com.andrew.action.HelloAction@46867b6b
相关标签: Java struts2