Struts2学习——0400ActionWildCard通配符配置
来源:互联网 发布:西安交大大数据学院 编辑:程序博客网 时间:2024/06/05 10:52
背景
我们在前面用了DMI,动态的进行了方法的引用,得到了一个效果,只需要配置一个action,多个result就可以了,不用针对一个类的不同方法,配置不同的action。但是,这样我们是觉得烦,能不能action只有一个,result也只有一个呢?这就是我们通配符配置的前提或者说是背景。
两个特点:
- 用通配符配置,能把代码变得最简洁
- 要遵循“约定优于配置”原则
所谓的“约定优于配置”的意思是,只要一群人用一种约定去开发程序,就能是的整个配置文件的书写变得最简洁。所以,约定要比配置更重要。
通配符配置
基于背景,我们可以看看,到底能够怎么样配置使得struts.xml文件最简洁。
【注】“*”星号就是通配符,一个星号可以理解为代表一段字符串。例如
* 可以代表Teacher,也可以代表Student。* 可以代表add,也可以代表delete
1. 分析
struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <constant name="struts.devMode" value="true" /> <package name="actions" extends="struts-default" namespace="/actions"> <!-- 第一个action,action_name=user*,可以是useradd,也可以是userdelete... --> <!-- 由于可以有多个*,所以{1}代表第一个*代表的内容,如action_name=useradd,{1}就是add--> <action name="Student*" class="com.bjsxt.struts2.action.StudentAction" method="{1}"> <!-- 若action_name=useradd,result就跳转到Studentadd_success.jsp --> <!-- 当然,这些文件、java类、方法的命名方法要提前约定好。再一次的体现出了“约定优于配置” --> <result>/Student{1}_success.jsp</result> </action> <!-- 此action_name可以是Teacher_add,也可以是Course_delete --> <!-- 第一个通配符可以确定class的名称和位置,第二个通配符可以确定class中的方法 --> <action name="*_*" class="com.bjsxt.struts2.action.{1}Action" method="{2}"> <!-- result也可以由通配符决定,达到极简 --> <result>/{1}_{2}_success.jsp</result> <!-- {0}_success.jsp --> </action> </package></struts>
StudentAction
package com.bjsxt.struts2.action;import com.opensymphony.xwork2.ActionSupport;public class StudentAction extends ActionSupport { /* 两个方法add()和delete() 都是返回success */ public String add() { return SUCCESS; } public String delete() { return SUCCESS; }}
TeacherAction
package com.bjsxt.struts2.action;import com.opensymphony.xwork2.ActionSupport;public class TeacherAction extends ActionSupport { /* 同样TeacherAction也有两个方法 add()和delete(),返回success */ 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(); %><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><a href="<%=context %>/actions/Studentdelete">删除学生</a><br />不过,一定要遵守"约定优于配置"的原则<br /><a href="<%=context %>/actions/Teacher_add">添加老师</a><a href="<%=context %>/actions/Teacher_delete">删除老师</a><a href="<%=context %>/actions/Course_add">添加课程</a><a href="<%=context %>/actions/Course_delete">删除课程</a></body></html>
2. 结果
3. 分析
在演示中,我们可以看到,课程删除和课程添加还没完成,现在,完成以下这两部分。
CourseAction
package com.bjsxt.struts2.action;import com.opensymphony.xwork2.ActionSupport;public class CourseAction extends ActionSupport { public String add(){ return SUCCESS; } public String delete(){ return SUCCESS; }}
Course_add_sucess.jsp
package com.bjsxt.struts2.action;import com.opensymphony.xwork2.ActionSupport;public class CourseAction extends ActionSupport { public String add(){ return SUCCESS; } public String delete(){ return SUCCESS; }}
Course_delete_success.jsp
<?xml version="1.0" encoding="GB18030" ?><%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <%@taglib uri="/struts-tags" prefix="s" %><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> Course Delete Success!</body></html>
可以看到,我根本就没有动xml文件,就完成了功能的添加,所以这个通配符配置还是很有好处的。
若有不足之处,请不吝赐教
0 0
- Struts2学习——0400ActionWildCard通配符配置
- Struts2学习笔记——ActionWildcard(通配符)
- struts2 ActionWildCard 通配符配置
- Struts2 学习笔记 —— 06 —— ActionWildCard 通配符
- 有关Struts2——ActionWildcard通配符的学习
- 通配符做配置 actionWildcard
- ActionWildcard通配符配置
- Struts2 ActionWildcard(通配符配置)约定优于配置
- Struts2学习笔记—通配符
- struts2—使用通配符
- struts2学习:配置篇之通配符映射
- struts2学习:配置篇之通配符映射
- Struts2 学习笔记06 通配符配置
- struts2学习:配置篇之通配符映射
- 【Struts2学习笔记】struts2.5使用通配符配置action跳转
- Struts2学习笔记(一)——命名空间、路径、通配符
- Struts2学习笔记(一)——命名空间、路径、通配符
- Struts2 Action配置通配符
- ios 简单的block传值
- 求职之计算机网络
- 高中OJ3012. 【NOIP2012模拟10.6】购买
- 测试spring和Hibernate的整合,报错: “Unable to get the default Bean Validation”
- MVP
- Struts2学习——0400ActionWildCard通配符配置
- 访问国际英文版google
- 中文字符处理 python
- 一键设置Java环境变量
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料(下)
- Android开机过程
- XML解析-SAX方式
- 【NOIP2016提高A组五校联考2】running
- leetcode--37--数独问题