(1)Action执行的时候并不一定要执行execute方法
(2)可以在配置文件中配置action的时候用method=来指定执行哪个方法,也可以在url中动态指定(动态方法调用DMI)(推荐使用)
(3)第一种会产生太多的action,所以不推荐使用。
(4)如果使用DMI的方式,在struts.xml配置文件中需要设置:
<constantname="struts.enable.DynamicMethodInvocation" value="true"/>
(5)DMI:DynamicMethodInvocation动态方法调用
struts.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="user" namespace="/user" extends="struts-default">
<!--这种方法不推荐使用,会产生太多的action -->
<action name="userAdd" class="com.dqpi.eonline.UserAction" method="add">
<result>
/user_add_success.jsp
</result>
</action>
<!--使用了DMI动态的方法调用(Dynamic method invocation) -->
<action name="user" class="com.dqpi.eonline.UserAction">
<result>
/user_add_success.jsp
</result>
</action>
</package>
</struts>
</span>
UserAction.java
<span style="font-size:18px;">package com.dqpi.eonline;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private static final long serialVersionUID = -1437555295816924828L;
public String add(){
return "success";
}
}
</span>
index.jsp
<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>
<!--此处使用了myeclipse中经常使用的basepath -->
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</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="user/userAdd">添加用户</a>
<a href="user/user!add">添加用户DMI</a>
</body>
</html>
</span>
user_add_success.jsp
<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!-- 此处引进了struts的标签库 -->
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'path.jsp' starting page</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>
</span>