Struts2的Action类中可以放置多个方法并在struts.xml中配置供页面调用。只要方法符合execute()方法的标准即返回类型是String就可以了。
同一个Action多个方法调用的方式中有三种方法
1.在<action>标签中有一个method的属性可以配置自定义的方法。
2.使用DMI方法。
3.使用通配符
今天我们着重介绍第两种方法的使用:
第一种方法的弊端是你在Action类中定义多少方法就需要使用多少个<action></action>标签,这样会导致struts.xml文件越来越大。
第二种方法的使用:
Action类的:

 package test.struts2.DMI;  

 import com.opensymphony.xwork2.ActionSupport;  

 public class TestDMI extends ActionSupport {
private String message; public String getMessage() {
return message;
} public String searchUsers()throws Exception{
message = "你调用的是searchUsers()查询方法!";
return SUCCESS;
} public String addUser()throws Exception{
message = "你调用的是addUser()添加方法!";
return SUCCESS;
} }

和一起写法一样
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.i18n.encoding" value="GBK"></constant>
<package name="struts2" extends="struts-default" namespace="/">
<action name="user" class="test.struts2.DMI.TestDMI">
<result>result.jsp</result>
</action>
<!--下面两个是第一种方法的写法,如果使用DMI下面两个action就不要了-->
<action name="userAdd" class="test.struts2.DMI.TestDMI" method="addUser">
<result>result.jsp</result>
</action>
<action name="userSearch" class="test.struts2.DMI.TestDMI" method="searchUsers">
<result>result.jsp</result>
</action>
</package>
</struts>

页面调用的写法

 <%@ page language="java" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
%> <html>
<head> <title>Struts DMI</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head> <body>
<a href="userAdd">通过普通调用addUser()方法</a>
<br/>
<a href="userSearch">通过普通调用searchUsers()方法</a>
<hr>
<a href="user!addUser">通过DMI方式调用addUser()方法</a>
<br/>
<a href="user!searchUsers">通过DMI方式调用searchUsers()方法</a>
</body>
</html>

注意:大家看我的超链接中userAdd并没有加扩展名.action因为Struts2默认就会加上.action,我说的是在你没有更改默认扩展名的情况下可以不加扩展名。如果你把扩展名修改了就必须加上你修改过的扩展名如:.do;加了扩展名的页面如下:

 <%@ page language="java" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
%> <html>
<head> <title>Struts DMI</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head> <body>
<a href="userAdd.action">通过普通调用addUser()方法</a>
<br/>
<a href="userSearch.action">通过普通调用searchUsers()方法</a>
<hr>
<a href="user!addUser.action">通过DMI方式调用addUser()方法</a>
<br/>
<a href="user!searchUsers.action">通过DMI方式调用searchUsers()方法</a>
</body>
</html>

如果想在链接后面加参数的话写法如:href="user!addUser.action?userName=张三"

第三种使用通配符弊端是struts.xml中比较混乱,代码可读性比较差。

Struts2 DMI的使用的更多相关文章

  1. struts2 DMI

    在使用DMI(动态方法调用)的时候要注意struts.xml配置时要把 <constant name="struts.enable.DynamicMethodInvocation&qu ...

  2. struts2 DMI问题

    最新开始学习struts2,在官网上下载的最新的struts2(2.3.15.2), jar包,在使用动态方法调用的时候老是报错,错误代码如下HTTP Status 404 - There is no ...

  3. struts2 s2-032漏洞分析

    0x01Brief Description 最近面试几家公司,很多都问到了s2漏洞的原理,之前调试分析过java反序列化的漏洞,觉得s2漏洞应该不会太难,今天就分析了一下,然后发现其实漏洞的原理不难, ...

  4. ref:Struts2 命令执行系列回顾

    ref:http://www.zerokeeper.com/vul-analysis/struts2-command-execution-series-review.html Struts2 命令执行 ...

  5. struts2 CVE-2012-0392 S2-008 Strict DMI does not work correctly allows remote command execution and arbitrary file overwrite

    catalog . Description . Effected Scope . Exploit Analysis . Principle Of Vulnerability . Patch Fix 1 ...

  6. Struts2的DMI跟SMI

    我使用的Struts2的版本是2.5.2,今天在使用Struts2的DMI(动态方法调用)的时候出现了一个有趣的问题,我先把我的配置及代码展示一下: web.xml <filter> &l ...

  7. struts2的动态方法调用(DMI)和通配符映射

    动态方法调用   1.Struts2默认关闭DMI功能,需要使用需要手动打开,配置常量 struts.enable.DynamicMethodInvocation = true 2.使用“!”方法,即 ...

  8. struts2 中使用DMI(动态调用方法)方式配置action

    1.使用 "!"方式,即 action名称 ! 方法名称 struts.xml 配置 <package name="user" namespace=&qu ...

  9. Struts2动态方法调用(DMI)

    当structs.xml解析到Action的时候,默认执行的是此action的execute()方法,但是实际开发中,我们的action中含有很多方法,比如说增删改查的方法,那么structs.xml ...

随机推荐

  1. Linux用户与用户组的详解

    1,用户和用户组文件   在linux中,用户帐号,用户密码,用户组信息和用户组密码均是存放在不同的配置文件中的.   在linux系统中,所创建的用户帐号和其相关信息(密码除外)均是存放在/etc/ ...

  2. Monkey log分析说明

    运行命令: adb shell monkey -p com.crazyhornets.MyHokageAndroidZSY -v -v -v 20 -- throttle 1000 Log: :Mon ...

  3. Servlet跳转

    方便自己查询,嫌低级的勿喷.... 在Servlet中跳转有两种: 1.客户端跳转 在Servlet中要进行客户端跳转(地址栏的地址信息将发生改变),直接使用HttpServletResponse接口 ...

  4. 缓存的概念(反向代理、CDN)

    缓存在我们的日常开发中随处可见,理解缓存的概念,本质就是就近处理,比如很多热点数据,访问量很多,我们需要使用,就可以把它 缓存起来,然后下次访问就不用再去数据库的去查询了,而是直接使用缓存,现在说说大 ...

  5. [置顶] 步步辨析JS中的对象成员

    前提 首先我们应该明白创建一个JS对象的具体实例是实例化的过程,而实例化是通过new关键字实现的,这个对象是含有constructor的,一般的核心对象都会具有constructor以便创建其实例.因 ...

  6. [Unity-24] Unity的四种载入场景的方法

    Unity官方提供了4种载入场景(scene)的方法.各自是: 1. Application.LoadLevel():同步载入 2. Application.LoadLevelAsync():异步载入 ...

  7. PHP Ctype函数(转)

    Ctype函数是PHP内置的字符串体测函数.主要有以下几种 ctype_alnum -- Check for alphanumeric character(s)检测是否是只包含[A-Za-z0-9] ...

  8. Meth | phpstorm 2016.2 的最新破解方法(截止2016-8-1)

    今天刚更新了phpstorm 2016.2版本,发现网上提供的破解地址都有问题,即*.lanyus.com及*.qinxi1992.cn下的全部授权服务器已遭JetBrains封杀. 最后网上找到一个 ...

  9. python一些技巧

    1. 使用目录管理 sys.path.append(sys.argv[0][:sys.argv[0].rfind('com'+os.sep+'abc')]) from com.abc.libs imp ...

  10. [转] 设计模式 -- Facade

    PS:Façade模式注重简化接口,Adapter模式注重转换接口,Bridge模式注重分离接口(抽象)与其实现,Decorator模式注重稳定接口的前提下为对象扩展功能. Facade模式 Faca ...