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

前端之家收集整理的这篇文章主要介绍了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相关文章