目录
1,分模块配置方法;
我们先在struts.xml里面写上两个.xml文件;
用到include语法
<include file="xiaoshou.xml"></include>
<include file="yanfa.xml"></include>
并且新建两个.xml文件如下;
xiaoshou.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="xiaoshou" namespace="/xiaoshou" extends="struts-default">
<action name="xiaoshou" class="com.java.action.XiaoShouAction">
<result name="success">${pageContext.request.contextPath}/success.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>
yanfa.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="yanfa" namespace="/yanfa" extends="struts-default">
<action name="yanfa" class="com.java.action.YanFaAction">
<result name="success">${pageContext.request.contextPath}/success.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>
然后我们写处理类XiaoShouAction类和YanFaAction类;
XiaoShouAction:
package com.java.action;
import com.opensymphony.xwork2.ActionSupport;
public class XiaoShouAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("执行了xiaoshou的默认方法!");
return SUCCESS;
}
}
YanFaAction:
package com.java.action;
import com.opensymphony.xwork2.ActionSupport;
public class YanFaAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("执行了yanfa的默认方法!");
return SUCCESS;
}
}
这时,我们可以测试一下,在浏览器中输入:
可以跳到success.jsp页面!
并且也调用了execute()方法:
2,struts2通配符;
先建立两个实体类;
SAction.java;
package com.java.action;
import com.opensymphony.xwork2.ActionSupport;
public class SAction 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;
}
}
GAction.java;
package com.java.action;
import com.opensymphony.xwork2.ActionSupport;
public class GAction 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;
}
}
struts.xml配置;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="manage" namespace="/" extends="struts-default">
<global-allowed-methods>regex:.*</global-allowed-methods>
<action name="student_*" class="com.java.action.SAction" method="{1}">
<result name="success">/success.jsp</result>
</action>
<action name="grade_*" class="com.java.action.GAction" method="{1}">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
sg.jsp;
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</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/>
<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>
success.jsp;
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
success!${name}
</body>
</html>
运行结果:
还有另外一种通配符格式;
<action name="*_*" class="com.java.action.{1}Action" method="{2}">
<result name="success">success.jsp</result>
</action>
使用这种格式的话要将sg.jsp里面的链接首字母要大写,而且实体类的名称要与链接名称与之对应!
3,动态方法调用;
sg.jsp页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</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/>
<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>
struts.xml;
<action name="student" class="com.java.action.SAction">
<result name="success">/success.jsp</result>
</action>
<action name="grade" class="com.java.action.GAction">
<result name="success">/success.jsp</result>
</action>
运行结果:
欢迎大家关注我的微信公众号《程序员的时光》,非常感谢!