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. table头部、尾部固定;中间内容定高自适应滚动

    table头部.尾部固定;中间内容定高自适应滚动 很多时候,需要使用到表格做数据分析,不管是前端展现,还是后台管理系统节点展现 工作过程中遇到了,作为一个小笔记,备忘! 如下图所示 --------- ...

  2. Java学习的随笔(2)Java语言的三大特性

    1.面向对象的三大特性 面向对象的三大特性主要包括:继承.封装.多态 (1)继承:就是指子类(导出类)获得了基类的全部功能(所有的域和方法).  注:在子类中,想要调用基类的方法可以使用“super. ...

  3. xcode设置项目图标玻璃镜效果

    xcode5中设置 ios6和ios7的适配一些小细节注意,ios6中图标会默认的设置玻璃镜效果 找到图片文件夹APPlcon中右侧设置中的有个iOS icon is pre-rend-rendere ...

  4. 设置IIS7文件上传的最大大小 maxAllowedContentLength,maxRequestLength

    当上传一个超过30M的文件时,服务器会重定向至404.13页面,报错如下: HTTP Error 404.13 - Not Found The request filtering module is ...

  5. CreateFont具体解释

    CFont * f;    f = new CFont;    f->CreateFont(10, // nHeight         0, // nWidth         0, // n ...

  6. android中Canvas使用drawBitmap绘制图片

    1.主要的绘制图片方法 //Bitmap:图片对象,left:偏移左边的位置,top: 偏移顶部的位置     drawBitmap(Bitmap bitmap, float left, float ...

  7. 关于Xcode的Other Linker Flags

    背景 在ios开发过程中,有时候会用到第三方的静态库(.a文件),然后导入后发现编译正常但运行时会出现selector not recognized的错误,从而导致app闪退.接着仔细阅读库文件的说明 ...

  8. C#自定义事件:属性改变引发事件示例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  9. Creating a Navigation Drawer 创建一个导航侧边栏

    The navigation drawer is a panel that displays the app’s main navigation options on the left edge of ...

  10. SpringMVC11文件上传

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...