gpt4 book ai didi

spring - Spring Mvc项目中如何设置Context root

转载 作者:行者123 更新时间:2023-11-28 21:50:48 25 4
gpt4 key购买 nike

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

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

这是第二次, http://localhost:8080/ControllerWeb.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" />

最佳答案

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 ?

You NEED TO fix the root context for your web application (DemoApplication) 以修复 URL,您可以使用以下选项之一实现:

(1) 按照说明在 Tomcat 中将 root context 设置为 DemoApplication here

(2) 如果您使用的是 Maven,则可以通过将以下标记添加到 pom.xml 中来修复您的 war 名称:

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

完成上述任一选项后,您应该能够始终使用以下 url 访问您的应用程序 Controller :

http://localhost:8080/DemoApplication/Controller

关于spring - Spring Mvc项目中如何设置Context root,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40670654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com