Spring MVC将jsp作为text/plain内容类型返回

我有Spring MVC和jsp网页的问题.

当我请求网页时,它默认返回text / plain.我尝试在HttpServletResponse控制器上手动将内容类型设置为text / html,并且Web浏览器重新协调它,正确显示它但是在主体上编码jsp标记.例:

JSP:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

Meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    

浏览器收到:

Meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    

我正在使用Spring Boot和基于java web的注释进行配置.另外,我正在使用@EnableAutoConfiguration,我发现通过将sevlet从“/ *”映射到“/”来解决问题.

由于java注释,我不得不重写dispatchedServlet(https://github.com/spring-projects/spring-boot/issues/91#issuecomment-27626036),配置类是:

@Configuration
public class DispatcherConfiguration {

@Bean
public ServletRegistrationBean dispatcherRegistration() {
    ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet());
    registration.addUrlMappings("/");
    registration.setLoadOnStartup(1);

    System.out.println("~~~~~~~ Servlet regristated " + registration.getServletName());
    return registration;

}

@Bean
public DispatcherServlet dispatcherServlet() {
    return new DispatcherServlet();
}

请注意,我正在使用@Bean注释错过上一个注释链接,因为如果没有,Spring引发异常.但即使不使用bean注释,并使用xml主要问题仍然存在).

这是Spring MVC配置文件

@Configuration
@EnableWebMvc
@ComponentScan("es.sfacut")
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/login").setViewName("login"); 
    registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}

// Maps resources path to webapp/resources
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    registry.addResourceHandler("/images/**").addResourceLocations("/resources/images/");
    registry.addResourceHandler("/imgs/**")
        .addResourceLocations(Constant.RESOURCE_IMAGES_PATH);
}

@Bean
public UrlBasedViewResolver setupViewResolver() {
    UrlBasedViewResolver resolver = new UrlBasedViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
// resolver.setContentType("text/html");
    resolver.setViewClass(JstlView.class);

    return resolver;
}

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

@Bean
public MultipartResolver multipartResolver() {
    CommonsMultipartResolver resolver = new CommonsMultipartResolver();
    // Size in bytes
    resolver.setMaxUploadSize(1752300);
    return resolver;
}

}

主要课程:

@EnableAutoConfiguration
@ComponentScan("es.sfacut")
@EnableJpaRepositories
public class TelocomproMain {

    public static void main(String[] args) {
        SpringApplication.run(TelocomproMain.class,args);
    }

}

控制器:

@RestController
@RequestMapping(value = "/api/")  
public class CategoryController {
    @RequestMapping(value="test4",method = RequestMethod.GET,produces = "text/plain;charset=UTF-8")
    public ModelAndView pageTest4(HttpServletResponse response) {

        ModelAndView model = new ModelAndView("elovendo/index");
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        return model;
    }
}

Application.properties:

#View resolver
spring.view.prefix= /WEB-INF/views/
spring.view.suffix= .jsp

不确定我是否遗漏了什么,我会发布你需要的一切.

先感谢您.

最佳答案
您的控制器应该是@Controller而不是@RestController,否则响应将作为您观察到的字符串返回.

正如评论中已经提到的那样,根本不需要您的自定义配置,您可以查看Spring启动团队https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp提供的示例.

相关文章

Spring Cloud为Spring Boot应用程序提供Netflix OSS集成。 提供的功能模块包括服务发现(Eureka),断路...
Spring Cloud 学习笔记;maven配置;入门学习;基于Spring Boot 实现;服务端配置,客户端配置;
可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的...
Spring中各种方式进行日期时间处理,有作用于单个实体的,也有作用于全局的,有作用于请求入参的,有作...
跨域资源共享(Cross-origin resource sharing)(CORS)是W3C的标准,大部分的浏览器都实现了这个标准...
Spring Boot使创建基于Spring的应用程序变得轻松,大部分的SpringBoot应用程序都只需要很少的Spring配置...