【问题标题】:changing app root for spring mvc app on tomcat在tomcat上更改spring mvc应用程序的应用程序根目录
【发布时间】:2010-09-22 21:14:44
【问题描述】:

我正在使用 Spring MVC 3.0 上的示例 RESTEasy 2.0 资源并部署到 Tomcat 6。我可以通过 http://localhost:8080/examples-resteasy-2.1-SNAPSHOT/contacts 访问我的资源,但我想通过 http://localhost:8080/contacts 甚至 http://localhost:8080/myservice/contacts 访问

我的应用程序映射到路径的方式是否需要更改?

Web.xml

<web-app>

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/contacts/*</url-pattern>
    </servlet-mapping>

</web-app>

springmvc-servlet.xml

<beans xmlns="http: //www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http: //www.springframework.org/schema/context http: //www.springframework.org/schema/context/spring-context-3.0.xsd
        http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans.xsd
    ">
    <context:component-scan base-package="org.jboss.resteasy.examples.springmvc" />
    <context:annotation-config />
    <import resource="classpath:springmvc-resteasy.xml" />  <!-- this is included in the resteasy-spring library-->

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

我的 RESTEasy 资源类

@Controller
@Path("/contacts")
public class ContactsResource {
...

【问题讨论】:

    标签: spring tomcat


    【解决方案1】:

    您可以在 Tomcat server.xml 中设置这些。

    &lt;Host&gt; 中添加一个&lt;Context&gt; 元素,如下所示,将您的examples-resteasy-2.1-SNAPSHOT 设置为默认网络应用程序。

    <Context docBase="examples-resteasy-2.1-SNAPSHOT" path="" reloadable="true" />
    

    这应该允许您以 http://localhost:8080/contacts 访问它

    将路径设置为“myservice”,如下所示

    <Context docBase="examples-resteasy-2.1-SNAPSHOT" path="/myservice" reloadable="true" />
    

    应该允许你以 http://localhost:8080/myservice/contacts 访问它

    【讨论】:

    • 谢谢。我将尝试 server.xml 方式。我唯一担心的是 Tomcat 文档和其他文档强烈反对通过 server.xml 并鼓励 META-INF/context.xml
    • @pastafarian: 没错,但在 6.0 上,我没有通过 META-INF 让它工作,只有 server.xml
    猜你喜欢
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多