首页 > 代码库 > Struts2第四天
Struts2第四天
三. 分模块配置方法
<inclue file=""></include>
XML
cheliang.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="cheliang" namespace="/cheliang" extends="struts-default">
<action name="cheliang" class="com.java1234.action.CheLiangAction">
<result name="success">${pageContext.request.contextPath}/success.jsp</result>
</action>
</package>
</struts>
zichan.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="zichan" namespace="/zichan" extends="struts-default">
<action name="zichan" class="com.java1234.action.ZiChanAction">
<result name="success">${pageContext.request.contextPath}/success.jsp</result>
</action>
</package>
</struts>
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>
action
CheLiangAction.java
package com.java1234.action;
import com.opensymphony.xwork2.ActionSupport;
public class CheLiangAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception {
System.out.println("执行了CheLiangAction Action的默认方法");
return SUCCESS;
}
}
ZiChanAction.java
package com.java1234.action;
import com.opensymphony.xwork2.ActionSupport;
public class ZiChanAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception {
System.out.println("执行了ZiChanAction Action的默认方法");
return SUCCESS;
}
}
四. 使用通配符
action
GradeAction.java
package com.java1234.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;
}
}
StudentAction.java
package com.java1234.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;
}
}
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="http://www.mamicode.com/true" />
<package name="manage" namespace="/" extends="struts-default">
<!-- <action name="student_*" class="com.java1234.action.StudentAction" method="{1}">
<result name="success">success.jsp</result>
</action>
<action name="grade_*" class="com.java1234.action.GradeAction" method="{1}">
<result name="success">success.jsp</result>
</action> -->
<!-- <action name="*_*" class="com.java1234.action.{1}Action" method="{2}">
<result name="success">success.jsp</result>
</action> -->
<action name="student" class="com.java1234.action.StudentAction">
<result name="success">success.jsp</result>
</action>
<action name="grade" class="com.java1234.action.GradeAction">
<result name="success">success.jsp</result>
</action>
</package>
</struts>
第六节.动态方法调用
<constant name="struts.enable.DynamicMethodInvocation" value="http://www.mamicode.com/true" />
XML文件
<%@ 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>
<h1>通配符</h1>
<a href="http://www.mamicode.com/Student_list" target="_blank">学生信息查询</a>
<a href="http://www.mamicode.com/Student_add" target="_blank">学生信息添加</a>
<a href="http://www.mamicode.com/Student_update" target="_blank">学生信息修改</a>
<a href="http://www.mamicode.com/Student_delete" target="_blank">学生信息删除</a>
<br/><br/>
<a href="http://www.mamicode.com/Grade_list" target="_blank">班级信息查询</a>
<a href="http://www.mamicode.com/Grade_add" target="_blank">班级信息添加</a>
<a href="http://www.mamicode.com/Grade_update" target="_blank">班级信息修改</a>
<a href="http://www.mamicode.com/Grade_delete" target="_blank">班级信息删除</a> <br/>
<h1>动态方法调用</h1>
<a href="http://www.mamicode.com/student!list" target="_blank">学生信息查询</a>
<a href="http://www.mamicode.com/student!add" target="_blank">学生信息添加</a>
<a href="http://www.mamicode.com/student!update" target="_blank">学生信息修改</a>
<a href="http://www.mamicode.com/student!delete" target="_blank">学生信息删除</a>
<br/><br/>
<a href="http://www.mamicode.com/grade!list" target="_blank">班级信息查询</a>
<a href="http://www.mamicode.com/grade!add" target="_blank">班级信息添加</a>
<a href="http://www.mamicode.com/grade!update" target="_blank">班级信息修改</a>
<a href="http://www.mamicode.com/grade!delete" target="_blank">班级信息删除</a> <br/>
</body>
</html>
第七节: Action生命周期
Action
HelloAction
public class HelloAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
public HelloAction() {
System.out.println(this);
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}
struts.xml
<!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.java1234.action.HelloAction">
<result name="success">success.jsp</result>
</action>
</package>
</struts>
注意:每次请求都会创建action实例,Spring默认是单例
Struts2第四天