Struts2中的动态方法调用(DMI)

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:

01.<?xml version="1.0" encoding="UTF-8"?>
02.<web-app version="2.5"
03.xmlns="http://java.sun.com/xml/ns/javaee"
04.xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
05.xsi:schemaLocation="http://java.sun.com/xml/ns/javaee ;
06.http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
07.<welcome-file-list>
08.<welcome-file>hello.jsp</welcome-file>
09.</welcome-file-list>
10.<filter>
11.<filter-name>struts2</filter-name>
12.<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
13.</filter>
14.<filter-mapping>
15.<filter-name>struts2</filter-name>
16.<url-pattern>/*</url-pattern>
17.</filter-mapping>
18.</web-app>


struts.xml:
01.<?xml version="1.0" encoding="UTF-8" ?>
02.<!DOCTYPE struts PUBLIC
03."-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
04."http://struts.apache.org/dtds/struts-2.0.dtd">
05.
06.<struts>
07.<!--
08.<constant name="struts.enable.DynamicMethodInvocation" value="false" />
09.<constant name="struts.devMode" value="false" />
10.
11.<include file="example.xml"/>
12.
13.
14.
15.<package name="default" namespace="/" extends="struts-default">
16.<default-action-ref name="index" />
17.<action name="index">
18.<result type="redirectAction">
19.<param name="actionName">HelloWorld</param>
20.<param name="namespace">/example</param>
21.</result>
22.</action>
23.</package>
24.-->
25.
26.<!-- Add packages here -->
27.<constant name="struts.devMode" value="true" />
28.<package name="user" namespace="/" extends="struts-default">
29.<action name="userAdd" class="cn.edu.ahau.mgc.struts2.action.UserAction" method="add">
30.<result name="addSuccess">/addSuccess.jsp</result>
31.</action>
32.<action name="userDelete" class="cn.edu.ahau.mgc.struts2.action.UserAction" method="delete">
33.<result name="deleteSuccess">/deleteSuccess.jsp</result>
34.</action>
35.
36.<action name="user" class="cn.edu.ahau.mgc.struts2.action.UserAction">
37.<result name="addSuccess">/addSuccess.jsp</result>
38.<result name="deleteSuccess">/deleteSuccess.jsp</result>
39.</action>
40.</package>
41.</struts>


UserAction.java:
01.package cn.edu.ahau.mgc.struts2.action;
02.
03.import com.opensymphony.xwork2.ActionSupport;
04.
05.public class UserAction extends ActionSupport {
06.
07.public String add() {
08.return "addSuccess";
09.}
10.
11.public String delete() {
12.return "deleteSuccess";
13.}
14.}


index.jsp:
01.<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
02.<%
03.String path = request.getContextPath();
04.String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
05.%>
06.
07.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
08.<html>
09.<head>
10.<base href="<%=basePath%>">
11.
12.<title>DMI</title>
13.<meta http-equiv="pragma" content="no-cache">
14.<meta http-equiv="cache-control" content="no-cache">
15.<meta http-equiv="expires" content="0">   
16.<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17.<meta http-equiv="description" content="This is my page">
18.<!--
19.<link rel="stylesheet" type="text/css" href="styles.css">
20.-->
21.</head>
22.
23.<body>
24.<a href="userAdd">UserAdd</a>
25.<a href="userDelete">UserDelete</a>
26.<br />
27.<br />
28.
29.<a href="user!add">user!add</a>
30.<a href="user!delete">user!delete</a>
31.</body>
32.</html>


addSuccess.jsp:
01.<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
02.<%
03.String path = request.getContextPath();
04.String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
05.%>
06.
07.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
08.<html>
09.<head>
10.<base href="<%=basePath%>">
11.
12.<title>AddSuccess</title>
13.<meta http-equiv="pragma" content="no-cache">
14.<meta http-equiv="cache-control" content="no-cache">
15.<meta http-equiv="expires" content="0">   
16.<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17.<meta http-equiv="description" content="This is my page">
18.<!--
19.<link rel="stylesheet" type="text/css" href="styles.css">
20.-->
21.</head>
22.
23.<body>
24.User Add Success! <br>
25.</body>
26.</html>


deleteSuccess.jsp:
view source
< id="highlighter_598242_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://mgc.name/common/SyntaxHighlighter/scripts/clipboard.swf" menu="false" flashvars="highlighterId=highlighter_598242" wmode="transparent" allowscriptaccess="always">
print?
01.<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
02.<%
03.String path = request.getContextPath();
04.String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
05.%>
06.
07.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
08.<html>
09.<head>
10.<base href="<%=basePath%>">
11.
12.<title>DeleteSuccess</title>
13.<meta http-equiv="pragma" content="no-cache">
14.<meta http-equiv="cache-control" content="no-cache">
15.<meta http-equiv="expires" content="0">   
16.<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17.<meta http-equiv="description" content="This is my page">
18.<!--
19.<link rel="stylesheet" type="text/css" href="styles.css">
20.-->
21.</head>
22.
23.<body>
24.User Delete Success! <br>
25.</body>
26.</html

你可能感兴趣的:(apache,html,jsp,xml,struts)