8

我正在使用带有 Struts2 的球衣。但是通过 RestFul 服务调用不起作用。以下是我的配置文件

struts.xml

<?xml version="1.0" encoding="UTF-8"?>    
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
        "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>
    <package name="default" namespace="/" extends="struts-default">
    <result-types>
        <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
    </result-types>

     <interceptors>
            <interceptor-stack name="default">
                 <interceptor-ref name="defaultStack">
                    <param name="exception.logEnabled">true</param>
                    <param name="exception.logLevel">ERROR</param>
                    <param name="params.excludeParams">/service/*</param>
                </interceptor-ref>
            </interceptor-stack>
        </interceptors>

        <default-interceptor-ref name="default" />

        <action name="defaultAction"
            class="com.gemini.web.controller.BinMasterController"
            method="binMaster">
            <result name="binMaster" type="tiles">binMaster</result>
        </action>    
    </package>    
</struts>

Web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <!-- Client config -->
    <context-param>
        <param-name>
            org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG
        </param-name>
        <param-value>
            /WEB-INF/tiles.xml
        </param-value>
    </context-param>

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

    <listener>
        <listener-class>
            org.apache.struts2.tiles.StrutsTilesListener
        </listener-class>
    </listener>

    <!-- Jersey Support Configuration for RestFul WebServices -->
    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>
            com.sun.jersey.spi.container.servlet.ServletContainer
        </servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.gemini.rest.controller</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/service/*</url-pattern>
    </servlet-mapping>

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

2 回答 2

10

使用这种配置,Struts2 过滤器拦截所有请求。

要排除您的/service/*请求,请使用struts.action.excludePattern常量:

<constant name="struts.action.excludePattern" value="/service/.*" />
于 2013-06-25T09:08:47.737 回答
0

我有一个类似的架构(使用 Struts2 和 Jersey)。

在我的 web.xml 中,我有:

<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>/admin/*</url-pattern>
</filter-mapping>

我的 Jersey 是通过扩展配置的ResourceConfig

@ApplicationPath("rest")
public class Config extends ResourceConfig {

    public Config() {
        super();
        register(new Binder());
        packages(true, this.getClass().getPackage().getName());
    }

}

所以两者在不同的 url 路径上:

Struts 开启/admin,Jersey 开启/rest

于 2015-08-02T21:30:20.003 回答