Struts2的动态方法调用

Struts2的动态方法调用


在struts2中,可以通过两种方式调用Action类的除了execute之外的其他方法。而这种方法是struts框架直接支持的。


方式1:struts.xml中的action的属性为method


  <?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" />

    <package name="default" namespace="/" extends="struts-default">

        <default-action-ref name="index" />

        <global-results>
            <result name="error">/error.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>

        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package>

    <include file="example.xml"/>
 	-->
    <!-- Add packages here -->
	<!-- 
	<package name="default" namespace="/" extends="struts-default">
		<action name="helloworld">
			<result>/helloworld.jsp</result>
		</action>
	</package>
	-->
	<!--  使用Action 的三种方式,在企业最常用的是继承ActionSupport类 -->
	<package name="default" namespace="/action" extends="struts-default">
		<action name="test01" class="com.demo.action.DemoAction01">
			<result name="success">/action01.jsp</result>
		</action>
		<action name="test02" class="com.demo.action.DemoAction02">
			<result name="success">/action02.jsp</result>
		</action>
		<action name="test03" class="com.demo.action.DemoAction03">
			<result name="success">/action03.jsp</result>
		</action>
	</package>
	
	
	<!--  注意:在struts配置中 package包的name属性名称不能相同 -->
	<package name="dmi_pack" namespace="/dmi" extends="struts-default">
		<action name="add" class="com.demo.action.DMIActionDemo" method="add">
			<result name="add">/add.jsp</result>
		</action> 
		
		<action name="edit" class="com.demo.action.DMIActionDemo" method="edit">
			<result>/edit.jsp</result>
		</action>
		<action name="delete" class="com.demo.action.DMIActionDemo" method="delete">
			<result>/delete.jsp</result>
		</action>
		<action name="query" class="com.demo.action.DMIActionDemo" method="query">
			<result>/query.jsp</result>
		</action>
	</package>
	
	<!-- 动态方法调用 -->
	<package name="dmi_method" namespace="/dmi_method" extends="struts-default">
		<action name="dmiMethod" class="com.demo.action.DMIAction" >
			<result name="success">/result.jsp</result>
		</action>  
	</package>
	 
	
</struts>

 

 

   package com.demo.action;

import com.opensymphony.xwork2.ActionSupport;

public class DMIActionDemo extends ActionSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public String add() {
		
		System.out.println("===========添加操作=========");
		System.out.println("===========添加数据=========");
		return "add";
	}
	
	public String edit() {
		System.out.println("===========编辑操作=========");
		System.out.println("===========编辑数据=========");
		return SUCCESS;
	}
	
	public String delete() {
		System.out.println("===========删除操作=========");
		System.out.println("===========删除数据=========");
		return SUCCESS;
	}
	
	public String query() {
		System.out.println("===========查询操作=========");
		System.out.println("===========查询数据=========");
		return SUCCESS;
	}
}
 


方式2:动态调用方法(DMI)

不需要进行struts的配置,直接在视图页面中通过标识符号制定了要调用的方法。标识符号为"!"号。

注意:/名称空间/action名称!操作方法.action

    <a href="/dmi_method/dmiMethod!add.action">添加操作</a>

    <a href="/dmi_method/dmiMethod!edit.action">修改操作</a>

    <a href="/dmi_method/dmiMethod!delete.action">删除操作</a>

    <a href="/dmi_method/dmiMethod!query.action">查询操作</a>

 

     <!-- 动态方法调用 -->

	<package name="dmi_method" namespace="/dmi_method" extends="struts-default">
		<action name="dmiMethod" class="com.demo.action.DMIAction" >
			<result name="success">/result.jsp</result>
		</action>  
	</package>
	
	
 

 

package com.demo.action;

import com.opensymphony.xwork2.ActionSupport;

public class DMIAction extends ActionSupport{
	
	public String add() {
		System.out.println("=======DMI 添加操作");
		return SUCCESS;
	}
	
	public String edit() {
		System.out.println("=======DMI 编辑操作");
		return SUCCESS;
	}
	
	public String delete() {
		System.out.println("=======DMI 删除操作");
		return SUCCESS;
	}
	
	public String query() {
		System.out.println("=======DMI 查询操作");
		return SUCCESS;
	}
}
 

你可能感兴趣的:(struts2)