<Host name="localhost" appBase="aaa"java
unpackWARs="true" autoDeploy="true">web
<Context docBase="/home/tomcat_7.0.92/webapps/name" path="" reloadable="false"/>spring
<Context docBase="/home/tomcat_7.0.92/webapps/name" path="/name" reloadable="true"/>tomcat
</Host>mvc
問題:項目會加載兩次app
一、修改tomcat的server.xml文件webapp
<Host name="localhost" appBase="aaa"url
unpackWARs="true" autoDeploy="true">spa
<Context docBase="/home/tomcat_7.0.92/webapps/name" path="" reloadable="false"/>code
</Host>
做用:使全部的請求都轉發到name項目中
二、重寫UrlPathHelper的getLookupPathForRequest方法
package com.hundsun.itn.util.spring; import javax.servlet.http.HttpServletRequest; import org.springframework.web.util.UrlPathHelper; public class MyUrlPathHelper extends UrlPathHelper { public String getLookupPathForRequest(HttpServletRequest request) { String url = super.getLookupPathForRequest(request); if (!url.startsWith("/name")) { //name改成你本身的項目名 return url; } return url.replaceFirst("/name", ""); //name改成你本身的項目名 } }
該方法是spring mvc獲取request請求中url的方法,若是url中有項目名稱,則將項目名稱(name)去掉
三、修改spring mvc配置文件
<bean id="myUrlPathHelper" class="com.hundsun.itn.util.spring.MyUrlPathHelper"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="urlPathHelper" ref="myUrlPathHelper" />
</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!-- <mvc:annotation-driven /> -->
做用:配置RequestMappingHandlerMapping使用自定義的myUrlPathHelper
注意:若是以前使用的是<mvc:annotation-driven />,要將其去掉,手動配置RequestMappingHandlerMapping和RequestMappingHandlerAdapter