struts2的action方法匹配以及通配符的使用

 1、 ActionMethod:Action执行的时候并不一定要执行execute方法,可以在配置文件中配置action的时候用“method”属性来指定执行哪个方法,也可以在url地址中动态指定(动态方法调用DMI)Struts.xml文件的配置:
"1.0" encoding= "UTF-8" ?>
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

    "struts.devMode" value= "true" />
    "user" extends= "struts-default" namespace= "/user">
        "userAdd" class= "com.bjsxt.struts2.user.action.UserAction" method= "add">
            /user_add_success.jsp
       

        "user" class= "com.bjsxt.struts2.user.action.UserAction">
            /user_add_success.jsp
            "delete">/user_delete_success.jsp
       

   


Index.jsp页面的内容:
"1.0" encoding= "GB18030" ?>
<%@ page language= "java" contentType= "text/html; charset=GB18030"
    pageEncoding= "GB18030"%>
<% String context = request.getContextPath(); %>

"http://www.w3.org/1999/xhtml">

"Content-Type" content= "text/html; charset=GB18030" />
Insert title here


     "<%=context %> /user/userAdd">添加用户

     "<%=context %> /user/user!add">添加用户

     "<%=context %> /user/user!delete">删除用户



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文件的配置
"1.0" encoding= "UTF-8" ?>
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

    "struts.devMode" value= "true" />
    "actions" extends= "struts-default" namespace= "/actions">
        "Student*" class= "com.bjsxt.struts2.action.StudentAction" method= "{1}">
            /Student{1}_success.jsp
       

       
        "*_*" class= "com.bjsxt.struts2.action.{1}Action" method= "{2}">
            /{1}_{2}_success.jsp
       

   


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页面的内容:
"1.0" encoding= "GB18030" ?>
<%@ page language= "java" contentType= "text/html; charset=GB18030"
     pageEncoding= "GB18030"%>
<%String context = request.getContextPath();%>

"http://www.w3.org/1999/xhtml">
    
         "Content-Type" content= "text/html; charset=GB18030" />
         Insert title here
    
    
         使用通配符,将配置量降到最低

         "<%=context%> /actions/Studentadd">添加学生
         "<%=context%> /actions/Studentdelete">删除学生

         不过,一定要遵守"约定优于配置"的原则

         "<%=context%> /actions/Teacher_add">添加老师
         "<%=context%> /actions/Teacher_delete">删除老师
         "<%=context%> /actions/Course_add">添加课程
         "<%=context%> /actions/Course_delete">删除课程
    

相应的jsp页面有:
Course_add_seccess.jsp
Course_delete_success.jsp
Teacher_add_seccess.jsp
Teacher_delete_success.jsp
Studentadd_success.jsp
Studentdelete_success.jsp
 

你可能感兴趣的:(struts2相关)