1.动态方法调用
①index.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<a href="dologin">login</a>
<a href="doexecute">execute</a>
</body>
</html>
②struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!-- 版本,编码 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"><!-- 类型,地址 -->
<struts>
<!-- 开发模式 -->
<constant name="struts.devMode" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- 功能模块,一般命名空间与其名字相同,根路径+功能模块与Action匹配 -->
<package name="default" namespace="/" extends="struts-default">
<!-- /helloWord method="execute" -->
<action name="do*" class="struts.one.LoginAction" method="{1}">
<!--绝对路径,根路径 -->
<result name="login">/login.jsp</result>
<result name="success">/success.jsp</result>
<result name="error">/fail.jsp</result>
</action>
</package>
<include file="example.xml" />
<!-- Add packages here -->
</struts>
③LoginAction(根据do*,可匹配所有以do开头的Action)
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;
public String login() {
return LOGIN;
}
public String execute() {
return SUCCESS;
}
public String add() {
return ERROR;
}
}
④输入地址http://localhost:8080/hellostrus/index.jsp,点击login会进入登录界面,在地址栏加!add会调用add方法
如图:
2.通配符配置
①index.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<a href="Teacher">teacher</a>
<a href="Student">student</a>
</body>
</html>
②struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!-- 版本,编码 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"><!-- 类型,地址 -->
<struts>
<!-- 开发模式 -->
<constant name="struts.devMode" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- 功能模块,一般命名空间与其名字相同,根路径+功能模块与Action匹配 -->
<package name="default" namespace="/" extends="struts-default">
<action name="*_*" class="struts.one.{1}Action" method="{2}">
<result>/success.jsp</result>
<result name="{2}_{1}_success">/{2}_{1}_success.jsp</result>
</action>
</package>
<include file="example.xml" />
<!-- Add packages here -->
</struts>
③建立StudentAction和TeacherAction
package struts.one;
import com.opensymphony.xwork2.ActionSupport;
public class StudentAction extends ActionSupport {
public String add() {
return "add_Student_success";
}
public String delete() {
return "delete_Student_success";
}
}
package struts.one;
import com.opensymphony.xwork2.ActionSupport;
public class TeacherAction extends ActionSupport {
public String add() {
return "add_Teacher_success";
}
public String delete() {
return "delete_Teacher_success";
}
}
④建立对应的jsp页面
//add_Student_success
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
add_Student_success.jsp
</body>
</html>
//add_Teacher_success.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
add_Teacher_success.jsp
</body>
</html>
//delete_Teacher_success.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
delete_Teacher_success.jsp
</body>
</html>
⑤运行
输入地址http://localhost:8080/hellostrus/Teacher显示success,输入地址http://localhost:8080/hellostrus/Teacher_add进入登录成功界面