一、struts.xml配置:
1.分模块配置方法:
比如某个系统多个模块,我们把资产管理模块和车辆管理模块,分开,在总的struts.xml配置文件中include他们:
工程结构:
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><include file="cheliang.xml"></include><include file="zichan.xml"></include> </struts>
cheliang.xml:
<struts><package name="cheliang" namespace="/cheliang" extends="struts-default"><action name="cheliang" class="com.cy.action.CheLiangAction"><result name="success">${pageContext.request.contextPath}/success.jsp</result></action></package></struts>
zichan.xml:
<struts><package name="zichan" namespace="/zichan" extends="struts-default"><action name="zichan" class="com.cy.action.ZiChanAction"><result name="success">${pageContext.request.contextPath}/success.jsp</result></action></package></struts>
CheLiangAction.java:
public class CheLiangAction extends ActionSupport{private static final long serialVersionUID = 1L;public String execute() throws Exception {System.out.println("执行了CheLiangAction Action的默认方法");return SUCCESS;} }
ZiChanAction.java:
public class ZiChanAction extends ActionSupport{private static final long serialVersionUID = 1L;public String execute() throws Exception {System.out.println("执行了CheLiangAction Action的默认方法");return SUCCESS;} }
测试:
2.使用通配符:
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><package name="manage" namespace="/" extends="struts-default"><!-- method={1} 取到第一个通配符--><action name="student_*" class="com.cy.action.StudentAction" method="{1}"><result name="success">success.jsp</result></action><action name="grade_*" class="com.cy.action.GradeAction" method="{1}"><result name="success">success.jsp</result></action></package> </struts>
com.cy.action.GradeAction.java:
package com.cy.action;import com.opensymphony.xwork2.ActionSupport;public class GradeAction extends ActionSupport{private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public String list()throws Exception{System.out.println("班级查找");name="班级查找";return SUCCESS;}public String add()throws Exception{System.out.println("班级添加");name="班级添加";return SUCCESS;}public String update()throws Exception{System.out.println("班级修改");name="班级修改";return SUCCESS;}public String delete()throws Exception{System.out.println("班级删除");name="班级删除";return SUCCESS;}}
com.cy.action.StudentAction.java:
package com.cy.action;import com.opensymphony.xwork2.ActionSupport;public class StudentAction extends ActionSupport{private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public String list()throws Exception{System.out.println("学生查找");name="学生查找";return SUCCESS;}public String add()throws Exception{System.out.println("学生添加");name="学生添加";return SUCCESS;}public String update()throws Exception{System.out.println("学生修改");name="学生修改";return SUCCESS;}public String delete()throws Exception{System.out.println("学生删除");name="学生删除";return SUCCESS;}}
success.jsp:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body>OK! ${name} </body> </html>
index.jsp:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body><h1>通配符</h1><a href="student_list" target="_blank">学生信息查询</a> <a href="student_add" target="_blank">学生信息添加</a> <a href="student_update" target="_blank">学生信息修改</a> <a href="student_delete" target="_blank">学生信息删除</a> <br/><br/><a href="grade_list" target="_blank">班级信息查询</a> <a href="grade_add" target="_blank">班级信息添加</a> <a href="grade_update" target="_blank">班级信息修改</a> <a href="grade_delete" target="_blank">班级信息删除</a> <br/></body> </html>
测试:
3.上面的通配符配置方式还可以简化:
struts.xml:
<struts><package name="manage" namespace="/" extends="struts-default"><action name="*_*" class="com.cy.action.{1}Action" method="{2}"><result name="success">success.jsp</result></action></package> </struts>
因为{1}Action获取到student/grade是小写,index.jsp中改为大写的:
index.jsp:
<body><h1>通配符</h1><a href="Student_list" target="_blank">学生信息查询</a> <a href="Student_add" target="_blank">学生信息添加</a> <a href="Student_update" target="_blank">学生信息修改</a> <a href="Student_delete" target="_blank">学生信息删除</a> <br/><br/><a href="Grade_list" target="_blank">班级信息查询</a> <a href="Grade_add" target="_blank">班级信息添加</a> <a href="Grade_update" target="_blank">班级信息修改</a> <a href="Grade_delete" target="_blank">班级信息删除</a> <br/></body>
测试结果ok
二、动态方法调用:
上面的struts.xml修改为如下:
<struts><constant name="struts.enable.DynamicMethodInvocation" value="true" /><package name="manage" namespace="/" extends="struts-default"><action name="student" class="com.cy.action.StudentAction"><result name="success">success.jsp</result></action><action name="grade" class="com.cy.action.GradeAction"><result name="success">success.jsp</result></action></package> </struts>
index.jsp:
<body><h1>动态方法调用</h1><a href="student!list" target="_blank">学生信息查询</a> <a href="student!add" target="_blank">学生信息添加</a> <a href="student!update" target="_blank">学生信息修改</a> <a href="student!delete" target="_blank">学生信息删除</a> <br/><br/><a href="grade!list" target="_blank">班级信息查询</a> <a href="grade!add" target="_blank">班级信息添加</a> <a href="grade!update" target="_blank">班级信息修改</a> <a href="grade!delete" target="_blank">班级信息删除</a> <br/></body>
测试:
三、Action生命周期
例子:
com.cy.action.HelloAction.java:
package com.cy.action;import com.opensymphony.xwork2.ActionSupport;public class HelloAction extends ActionSupport{/*** */private static final long serialVersionUID = 1L;public HelloAction() {System.out.println(this);}@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubreturn SUCCESS;}}
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><package name="manage" namespace="/" extends="struts-default"><action name="hello" class="com.cy.action.HelloAction"><result name="success">success.jsp</result></action></package> </struts>
我们在每次请求hello链接的时候,产生HelloAction实例,实例的构造方法中打印了当前类对象;
测试结果:
多次访问:http://localhost:8080/Struts2Chap01/hello
console:
证明了:
----------------------