struts2分模块与action生命周期

阅读更多
1. 分模块

struts.xml


新建项目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


    
        ${pageContext.request.contextPath}/success.jsp
    


zichan.xml


    
        ${pageContext.request.contextPath}/success.jsp
    


struts.xml


    
    


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


    
        success.jsp
    


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)