struts2-rest plugin..making两个struts动作+休息动作一起工作但是。给出java.lang.reflect.InvocationTargetException

时间:2013-07-17 10:33:00

标签: rest struts struts2-convention-plugin

我正在将我现有的struts 2应用程序转换为通过一些基于休息的服务来提供服务。

我使用了两个插件,struts2-rest插件和struts-convention插件,

除了这些之外,我还使用了asm.jar,因为上面给出了一个未找到类的异常,它位于asm jar中。

我希望这两种功能都可以作为..my普通struts动作映射也应该与其他网址一起使用。

struts.xml中

<?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>
  <!--  Overwrite Convention -->

<constant name="struts.convention.action.mapAllMatches" value="true"/>    
<constant name="struts.convention.action.suffix" value="Controller"/>
<constant name="struts.convention.default.parent.package" value="rest-default"/>
<constant                name="struts.mapper.class"value="org.apache.struts2.dispatcher.mapper.PrefixBasedActionMapper" />
<constant name="struts.mapper.prefixMapping" value="/rest:rest,:struts" />

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

<package name="secureAction" extends="struts-default,json-default"
    namespace="/secure">
    <interceptors>

        ...............

    </interceptors>

    <default-interceptor-ref name="secureStack" />
    <global-results>
        .............
    </global-results>
    <global-exception-mappings>
        ..............
</global-exception-mappings>

</package>

<include file=".....xml" />
</struts>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
.........
 <display-name>strutsTest</display-name>
 <filter>
 <filter-name>struts2</filter-name>
 <filter-class>
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
 </filter>

  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <welcome-file-list>
       ....
  </welcome-file-list>
 <servlet>
 <description></description>
 <display-name>Logger</display-name>
 <servlet-name>Logger</servlet-name>
 <servlet-class>...........</servlet-class>
 <init-param>
  <param-name>log4j-properties-location</param-name>
  <param-value>WEB-INF/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
<servlet-name>Logger</servlet-name>
<url-pattern>/Logger</url-pattern>
  </servlet-mapping>

我无法找到任何错误..

当我运行基于struts动作的映射或休息网址时,我在屏幕上看到的内容..:

Struts has detected an unhandled exception:
Messages:   

com.thoughtworks.xstream.XStream
com/thoughtworks/xstream/XStream
java.lang.reflect.InvocationTargetException
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
java.lang.RuntimeException: java.lang.RuntimeException:               java.lang.reflect.InvocationTargetException
java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

File:   org/apache/catalina/loader/WebappClassLoader.java
Line number:    1,516

1 个答案:

答案 0 :(得分:3)

我用customMapper解决了这个问题..

这是我的自定义映射器类....

   package org.apache.struts2.rest.example;

    import javax.servlet.http.HttpServletRequest;
    import org.apache.struts2.dispatcher.mapper.ActionMapper;
    import org.apache.struts2.dispatcher.mapper.ActionMapping;
    import org.apache.struts2.dispatcher.mapper.DefaultActionMapper;
    import com.opensymphony.xwork2.config.ConfigurationManager;

   public class CustomActionMapper extends DefaultActionMapper {


    public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) {

        return getActionMapper(request, configManager).getMapping(request, configManager);

    }

    private ActionMapper getActionMapper(HttpServletRequest request, ConfigurationManager configManager) {

        ActionMapping actionMapping = new ActionMapping();  

        parseNameAndNamespace(request.getRequestURI(), actionMapping, configManager);

        if (!(actionMapping.getNamespace()).contains("/rest")) {

            return container.getInstance(ActionMapper.class, "struts");

        } 
        else 
        {
            return container.getInstance(ActionMapper.class, "rest");


}
}

........................................... 我的struts.xml如下......

<?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>
<!--  Overwrite Convention -->

    <bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="myActionMapper" class="org.apache.struts2.rest.example.CustomActionMapper" />
    <constant name="struts.mapper.class" value="myActionMapper" />
    <constant name="struts.convention.action.suffix" value="Controller"/>
    <constant name="struts.convention.action.mapAllMatches" value="true"/>
    <constant name="struts.convention.default.parent.package" value="rest-default"/>
    <constant name="struts.convention.package.locators" value="example"/> 
    <constant name="struts.action.extension" value="xhtml,,xml,json,action"/>      
<!-- overwrite action complete -->


<!-- normal struts actions -->

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

<action name="*">
        <result name="error">/view/error/error.jsp</result>
    </action>
    ..................

</package>




<include file="..........xml" />
<include file=".............xml" /> 


<!-- normal stryts actions complete -->


</struts>

这些是我正在使用的库..

- asm-3.1.jar
- asm-commons-3.1.jar
- commons-beanutils-1.7.0.jar
- commons-collections-3.1.jar
- commons-fileupload-1.2.2.jar
- commons-io-2.0.1.jar
- commons-lang-2.5.jar
- commons-logging-1.1.1.jar
- commons-logging-api-1.1.jar
- ezmorph-1.0.3.jar
- freemarker-2.3.16.jar
- javassist-3.11.0.GA.jar
- json-lib-2.1-jdk15.jar
- log4j-1.2.8.jar
- ognl-3.0.1.jar
- struts2-convention-plugin-2.2.3.jar
- struts2-core-2.2.3.jar
- struts2-json-plugin-2.2.3.jar
- struts2-rest-plugin-2.2.3.jar
- xpp3_min-1.1.3.4.O.jar
- xstream-1.2.2.jar
- xwork-core-2.2.3.jar

确保使用兼容的库版本....