如何在Spring Mvc项目中设置Context根路径

5
我正在使用Tomcat服务器中的Spring MVC项目。每次运行应用程序时,服务器上下文根会更改。如何设置固定的上下文根?
我的项目名称是:DemoApplication, 第一次部署此上下文根的路径为, http://localhost:8080/DemoApplication 第二次则为, http://localhost:8080/Controller Web.xml:
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

servlet-context.xml:

<annotation-driven />
<resources mapping="/resources/**" location="/resources/" />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <beans:property name="url"
        value="jdbc:mysql://localhost:3306/mydp" />
    <beans:property name="username" value="root" />
    <beans:property name="password" value="root" />
</beans:bean>
<beans:bean id="hibernate4AnnotatedSessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <beans:property name="dataSource" ref="dataSource" />
    <beans:property name="annotatedClasses">
        <beans:list>
             <beans:value>com.Aquatech.Model.Area</beans:value>
        </beans:list>
    </beans:property>
    <beans:property name="hibernateProperties">
        <beans:props>
            <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
            </beans:prop>
            <beans:prop key="hibernate.show_sql">false</beans:prop>
        </beans:props>
    </beans:property>
</beans:bean> 
<beans:bean id="GenericDao" class="com.Aquatech.Dao.Impl.GenericDaoImpl">
 <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory"></beans:property>
</beans:bean>
<beans:bean id="GenericserviceDao" class="com.Aquatech.ServiceDao.Impl.GenericServiceDaoImpl">
 <beans:property name="genericDao" ref="GenericDao"></beans:property>
</beans:bean>
<interceptors>
    <beans:bean id="webContentInterceptor"      class="org.springframework.web.servlet.mvc.WebContentInterceptor">
        <beans:property name="cacheSeconds" value="0" />
        <beans:property name="useExpiresHeader" value="true" />
        <beans:property name="useCacheControlHeader" value="true" />
        <beans:property name="useCacheControlNoStore" value="true" />
    </beans:bean>
</interceptors>
<context:component-scan base-package="com.Aquatech.Controller" />


1
请提供您的web.xml和Spring MVC的应用程序上下文xml。 - ScanQR
可能是一个重复的问题,与 如何在 Tomcat 7.0 中设置 Web 应用程序的上下文路径 相关。 - developer
@vila,你的web.xml看起来很好。对于Tomcat而言,默认的上下文根是war名称。 - ScanQR
2个回答

6
我的项目名称是: DemoApplication,第一次部署时上下文根路径为http://localhost:8080/DemoApplication,第二次则为http://localhost:8080/Controller。您需要修复您的Web应用程序的根上下文以修复URL,您可以通过以下选项之一实现:
(1) 在Tomcat中将根上下文设置为DemoApplication,如这里所述。
(2) 如果您使用Maven,则可以通过向pom.xml添加以下标记来修复您的war名称:
<build>
    <finalName>DemoApplication</finalName>
</build>

完成以上任一选项后,您应该能够始终通过以下URL访问您的应用程序控制器

http://localhost:8080/DemoApplication/Controller


0

如果你正在使用IntelliJ IDE,你可以进入右上角的服务器(编辑配置>选择服务器(例如:Tomcat 8.5.61)>部署)。 有一个字段“应用程序上下文:/(基本URL服务器名称)”,在这里你可以放任何你想要保留的内容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接