如何在Spring Mvc项目中设置上下文根 [英] How to Set Context root in Spring Mvc Project

查看:175
本文介绍了如何在Spring Mvc项目中设置上下文根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Tomcat Server中使用Spring MVC项目.每次运行该应用程序时,服务器上下文根都已更改. 如何设置固定上下文根?

I am Using Spring MVC project in Tomcat Server.Each time I run the application the server context root is changed. How to set fixed context root?

我的项目名称是:DemoApplication, 首先部署此上下文根,路径为 http://localhost:8080/DemoApplication

My projectname is: DemoApplication, first this context root is deployed the path is, http://localhost:8080/DemoApplication

第二次, http://localhost:8080/Controller Web.xml:

second time it is, 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:

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

推荐答案

我的项目名称是:DemoApplication,首先此上下文根是 部署的路径是 http://localhost:8080/DemoApplication 和第二个 现在是 http://localhost:8080/Controller 吗?

My projectname is: DemoApplication, first this context root is deployed the path is, http://localhost:8080/DemoApplication and second time it is, http://localhost:8080/Controller ?

您需要为Web应用程序修复根上下文 (DemoApplication)以修复URL,您可以使用以下选项之一来实现:

You NEED TO fix the root context for your web application (DemoApplication) to fix the URL, which you can achieve with one of the below options:

(1)按照(2)如果您使用的是Maven,则可以通过将以下标签添加到pom.xml中来修正您的战争名称:

(2) If you are using Maven, you can fix your war name by adding the below tag into the pom.xml:

<build>
    <finalName>DemoApplication</finalName>
</build>

完成以上任一选项后,您应该始终可以使用以下url来访问应用程序 controller :

Once you are done with either of the above options, you should be able to access your application controller always with below url :

http://localhost:8080/DemoApplication/Controller

这篇关于如何在Spring Mvc项目中设置上下文根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆