I have a problem with Spring MVC and jsp webpage.
When I request a webpage, it returns as text/plain by default. I tried setting the content type as text/html manually at the HttpServletResponse
controller, and the web browser reconigzes it, showing it correctly BUT encoding the jsp tags at the body. Example:
JSP:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Error 405 ******* </title>
</head>
<body> asdasdsasad </body>
</html>
Browser receives:
<html>
<body><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Title</title>
</body>
</html>
I'm using Spring Boot and java web based annotation for configuration. Also, I'm using @EnableAutoConfiguration
, and I found that the problem is solved by mapping the sevlet from "/*" to "/".
Because of the java annotation, I had to override the dispatchedServlet (https://github.com/spring-projects/spring-boot/issues/91#issuecomment-27626036) and the configuration class is:
@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();
}
Notice that I'm using the @Bean
annotation missfollowing the previous comment link, because if not, Spring boot throws an exception. But even if not using the bean annotation, and using an xml the main problem is still here).
This is the Spring MVC configuration file:
@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;
}
}
Main class:
@EnableAutoConfiguration
@ComponentScan("es.sfacut")
@EnableJpaRepositories
public class TelocomproMain {
public static void main(String[] args) {
SpringApplication.run(TelocomproMain.class, args);
}
}
Controller:
@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
Not sure if I'm missing something, I'll post everything you need.
Thank you in advance.
The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale and theme resolution as well as support for uploading files.
What Is Spring MVC? Spring MVC is a library within the Spring framework that simplifies handling HTTP requests and responses. It's built on the Servlet API and is an essential component of the Spring Framework.
Your controller should be a @Controller
not a @RestController
, otherwise the response will be returned as a string as you observe.
As mentioned already in the comments your custom configuration is not needed at all, you could have a look at the sample provided by the Spring boot
team https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With