1、 ActionMethod:
Action执行的时候并不一定要执行execute方法,有两种替换办法如下:
①在配置文件中配置action的时候用“method”属性来指定执行哪个方法
②在url地址中动态指定(动态方法调用DMI)(推荐使用)
Struts.xml文件的配置
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 <struts> 6 <constant name="struts.devMode" value="true" /> 7 <package name="user" extends="struts-default" namespace="/user"> 8 <action name="userAdd" class="com.bjsxt.struts2.user.action.UserAction" method="add"> 9 <result>/user_add_success.jsp</result> 10 </action> 11 <action name="user" class="com.bjsxt.struts2.user.action.UserAction"> 12 <result>/user_add_success.jsp</result> 13 <result name="delete">/user_delete_success.jsp</result> 14 </action> 15 </package> 16 </struts>
Index.jsp页面的内容:
1 <?xml version="1.0" encoding="GB18030" ?> 2 <%@ page language="java" contentType="text/html; charset=GB18030" 3 pageEncoding="GB18030"%> 4 <% String context = request.getContextPath(); %> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 6 <html xmlns="http://www.w3.org/1999/xhtml"> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=GB18030" /> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 <a href="<%=context %>/user/userAdd">添加用户</a><br /> 13 <a href="<%=context %>/user/user!add">添加用户</a><br /> 14 <a href="<%=context %>/user/user!delete">删除用户</a><br /> 15 </body> 16 </html>
UserAction:
1 package com.bjsxt.struts2.user.action; 2 import com.opensymphony.xwork2.ActionSupport; 3 public class UserAction extends ActionSupport { 4 public String add() { 5 return SUCCESS; 6 } 7 public String delete(){ 8 return "delete"; 9 } 10 }
2、 使用通配符
struts2的配置文件是 struts.xml 在这个配置文件里面可以使用通配符..其中的好处就是,大大减少了配置文件的内容.当然,付出的代价是可读性.
使用通配符的原则是 约定高于配置.
在项目中,我们有很多的命名规则是约定的.我们使用通配符那就必须有一个统一的约定.否则通配符将无法成立
Strtus.xml文件的配置
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 <struts> 6 <constant name="struts.devMode" value="true" /> 7 <package name="actions" extends="struts-default" namespace="/actions"> 8 <action name="Student_*" class="com.bjsxt.struts2.action.StudentAction" method="{1}"> 9 <result>/Student{1}_success.jsp</result> 10 </action> 11 12 <action name="*_*" class="com.bjsxt.struts2.action.{1}Action" method="{2}"> 13 <result>/{1}_{2}_success.jsp</result> 14 </action> 15 </package> 16 </struts>
{1}表示的是第一个"*"的内容,注意,这里的大括号,比如,如果是Student_add那么{1}就是表示 add ..
当然,这里只有一个"*".你可以有两个,甚至三个四个....比如这样写 *_* 这样就是两个"*" .那么我们也可以用{1},{2}来分别的表示他们.
Action的内容:
1 public class CourseAction extends ActionSupport { 2 public String add() { 3 return SUCCESS; 4 } 5 public String delete() { 6 return SUCCESS; 7 } 8 } 9 public class StudentAction extends ActionSupport { 10 public String add() { 11 return SUCCESS; 12 } 13 public String delete() { 14 return SUCCESS; 15 } 16 } 17 public class TeacherAction extends ActionSupport { 18 public String add() { 19 return SUCCESS; 20 } 21 public String delete() { 22 return SUCCESS; 23 } 24 }
Index.jsp:
1 <?xml version="1.0" encoding="GB18030" ?> 2 <%@ page language="java" contentType="text/html; charset=GB18030" 3 pageEncoding="GB18030"%> 4 <%String context = request.getContextPath();%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 6 <html xmlns="http://www.w3.org/1999/xhtml"> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=GB18030" /> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 使用通配符,将配置量降到最低<br /> 13 <a href="<%=context%>/actions/Studentadd">添加学生</a> 14 <a href="<%=context%>/actions/Studentdelete">删除学生</a><br /> 15 不过,一定要遵守"约定优于配置"的原则<br /> 16 <a href="<%=context%>/actions/Teacher_add">添加老师</a> 17 <a href="<%=context%>/actions/Teacher_delete">删除老师</a> 18 <a href="<%=context%>/actions/Course_add">添加课程</a> 19 <a href="<%=context%>/actions/Course_delete">删除课程</a> 20 </body> 21 </html>
相应的jsp页面有:
Course_add_seccess.jsp
Course_delete_success.jsp
Teacher_add_seccess.jsp
Teacher_delete_success.jsp
Studentadd_success.jsp
Studentdelete_success.jsp