Search code examples
spring-mvcservlet-3.0wildfly-8

Spring MVC latest, html response is getting downloaded instead of displaying


I have very simple spring mvc web app, running in wildfly 8.2.0 server. Configured fully with spring annotation config including WebApplicationInitializer (no web.xml). Controller is getting invoked but my results page is getting downloaded instead of displaying. given below is my config and controller code Config Code

@Configuration 
@EnableWebMvc
@ComponentScan(basePackages = { "com.sample.web.controllers" })
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public InternalResourceViewResolver jspViewResolver() {
        InternalResourceViewResolver bean = new InternalResourceViewResolver();
        bean.setPrefix("/WEB-INF/views/");
        bean.setSuffix(".jsp");
        return bean;
    }

    @Bean(name = "multipartResolver")
    public CommonsMultipartResolver getMultipartResolver() {
        return new CommonsMultipartResolver();
    }
}

Controller code is

@Controller
public class HomeController {

    @RequestMapping(value="/hello",method=RequestMethod.GET)
    public String sayHello(){
        return "hello";
    }
}

My hello.jsp is inside WEB-INF/views/hello.jsp

Any idea? why the result html is getting downloaded instead of displaying in browser ? Its downloaded as application/octet-stream with out any file extension, the content of the file is my jsp's html

4:59:11,812 DEBUG [org.springframework.web.servlet.DispatcherServlet] (default task-2) DispatcherServlet with name 'dispatcher' processing GET request for [/sample/common/hello] 14:59:11,816 DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (default task-2) Looking up handler method for path /common/hello 14:59:11,822 DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (default task-2) Returning handler method [public java.lang.String org.egov.admin.web.controllers.common.HomeController.sayHello()] 14:59:11,822 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] (default task-2) Returning cached instance of singleton bean 'homeController' 14:59:11,824 DEBUG [org.springframework.web.servlet.DispatcherServlet] (default task-2) Last-Modified value for [/sample/common/hello] is: -1 14:59:24,750 INFO [org.egov.admin.web.controllers.common.HomeController] (default task-2) In sayHello Method of HomeController 14:59:24,762 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] (default task-2) Invoking afterPropertiesSet() on bean with name 'common/hello' 14:59:24,762 DEBUG [org.springframework.web.servlet.DispatcherServlet] (default task-2) Rendering view [org.springframework.web.servlet.view.JstlView: name 'common/hello'; URL [/WEB-INF/views/common/hello.jsp]] in DispatcherServlet with name 'dispatcher' 14:59:24,766 DEBUG [org.springframework.web.servlet.view.JstlView] (default task-2) Forwarding to resource [/WEB-INF/views/common/hello.jsp] in InternalResourceView 'common/hello' 14:59:24,768 DEBUG [org.springframework.web.servlet.DispatcherServlet] (default task-2) DispatcherServlet with name 'dispatcher' processing GET request for [/sample/WEB-INF/views/common/hello.jsp] 14:59:24,773 DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (default task-2) Looking up handler method for path /WEB-INF/views/common/hello.jsp 14:59:24,773 DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (default task-2) Did not find handler method for [/WEB-INF/views/common/hello.jsp] 14:59:24,774 DEBUG [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (default task-2) Matching patterns for request [/WEB-INF/views/common/hello.jsp] are [/**] 14:59:24,775 DEBUG [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (default task-2) URI Template variables for request [/WEB-INF/views/common/hello.jsp] are {} 14:59:24,776 DEBUG [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (default task-2) Mapping [/WEB-INF/views/common/hello.jsp] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@4fb5a525] and 1 interceptor 14:59:24,777 DEBUG [org.springframework.web.servlet.DispatcherServlet] (default task-2) Last-Modified value for [/sample/WEB-INF/views/common/hello.jsp] is: -1 14:59:24,793 DEBUG [org.springframework.web.servlet.DispatcherServlet] (default task-2) Null ModelAndView returned to DispatcherServlet with name 'dispatcher': assuming HandlerAdapter completed request handling 14:59:24,793 DEBUG [org.springframework.web.servlet.DispatcherServlet] (default task-2) Successfully completed request 14:59:24,797 DEBUG [org.springframework.web.servlet.DispatcherServlet] (default task-2) Successfully completed request 15:00:00,758 DEBUG [org.jboss.ejb.client.txn] (Periodic Recovery) Send recover request for transaction origin node identifier 1 to EJB receiver with node name nick-laptop

Curl output

curl -v http://localhost:9090/sample/common/hello
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 9090 (#0)
> GET /sample/common/hello HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:9090
> Accept: */*
> 
< HTTP/1.1 200 OK
< Connection: keep-alive
< Last-Modified: Thu, 18 Dec 2014 12:29:49 GMT
< X-Powered-By: Undertow/1
* Server WildFly/8 is not blacklisted
< Server: WildFly/8
< Content-Type: application/octet-stream
< Content-Length: 244
< Content-Language: en-IN
< Date: Fri, 19 Dec 2014 09:45:23 GMT
< 
<%@page contentType="text/html; charset=UTF-8" language="java" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title> 
</head>
<body>
Hello There
</body>
* Connection #0 to host localhost left intact
</html>

As an update if i dont use WebApplicationInitializer and instead if i use web.xml with the following minimum content

<context-param>
    <param-name>contextClass</param-name>
    <param-value>
        org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    </param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>SpringDispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>org.egov</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>SpringDispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Solution

  • As answered on SPR-12558, the servlet mapping should be set to /, not /* in the ServletInitializer Java class (like in the web.xml).