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来 ...
随机推荐
- Unix域套接字简介
在Linux系统中,有很多进程间通信方式,套接字(Socket)就是其中的一种.但传统的套接字的用法都是基于TCP/IP协议栈的,需要指定IP地址.如果不同主机上的两个进程进行通信,当然这样做没什么问 ...
- nginx实战五
nginx用户认证 https://coding.net/u/aminglinux/p/nginx/git/blob/master/access/auth.md 当访问一些私密资源时,最好配置用户认证 ...
- rpm安装PostgreSQL
一.首先去官网下载相关的安装包 https://yum.postgresql.org/rpmchart.php 二.下载安装包 1. 最小的数据库服务器安装包: postgresql96--1PGDG ...
- Android利用Fiddler进行网络数据抓包【怎么跟踪微信请求】
主要介绍Android及IPhone手机上如何利用Fiddler进行网络数据抓包,比如我们想抓某个应用(微博.微信.墨迹天气)的网络通信请求就可以利用这个方法. Mac 下请使用 Charles 代替 ...
- ToDictionary的用法
上次已经查过一次了,这次又忘了...看来有必要记录一下: linq查询结果经常ToList.ToArray什么的,但是ToDictionary用的比较少,导致经常忘记怎么用... ToDictiona ...
- ISE和Modelsim联合仿真(详细步骤讲解)
ISE和Modelsim联合仿真(转) 地址:http://www.cnblogs.com/feitian629/archive/2013/07/13/3188192.html 相信很多人会遇到过这个 ...
- [na]PKI公钥处理思路
前提申明: 在使用任何基于RSA服务之前,一个实体要真实可靠的获取其他实体的公钥. 1,一个可以确认公钥身份的方案:[离线确认] 主:B做同样的事情得到A的公钥. 但是这种方法扩展性差,不可行. 2, ...
- nyoj138 哈希的简单应用(查找)
找球号(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描述 在某一国度里流行着一种游戏.游戏规则为:现有一堆球中,每个球上都有一个整数编号i(0<=i<=1 ...
- Oracle PLSQL Demo - 15.强类型REF游标[预先指定查询类型与返回类型]
declare Type ref_cur_emp IS REF CURSOR RETURN scott.emp%RowType; cur_emp ref_cur_emp; rec_emp cur_em ...
- jQuery图片tab栏切换
<script> $(function(){ $('.tab li').mouseenter(function(){ var $this=$(this); var index=$this. ...