Struts2 通配符配置

1、struts.xml

<struts>
 <constant name="struts.devMode" value="true" />
  <constant name="struts.enable.DynamicMethodInvocation" value="true" />
 <package name="actions" namespace="/actions" extends="struts-default">
        <action name="Student*" class="com.wxh.user.action.StudentAction" method="{1}">
        <!-- 前面如果有8个星号,就会排个顺序,1代表第一个星号 -->
            <result> 
            /Student{1}_success.jsp              
            </result>
        </action>
        
        <action name="*_*" class="com.wxh.user.action.{1}Action" method="{2}">
            <result> 
            /{1}_{2}_success.jsp              
            </result>
        </action>        
    </package>
</struts>

2、index.jsp

<body>
  使用通配符,将配置量降到最低<br>
    <a href=actions/Studentadd>添加学生</a> 
    <a href=actions/Studentdelete>删除学生</a> <br>
   不过,一定要遵守“约定优于配置”的原则<br>
   <a href=actions/Teacher_add>添加老师</a>
   <a href=actions/Teacher_delete>删除老师</a>

3、StudentAction.java

package com.wxh.user.action;

import com.opensymphony.xwork2.ActionSupport;

public class StudentAction extends ActionSupport{
	public String add(){
		return SUCCESS;		
	}
	public String delete(){
		return SUCCESS;
	}

}

4、TeacherAction.java

package com.wxh.user.action;

import com.opensymphony.xwork2.ActionSupport;

public class TeacherAction extends ActionSupport{
	public String add(){
		return SUCCESS;
	}
	
	public String delete(){
		return SUCCESS;
	}
}


5、接下来分别创建Studentadd_success.jsp和Studentdelete_success.jsp,Teacher_add_success.jsp  Teacher_delete_success.jsp(注意命名)


*代表所有,如果有多个能够匹配首先匹配最精确的。



你可能感兴趣的:(struts2.0)