在tomcat上更改spring mvc应用程序的应用程序根目录
问题描述:
我正在使用Spring MVC 3.0上的示例RESTEasy 2.0资源,并且依赖于Tomcat 6.我可以通过http:// localhost:8080/examples-resteasy- 2.1-SNAPSHOT /联系人,但我想通过访问http:// localhost:8080/contacts甚至http:// localhost:8080/myservice/contacts在tomcat上更改spring mvc应用程序的应用程序根目录
有什么我需要改变我的应用程序映射到路径?
的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 {
...
答
你可以在Tomcat server.xml中设置这些。
在下面的<Host>
内添加一个<Context>
元素,它将您的examples-resteasy-2.1-SNAPSHOT
设置为默认的Web应用程序。
<Context docBase="examples-resteasy-2.1-SNAPSHOT" path="" reloadable="true" />
这应该允许您访问它为http://本地主机:8080 /触点
设置,如路径 “为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 2010-09-23 13:38:19
@pastafarian:多数民众赞成真实,但在6.0我没有得到它通过META-INF工作,只有服务器.XML – JoseK 2010-09-24 04:31:22