Spring MVC最新,html响应正在下载而不是显示

时间:2014-12-18 13:14:14

标签: spring-mvc servlet-3.0 wildfly-8

我有一个非常简单的spring mvc web app,在wildfly 8.2.0服务器上运行。使用Spring注释配置完全配置,包括WebApplicationInitializer(无web.xml)。控制器被调用但我的结果页面正在下载而不是显示。下面给出的是我的配置和控制器代码 配置代码

@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
public class HomeController {

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

我的hello.jsp在WEB-INF / views / hello.jsp

有什么想法吗?为什么结果html被下载而不是在浏览器中显示? 它下载为application / octet-stream,没有任何文件扩展名,文件内容是我的jsp的html

  

4:59:11,812 DEBUG [org.springframework.web.servlet.DispatcherServlet](默认任务-2)名称为“dispatcher”的DispatcherServlet处理[/ sample / common / hello]的GET请求   14:59:11,816 DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping](默认任务-2)查找path / common / hello的处理程序方法   14:59:11,822 DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping](默认任务-2)返回处理程序方法[public java.lang.String org.egov.admin.web.controllers.common .HomeController.sayHello()]   14:59:11,822 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory](默认任务-2)返回单例bean'homeController'的缓存实例   14:59:11,824 DEBUG [org.springframework.web.servlet.DispatcherServlet](默认任务-2)[/ sample / common / hello]的Last-Modified值为:-1   14:59:24,750 INFO [org.egov.admin.web.controllers.common.HomeController](默认任务-2)在HomeHtroller的sayHello方法中   14:59:24,762 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory](默认任务-2)在名为'common / hello'的bean上调用afterPropertiesSet()   14:59:24,762 DEBUG [org.springframework.web.servlet.DispatcherServlet](默认任务-2)渲染视图[org.springframework.web.servlet.view.JstlView:name'common / hello';名为“dispatcher”的DispatcherServlet中的URL [/WEB-INF/views/common/hello.jsp]]   14:59:24,766 DEBUG [org.springframework.web.servlet.view.JstlView](默认任务-2)在InternalResourceView'common / hello'中转发资源[/WEB-INF/views/common/hello.jsp]   14:59:24,768 DEBUG [org.springframework.web.servlet.DispatcherServlet](默认任务-2)名称为“dispatcher”的DispatcherServlet处理[/sample/WEB-INF/views/common/hello.jsp]的GET请求   14:59:24,773 DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping](默认任务-2)查找路径/WEB-INF/views/common/hello.jsp的处理程序方法   14:59:24,773 DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping](默认任务-2)未找到[/WEB-INF/views/common/hello.jsp]的处理程序方法   14:59:24,774 DEBUG [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping](默认任务-2)请求的匹配模式[/WEB-INF/views/common/hello.jsp]是[/ **]   14:59:24,775 DEBUG [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping](默认任务-2)请求[/WEB-INF/views/common/hello.jsp]的URI模板变量是{}   14:59:24,776 DEBUG [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping](默认任务-2)使用handler [org.springframework.web]将[/WEB-INF/views/common/hello.jsp]映射到HandlerExecutionChain .servlet.resource.DefaultServletHttpRequestHandler @ 4fb5a525]和1个拦截器   14:59:24,777 DEBUG [org.springframework.web.servlet.DispatcherServlet](默认任务-2)[/sample/WEB-INF/views/common/hello.jsp]的最后修改值为:-1   14:59:24,793 DEBUG [org.springframework.web.servlet.DispatcherServlet](默认任务-2)Null ModelAndView返回DispatcherServlet,名称为'dispatcher':假设HandlerAdapter已完成请求处理   14:59:24,793 DEBUG [org.springframework.web.servlet.DispatcherServlet](默认任务-2)已成功完成请求   14:59:24,797 DEBUG [org.springframework.web.servlet.DispatcherServlet](默认任务-2)已成功完成请求   15:00:00,758 DEBUG [org.jboss.ejb.client.txn](定期恢复)将事务源节点标识符1的恢复请求发送到具有节点名称nick-laptop的EJB接收器

卷曲输出

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>

作为更新,如果我不使用WebApplicationInitializer,而是使用带有以下最低内容的web.xml

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

1 个答案:

答案 0 :(得分:0)

正如SPR-12558所述,servlet映射应设置为/,而不是/* Java类中的ServletInitializer(如web.xml中所示)