在本地部署到Tomcat的根(/)Context

时间:2016-08-16 23:37:05

标签: java eclipse spring spring-mvc tomcat

我有一个非常简单的Spring MVC应用程序,我在其中配置了调度程序servlet以接受/ pattern下的请求,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
          version="3.0">

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

我还有一个简单的Controller,它接受/ *:

下的所有请求
    @RequestMapping("/*")
    public ModelAndView index(HttpSession session) {
        System.out.println(session.getId());
        // do Smth heavy
        return new ModelAndView("index");
    }

现在我想在Tomcat中部署我的Web应用程序并将web上下文设置为/,这样当命中localhost时:8080我的索引页面会弹出。 这确实工作正常,但问题是在启动期间调用方法并且spring记录相同但没有任何浏览器或其他东西来请求该页面。

为了从Eclipse更改Web应用程序的上下文路径,我使用模块选项卡中的上下文描述符。

Modules Tab

DEBUG DispatcherServlet:823 - DispatcherServlet with name 'dispatcher' processing GET request for [/]
DEBUG RequestMappingHandlerMapping:220 - Looking up handler method for path /
DEBUG RequestMappingHandlerMapping:227 - Returning handler method [public org.springframework.web.servlet.ModelAndView com.controller.MainController.index()]
DEBUG DefaultListableBeanFactory:246 - Returning cached instance of singleton bean 'mainController'
DEBUG DispatcherServlet:912 - Last-Modified value for [/] is: -1

我尝试了什么:

i)通过添加以下jboss-web.xml对Wildfly进行相同的战争并且它正常工作,而无需在启动时调用/ *路径。

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
    <context-root>/</context-root>
</jboss-web>

ii)Tomcat 7和Tomcat 8上的同样的战争,使用名称ROOT.war进行部署,并且在不调用index()方法的情况下再次正常工作。

也许eclipse的插件导致了这个问题,因为如果我将路径更改为任何其他值,该方法不会被调用,或者我错过了什么?

对于长标题感到抱歉,无法找到更具描述性的标题。

Peter Rader提案之后

更新

控制器:

    @RequestMapping("/*")
    public ModelAndView index(HttpSession session,HttpServletRequest req) 
    {
        System.out.println(session.getId());
        System.out.println(req.getHeader("user-agent"));
        return new ModelAndView("index");
    }

服务器日志:

INFO: Server startup in 14690 ms
2016-08-17 18:25:53 [http-bio-8080-exec-1] DEBUG DispatcherServlet:823 - DispatcherServlet with name 'dispatcher' processing GET request for [/]
2016-08-17 18:25:53 [http-bio-8080-exec-1] DEBUG RequestMappingHandlerMapping:220 - Looking up handler method for path /
2016-08-17 18:25:53 [http-bio-8080-exec-1] DEBUG RequestMappingHandlerMapping:227 - Returning handler method [public org.springframework.web.servlet.ModelAndView com.controller.MainController.index(javax.servlet.http.HttpSession,javax.servlet.http.HttpServletRequest)]
2016-08-17 18:25:53 [http-bio-8080-exec-1] DEBUG DefaultListableBeanFactory:246 - Returning cached instance of singleton bean 'mainController'
2016-08-17 18:25:53 [http-bio-8080-exec-1] DEBUG DispatcherServlet:912 - Last-Modified value for [/] is: -1
735E606472EA914D4099265BD47474FA
Java/1.8.0_20
2016-08-17 18:25:56 [http-bio-8080-exec-1] DEBUG DefaultListableBeanFactory:1525 - Invoking afterPropertiesSet() on bean with name 'index'
2016-08-17 18:25:56 [http-bio-8080-exec-1] DEBUG DispatcherServlet:1206 - Rendering view [org.springframework.web.servlet.view.JstlView: name 'index'; URL [/WEB-INF/jsp/index.jsp]] in DispatcherServlet with name 'dispatcher'
2016-08-17 18:25:56 [http-bio-8080-exec-1] DEBUG JstlView:236 - Forwarding to resource [/WEB-INF/jsp/index.jsp] in InternalResourceView 'index'

0 个答案:

没有答案