对于DMI的理解我觉得还是看一些代码了解的快和记忆深刻,所以,我就从网上找了一段代码
关于Action的方法调用:
1.Action执行的时候不一定要执行execute方法;
2.动态方法调用(DMI)的两种方式:
a.通过配置文件struts.xml中action的method属性指定调用方法,如:
<action method="调用方法名">;
b.在URL中动态指定调用方法,使用感叹号“!”将action名和调用方法名分割开,如:
http://localhost:8080/Struts2_DMI/user!add
3.第一种方法需要配置很多action,一般使用第二种方法。
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>hello.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
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.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <include file="example.xml"/> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <action name="index"> <result type="redirectAction"> <param name="actionName">HelloWorld</param> <param name="namespace">/example</param> </result> </action> </package> --> <!-- Add packages here --> <constant name="struts.devMode" value="true" /> <package name="user" namespace="/" extends="struts-default"> <action name="userAdd" class="cn.edu.ahau.mgc.struts2.action.UserAction" method="add"> <result name="addSuccess">/addSuccess.jsp</result> </action> <action name="userDelete" class="cn.edu.ahau.mgc.struts2.action.UserAction" method="delete"> <result name="deleteSuccess">/deleteSuccess.jsp</result> </action> <action name="user" class="cn.edu.ahau.mgc.struts2.action.UserAction"> <result name="addSuccess">/addSuccess.jsp</result> <result name="deleteSuccess">/deleteSuccess.jsp</result> </action> </package> </struts>
UserAction.java:
package cn.edu.ahau.mgc.struts2.action; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { public String add() { return "addSuccess"; } public String delete() { return "deleteSuccess"; } }
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <a href="userAdd">UserAdd</a> <a href="userDelete">UserDelete</a> <br /> <br /> <a href="user!add">user!add</a> <a href="user!delete">user!delete</a> </body> </html>
addSuccess.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>AddSuccess</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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> User Add Success! <br> </body> </html>
deleteSuccess.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>DeleteSuccess</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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> User Delete Success! <br> </body> </html>
相关推荐
Struts2_动态方法调用,使用struts2架构的动态方法进行资源调用
NULL 博文链接:https://cdxs2.iteye.com/blog/1925232
NULL 博文链接:https://takeme.iteye.com/blog/1631525
Struts2动态方法调用 包括 1.js方法调用 2.普通的表单提交调用 3.通配符
struts2中动态方法的调用,如何实现动态方法调用
struts 1框架的动态调用也许你会,但是struts2 的动态调用更经典,这个文档让你玩转struts 2 动态调用.......
本文档主要介绍了struts2中API耦合的三种方法以及动态方法调用(Dynamic Method Invoked)的两种方法,希望对你有所帮助,谢谢!
Struts2中直接和间接访问ServletAPI以及动态方法调用之感叹号
利用Struts 2框架创建一个web项目chap2_e22,实现用户登录过程。具体要求是在loginAction类中分别用login()和registered()处理用户登录和注册的过程,分别创建login.jsp和register.jsp两个页面实现登录和注册的...
struts2动态调用之通配符,相当于是改进的method动态调用方法,减少对同一action不同method的多次配置
struts动态访问调用之一,采用method属性,同一个Action内的不同方法来响应用户请求
struts2利用通配符调用同一个Action里面不同的方法,在struts.xml配置文件中将请求方法的不相同部分抽象成“*".
主要介绍了详解Struts2动态方法调用,涉及调用方法的代码,具有一定参考价值,需要的朋友可以了解下。
struts2实验一:动态方法调用+请求参数的获取+简单的表单数据验证
详细讲解struts2中单个action中多个处理逻辑的配置方法, 以及默认Action的配置.
Struts2调用EJB异常项目包,出现的问题以及解决方案参见EJBStruts.zip中的说明.txt 以及博客 http://blog.csdn.net/icecream0/article/details/7167853
本文主要介绍Struts2的DMI动态调用的两种方式,简单实用,希望能给大家做一个参考。
struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别...
JS调用Struts中的ActionJS调用Struts中的ActionJS调用Struts中的Action
struts2中 页面加载时动态填充下拉框的例子,用于向数据库请求数据进行填充。通过Action中的方法进行请求数据,来初始化Action中的属性,以便页面上的使用。包含jar包。