struts2-Action配置-通配符-DMI
1、 ActionMethod:
Action执行的时候并不一定要执行execute方法,有两种替换办法如下:
①在配置文件中配置action的时候用“method”属性来指定执行哪个方法
②在url地址中动态指定(动态方法调用DMI)(推荐使用)
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="user" extends="struts-default" namespace="/user">
<action name="userAdd" class="com.bjsxt.struts2.user.action.UserAction" method="add">
<result>/user_add_success.jsp</result>
</action>
<action name="user" class="com.bjsxt.struts2.user.action.UserAction">
<result>/user_add_success.jsp</result>
<result name="delete">/user_delete_success.jsp</result>
</action>
</package>
</struts>
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>
<a href="<%=context %>/user/userAdd">添加用户</a><br />
<a href="<%=context %>/user/user!add">添加用户</a><br />
<a href="<%=context %>/user/user!delete">删除用户</a><br />
</body>
</html>
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、 使用通配符
struts2的配置文件是 struts.xml 在这个配置文件里面可以使用通配符..其中的好处就是,大大减少了配置文件的内容.当然,付出的代价是可读性.
使用通配符的原则是 约定高于配置.
在项目中,我们有很多的命名规则是约定的.我们使用通配符那就必须有一个统一的约定.否则通配符将无法成立
Strtus.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 name="Student_*" class="com.bjsxt.struts2.action.StudentAction" method="{1}">
<result>/Student{1}_success.jsp</result>
</action> <action name="*_*" class="com.bjsxt.struts2.action.{1}Action" method="{2}">
<result>/{1}_{2}_success.jsp</result>
</action>
</package>
</struts>
{1}表示的是第一个"*"的内容,注意,这里的大括号,比如,如果是Student_add那么{1}就是表示 add ..
当然,这里只有一个"*".你可以有两个,甚至三个四个....比如这样写 *_* 这样就是两个"*" .那么我们也可以用{1},{2}来分别的表示他们.
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:
<?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>
<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>
相应的jsp页面有:
Course_add_seccess.jsp
Course_delete_success.jsp
Teacher_add_seccess.jsp
Teacher_delete_success.jsp
Studentadd_success.jsp
Studentdelete_success.jsp
struts2-Action配置-通配符-DMI的更多相关文章
- struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法
struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法
- struts2 正确配置通配符方式访问,报错解决
今天遇到正确配置通配符访问action的方法,但是还是报错,原因struts 2.3 以后会内部会验证是否允许该方法,而我用的刚好是2.5的版本 要action配置中加上<allowed-met ...
- 第三章Struts2 Action中动态方法调用、通配符的使用
01.Struts 2基本结构 使用Struts2框架实现用登录的功能,使用struts2标签和ognl表达式简化了试图的开发,并且利用struts2提供的特性对输入的数据进行验证,以及访问Servl ...
- Struts2 Action中动态方法调用、通配符的使用
一.Struts2执行过程图: 二.struts2配置文件的加载顺序 struts-default.xml---struts-plugin.xml---struts.xml 具体步骤: 三.Actio ...
- Action中动态方法的调用 Action中通配符的使用 Result的配置
Action中动态方法的调用 动态方法调用(Dynamic Method Invocation,DMI) 标识符:! 一.通过以下选中的文件来查看是否禁止调用动态方法
- 关于Struts2中 Action 配置method的解读
为Action配置method属性: 将Action类中的每一个处理方法都定义成一个逻辑Action方法. <!DOCTYPE struts PUBLIC "-//Apache Sof ...
- struts2 action通配符
首先,看一个struts2的配置文件: <package name="actions" extends="struts-default" namespac ...
- java:struts2.3框架1(struts2快速配置,各文件之间的关系,基础代码简化版,XML中的通配符)
1.struts2快速配置: A.到http://struts.apache.org下载struts2开发包struts-2.3.32-all.zip B.新建web项目并添加struts2依赖的ja ...
- [JavaWeb基础] 018.Struts2 Action通配符使用
Struts2中有一个很牛逼的action通配符,可以用来简化action配置,以我们将要讲解的案例来说,如果我们要对一个学生信息进行增加,删除,修改,那么按照原来的做法,我们需要写3个Action来 ...
随机推荐
- STL之内存处理
说明:本文仅供学习交流,转载请标明出处,欢迎转载! STL中与内存配置相关的类是allocator类,头文件为:#include<alllocator>这是一个模板类,用于内存的分配.对象 ...
- Sql 列转行 三种方法对比
合并列值 --******************************************************************************************* ...
- spring cloud中通过配置文件自定义Ribbon负载均衡策略
一.Ribbon中的负载均衡策略 1.Ribbon中支持的负载均衡策略 AvailabilityFilteringRule:过滤掉那些因为一直连接失败的被标记为circuit tripped的后端se ...
- js操作checkbox(复选框)的方法总结
收集了一些用js代码操作checkbox复选框的代码,分享出来,供需要的朋友参考: <script> //复选框checkbox 处理方法 //搜集整理 www.jbxue.com fun ...
- VS中批量删除注释
批量删除: 按ctrl+H 选上正则表达式 Find what: //.* Replace with: (空) 点replace all就行了
- [self.view addSubview:vc2.view]程序崩溃的解决办法
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UIButt ...
- angular学习笔记(七)-迭代3
每个迭代项中还有以下三个变量: $first: 判断是否是迭代第一项,如果是,得到true,如果不是,得到false $middle: 判断是否迭代中间项(既不是第一项也不是最后一项的都是中间项),如 ...
- Oracle PLSQL Demo - 01.定义变量、打印信息
declare v_sal ) :; begin --if you could not see the output in console, you should set output on firs ...
- Filter method example
The Scala List class filter method implicitly loops over the List/Seq you supply, tests each element ...
- C++Primer 4th edition读书笔记-第二章
1 变量的定义用于为变量分配存储空间,还可以为变量指定初始值.在一个程序中,变量有且只有一个定义.声明用于向程序表明变量的名字和类型.定义也是声明:当定义变量时,我们声明了它的类型和名字.可以通过使用 ...