1、 ActionMethod:Action执行的时候并不一定要执行execute方法,可以在配置文件中配置action的时候用“method”属性来指定执行哪个方法,也可以在url地址中动态指定(动态方法调用DMI)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>
<constant name= "struts.devMode" value= "true" />
<package name= "user" extends= "struts-default" namespace= "/user">
<action name= "userAdd" class= "com.bjsxt.struts2.user.action.UserAction" method= "add">
<result>/user_add_success.jsp</result>
</action>
<action name= "user" class= "com.bjsxt.struts2.user.action.UserAction">
<result>/user_add_success.jsp</result>
<result name= "delete">/user_delete_success.jsp</result>
</action>
</package>
</struts>
Index.jsp页面的内容:
<?xml version= "1.0" encoding= "GB18030" ?>
<%@ page language= "java" contentType= "text/html; charset=GB18030"
pageEncoding= "GB18030"%>
<% String context = request.getContextPath(); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns= "http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv= "Content-Type" content= "text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
<a href= "<%=context %> /user/userAdd">添加用户</a><br />
<a href= "<%=context %> /user/user!add">添加用户</a><br />
<a href= "<%=context %> /user/user!delete">删除用户</a><br />
</body>
</html>
UserAction的内容:
package com.bjsxt.struts2.user.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
public String add() {
return SUCCESS;
}
public String delete(){
return "delete";
}
}
2、 使用通配符
Strtus.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.devMode" value= "true" />
<package name= "actions" extends= "struts-default" namespace= "/actions">
<action name= "Student*" class= "com.bjsxt.struts2.action.StudentAction" method= "{1}">
<result>/Student{1}_success.jsp</result>
</action>
<action name= "*_*" class= "com.bjsxt.struts2.action.{1}Action" method= "{2}">
<result>/{1}_{2}_success.jsp</result>
</action>
</package>
</struts>
Action的内容:
public class CourseAction extends ActionSupport {
public String add() {
return SUCCESS;
}
public String delete() {
return SUCCESS;
}
}
public class StudentAction extends ActionSupport {
public String add() {
return SUCCESS;
}
public String delete() {
return SUCCESS;
}
}
public class TeacherAction extends ActionSupport {
public String add() {
return SUCCESS;
}
public String delete() {
return SUCCESS;
}
}
Index.jsp页面的内容:
<?xml version= "1.0" encoding= "GB18030" ?>
<%@ page language= "java" contentType= "text/html; charset=GB18030"
pageEncoding= "GB18030"%>
<%String context = request.getContextPath();%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns= "http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv= "Content-Type" content= "text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
使用通配符,将配置量降到最低<br />
<a href= "<%=context%> /actions/Studentadd">添加学生</a>
<a href= "<%=context%> /actions/Studentdelete">删除学生</a><br />
不过,一定要遵守"约定优于配置"的原则<br />
<a href= "<%=context%> /actions/Teacher_add">添加老师</a>
<a href= "<%=context%> /actions/Teacher_delete">删除老师</a>
<a href= "<%=context%> /actions/Course_add">添加课程</a>
<a href= "<%=context%> /actions/Course_delete">删除课程</a>
</body>
</html>
相应的jsp页面有:
Course_add_seccess.jsp
Course_delete_success.jsp
Teacher_add_seccess.jsp
Teacher_delete_success.jsp
Studentadd_success.jsp
Studentdelete_success.jsp
<?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.devMode" value= "true" />
<package name= "user" extends= "struts-default" namespace= "/user">
<action name= "userAdd" class= "com.bjsxt.struts2.user.action.UserAction" method= "add">
<result>/user_add_success.jsp</result>
</action>
<action name= "user" class= "com.bjsxt.struts2.user.action.UserAction">
<result>/user_add_success.jsp</result>
<result name= "delete">/user_delete_success.jsp</result>
</action>
</package>
</struts>
Index.jsp页面的内容:
<?xml version= "1.0" encoding= "GB18030" ?>
<%@ page language= "java" contentType= "text/html; charset=GB18030"
pageEncoding= "GB18030"%>
<% String context = request.getContextPath(); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns= "http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv= "Content-Type" content= "text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
<a href= "<%=context %> /user/userAdd">添加用户</a><br />
<a href= "<%=context %> /user/user!add">添加用户</a><br />
<a href= "<%=context %> /user/user!delete">删除用户</a><br />
</body>
</html>
UserAction的内容:
package com.bjsxt.struts2.user.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
public String add() {
return SUCCESS;
}
public String delete(){
return "delete";
}
}
2、 使用通配符
Strtus.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.devMode" value= "true" />
<package name= "actions" extends= "struts-default" namespace= "/actions">
<action name= "Student*" class= "com.bjsxt.struts2.action.StudentAction" method= "{1}">
<result>/Student{1}_success.jsp</result>
</action>
<action name= "*_*" class= "com.bjsxt.struts2.action.{1}Action" method= "{2}">
<result>/{1}_{2}_success.jsp</result>
</action>
</package>
</struts>
Action的内容:
public class CourseAction extends ActionSupport {
public String add() {
return SUCCESS;
}
public String delete() {
return SUCCESS;
}
}
public class StudentAction extends ActionSupport {
public String add() {
return SUCCESS;
}
public String delete() {
return SUCCESS;
}
}
public class TeacherAction extends ActionSupport {
public String add() {
return SUCCESS;
}
public String delete() {
return SUCCESS;
}
}
Index.jsp页面的内容:
<?xml version= "1.0" encoding= "GB18030" ?>
<%@ page language= "java" contentType= "text/html; charset=GB18030"
pageEncoding= "GB18030"%>
<%String context = request.getContextPath();%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns= "http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv= "Content-Type" content= "text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
使用通配符,将配置量降到最低<br />
<a href= "<%=context%> /actions/Studentadd">添加学生</a>
<a href= "<%=context%> /actions/Studentdelete">删除学生</a><br />
不过,一定要遵守"约定优于配置"的原则<br />
<a href= "<%=context%> /actions/Teacher_add">添加老师</a>
<a href= "<%=context%> /actions/Teacher_delete">删除老师</a>
<a href= "<%=context%> /actions/Course_add">添加课程</a>
<a href= "<%=context%> /actions/Course_delete">删除课程</a>
</body>
</html>
相应的jsp页面有:
Course_add_seccess.jsp
Course_delete_success.jsp
Teacher_add_seccess.jsp
Teacher_delete_success.jsp
Studentadd_success.jsp
Studentdelete_success.jsp