日期:2014-05-18  浏览次数:20687 次

使用最新struts2(2.3.15.2),不能使用DMI动态调用方法
最新开始学习struts2,在官网上下载的最新的struts2(2.3.15.2), jar包,在使

用动态方法调用的时候老是报错,错误代码如下HTTP Status 404 - There is no 

Action mapped for namespace [/] and action name [book!delBook] associated 

with context path [/Struts2_0900_eg].但是导入struts-2.2.3.1的jar程序就能

正常动态方法调用,最新版的struts2到底怎么实现动态方法调用?代码如下

web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<filter>
<filter-name>struts2</filter-name>
<filter-

class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilt

er</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


struts.xml配置
<?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>
<package name="struts2" extends="struts-default">
<action name="book" 

class="com.softeem.struts.actions.BookAction">
<result type="redirect">/index.jsp</result>
</action>
</package>

</struts>


jsp页面
  
<body>
    <a href="book!addBook.action">添加图书信息</a><br />
    <a href="book!delBook.action">删除图书信息</a><br />
    <a href="book!updateBook.action">修改图书信息</a><br />
    <a href="book!readBook.action">查询图书信息</a><br />
  </body>


Action类
package com.softeem.struts.actions;

import com.opensymphony.xwork2.ActionSupport;

public class BookAction extends ActionSupport {
public String addBook() {

System.out.println("添加图书成功!");
return SUCCESS;
}

public String delBook() {

System.out.println("删除图书成功!");
return SUCCESS;
}

public String updateBook() {

System.out.println("修改图书成功!");
return SUCCESS;
}

public String readBook() {

System.out.println("查询图书成功!");
return SUCCESS;
}
}

------解决方案--------------------
在struts.xml中添加
<constant name="struts.enable.DynamicMethodInvocation" value="true" /> 
打开动态方法调用。
动态方法调用官方推荐的做法是,使用通配符的形式。不要使用actionName!methodName
的方式。