我遇到了Spring MVC和jsp网页的问题。
当我请求网页时,默认情况下返回 text / plain 。我尝试在HttpServletResponse
控制器上手动将内容类型设置为 text / html ,然后网络浏览器重新协调它,正确显示它但是在jsp标签处编码身体。示例:
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>
浏览器收到:
<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>
我使用Spring Boot和基于java web的注释进行配置。另外,我使用@EnableAutoConfiguration
,我发现问题是通过映射来自&#34; / *&#34;的sevlet来解决的。到&#34; /&#34;。
由于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
不确定我是否遗漏了某些内容,我会发布您需要的所有内容。
提前谢谢。
答案 0 :(得分:2)
您的控制器应该是@Controller
而不是@RestController
,否则响应将以您观察到的字符串形式返回。
正如评论中已经提到的,您根本不需要自定义配置,您可以查看Spring boot
团队https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp提供的示例。
答案 1 :(得分:0)
有同样的问题。 当你的jsp在目录中由 ResourceHandler 处理时会发生这种情况:
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
这仅适用于静态资源 - img,css,js。 你应该把你的jsp移到外面