Struts2学习笔记——ActionWildcard(通配符)

来源:互联网 发布:中国芯片 知乎 编辑:程序博客网 时间:2024/05/21 16:58

使用通配符可以将配置量降到最低。但是一定要遵守“约定由于配置”的原则

Struts.xml:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts>  <constant name="struts.devMode" value="true" />  <!-- 通配符的使用 -->  <constant name="struts.enable.DynamicMethodInvocation" value="false" />   <package name="user" namespace="/actions" extends="struts-default">        <action name="Student*" class="com.smile.struts2.front.action.StudentAction" method="{1}">            <result name="success"> /Student{1}_success.jsp  </result>        </action>                <action name="*_*" class="com.smile.struts2.front.action.{1}Action" method="{2}">            <result name="success"> /{1}_{2}_success.jsp  </result>        </action>    </package></struts>
在这个配置中我们可以看到在配置StudentAction的时候使用了一个通配符来匹配Action的name。

<action name="Student*" class="com.smile.struts2.front.action.StudentAction" method="{1}">            <result name="success"> /Student{1}_success.jsp  </result>        </action>
表示如果我们的action是Studentadd则这里的method = “add” 返回指向的jsp文件就是Studentadd_success.jsp。

<action name="*_*" class="com.smile.struts2.front.action.{1}Action" method="{2}">            <result name="success"> /{1}_{2}_success.jsp  </result>        </action>
这个配置中使用了两个占位符。如果name = "Teacher_add" 则对应的class = “com.smile.struts2.front.action.TeacherAction” method = "add" 返回指向的jsp界面就是Teacher_add_success.jsp。

增加三个action类来处理:

CourseAction:

package com.smile.struts2.front.action;import com.opensymphony.xwork2.ActionSupport;public class CourseAction extends ActionSupport {//public String execute(){//return "path";//}public String add(){return SUCCESS;}public String delete(){return SUCCESS;}}
TeacherAction:

package com.smile.struts2.front.action;import com.opensymphony.xwork2.ActionSupport;public class TeacherAction extends ActionSupport {//public String execute(){//return "path";//}public String add(){return SUCCESS;}public String delete(){return SUCCESS;}}
StudentAction:

package com.smile.struts2.front.action;import com.opensymphony.xwork2.ActionSupport;public class StudentAction extends ActionSupport {//public String execute(){//return "path";//}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> <!-- Studentadd_success.jsp --><a href="<%=context %>/actions/Studentdelete">删除学生</a> <!-- Studentdelete_success.jsp --><br />不过,一定要遵守"约定优于配置"的原则<br /><a href="<%=context %>/actions/Teacher_add">添加老师</a> <!-- Teacher_add_success.jsp --><a href="<%=context %>/actions/Teacher_delete">删除老师</a> <!-- Teacher_delete_success.jsp --><a href="<%=context %>/actions/Course_add">添加课程</a> <!-- Course_add_success.jsp --><a href="<%=context %>/actions/Course_delete">删除课程</a><!-- Course_delete_success.jsp -->    </body></html>
这样就可以使用通配符减少在Struts.xml中的配置。

0 0