Struts2学习笔记---DMI(动态方法调用)


用action的我们可以使用method来调用一个方法,不过还可以用DMI,动态方法调用

Action执行的时候并不一定要执行execute方法
可以在配置文件中配置Action的时候用method=来指定执行哪个方法
也可以在url地址中动态指定(动态方法调用DMI)(推荐)
前者会产生太多的action,所以不推荐使用

<?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>
    <!-- Add packages here -->
	<constant name="struts.devMode" value="true" />
    <package name="user" extends="struts-default" namespace="/user">
        <action name="userAdd" class="org.hualang.actionmethod.ActionMethod" method="add">
            <result>/MyJsp.jsp</result>
        </action>
        
        <action name="user" class="org.hualang.actionmethod.ActionMethod">
        	<result>/MyJsp.jsp</result>
        </action>
    </package>
</struts>

这里有两个action,第一个action用的就是method方式,但是不推荐用,而第二个就是用的DMI动态方法调用

ActionMethod.java(在src目录下的org.hualang.actionmethod包中)

这里没有execute()方法,而是add方法

第一个method="add",说明调用add()方法,只要返回值是String,想调用什么就调用什么
action的名字是userAdd,而对应的result是MyJsp.jsp,所以到这个页面,显示“添加页面”

package org.hualang.actionmethod;

import com.opensymphony.xwork2.ActionSupport;

public class ActionMethod extends ActionSupport {
	public String add()
	{
		return SUCCESS;
	}
}

 
index.jsp(另外还有个MyJsp.jsp就是显示字符串“添加页面”)

<body>
Action执行的时候并不一定要执行execute方法<br />
可以在配置文件中配置Action的时候用method=来指定执行哪个方法<br/>
也可以在url地址中动态指定(动态方法调用DMI)(推荐)<br />
	<a href="<%=context %>/user/userAdd">添加用户</a>
	<br />
	<a href="<%=context %>/user/user!add">添加用户</a>
	<br />
前者会产生太多的action,所以不推荐使用
	
</body>

其他的跟以前的基本一样

运行结果如下:


点击第一个链接
 

 点击第二个链接



 

猜你喜欢

转载自hualang.iteye.com/blog/1030215