Struts2 ActionWildcard(通配符配置)约定优于配置

Posted 邵江莉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2 ActionWildcard(通配符配置)约定优于配置相关的知识,希望对你有一定的参考价值。

1.新建web Project:Struts2_ActionWildcard
2.新建以下的文件:
项目图:
  src:

    StudentAction.java

    TeacherAction.java

    struts.xml

  WebRoot:

    index.jsp

    Studentadd_success.jsp

    Studentdelete_success.jsp

    Teacher_add_success.jsp

    Teacher_delete_success.jsp

3.以下为项目中各文件的代码:

(1)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 
 6 <struts>
 7     <constant name="struts.devMode" value="true" />
 8     <package name="actions" extends="struts-default" namespace="/actions">
 9         <action name="Student*" class="com.bjsxt.struts2.action.StudentAction" method="{1}">
10             <result>/Student{1}_success.jsp</result>
11         </action>
12         
13         <action name="*_*" class="com.bjsxt.struts2.action.{1}Action" method="{2}">
14             <result>/{1}_{2}_success.jsp</result>
15             <!-- {0}_success.jsp -->
16         </action>
17     </package>
18 </struts>
struts.xml

(2)StudentAction.java

  代码:

 1 package com.bjsxt.struts2.action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 public class StudentAction extends ActionSupport {
 6     public String add() {
 7         return SUCCESS;
 8     }
 9     public String delete() {
10         return SUCCESS;
11     }
12     
13 }
StudentAction

------------------------------------------------------------------------------------

------------------------------------------------------------------------------------

(3)TeacherAction.java

  代码:

 1 package com.bjsxt.struts2.action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 public class TeacherAction extends ActionSupport {
 6     public String add() {
 7         return SUCCESS;
 8     }
 9     
10     public String delete() {
11         return SUCCESS;
12     }
13     
14     
15 }
TeacherAction

------------------------------------------------------------------------------------

------------------------------------------------------------------------------------

(4) index.jsp

  代码:

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<% 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=UTF-8" />
<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>

</body>
</html>

------------------------------------------------------------------------------------

------------------------------------------------------------------------------------

(5)Studentadd_success.jsp

  代码:

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!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=UTF-8" />
<title>Insert title here</title>
</head>
<body>
Student Add Success!
</body>
</html>

 

 如Studentadd_success.jsp页面案例所示,

 在Studentdelete_success.jsp页面中输出Student Delete Success!

     Teacher_add_success.jsp页面中输出Teacher Add Success!

    Teacher_delete_success.jsp页面中输出Teacher Delete Success!

 

                    

 

以上是关于Struts2 ActionWildcard(通配符配置)约定优于配置的主要内容,如果未能解决你的问题,请参考以下文章

Struts2——通配符,Action Method_DMI

01_6_Struts_ActionWildcard_通配符配置

通配符(WildCard)的使用

WildCard的使用

通配符的使用

struts2.5.2 通配符问题_亲测有用