struts2动态方法调用(DMI)

本文介绍了Struts2框架中动态方法调用(DMI)的使用方式,包括如何在配置文件中启用DMI,以及如何通过URL动态指定Action执行的方法,对比了直接配置方法与DMI的不同。

(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>

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值