我正在使用Spring Boot构建Spring MVC应用程序。出于某种原因,每当我从ModelAndView
返回Controller
时,这只会发送另一个请求,而不是返回JSP页面。
这个问题因日志而变得明显。
2016-02-17 14:24:47 DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/login]
2016-02-17 14:24:47 DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /login
2016-02-17 14:24:47 DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Returning handler method [public org.springframework.web.servlet.ModelAndView com.locker.controller.LoginController.login()]
2016-02-17 14:24:47 DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'loginController'
2016-02-17 14:24:47 DEBUG o.s.web.servlet.DispatcherServlet - Last-Modified value for [/login] is: -1
2016-02-17 14:24:47 DEBUG o.s.w.s.view.BeanNameViewResolver - No matching bean found for view name 'login'
2016-02-17 14:24:47 DEBUG o.s.b.f.s.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'login'
2016-02-17 14:24:47 DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2016-02-17 14:24:47 DEBUG o.s.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'login'; URL [/WEB-INF/pages/login.jsp]] in DispatcherServlet with name 'dispatcherServlet'
2016-02-17 14:24:47 DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'requestDataValueProcessor'
2016-02-17 14:24:47 DEBUG o.s.web.servlet.view.JstlView - Forwarding to resource [/WEB-INF/pages/login.jsp] in InternalResourceView 'login'
2016-02-17 14:24:47 DEBUG o.s.security.web.FilterChainProxy - /WEB-INF/pages/login.jsp at position 1 of 12 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter'
2016-02-17 14:24:47 DEBUG o.s.security.web.FilterChainProxy - /WEB-INF/pages/login.jsp at position 2 of 12 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
2016-02-17 14:24:47 DEBUG o.s.security.web.FilterChainProxy - /WEB-INF/pages/login.jsp at position 3 of 12 in additional filter chain; firing Filter: 'HeaderWriterFilter'
2016-02-17 14:24:47 DEBUG o.s.security.web.FilterChainProxy - /WEB-INF/pages/login.jsp at position 4 of 12 in additional filter chain; firing Filter: 'CsrfFilter'
2016-02-17 14:24:47 DEBUG o.s.security.web.FilterChainProxy - /WEB-INF/pages/login.jsp at position 5 of 12 in additional filter chain; firing Filter: 'LogoutFilter'
2016-02-17 14:24:47 DEBUG o.s.s.w.u.m.AntPathRequestMatcher - Request 'GET /web-inf/pages/login.jsp' doesn't match 'POST /logout
2016-02-17 14:24:47 DEBUG o.s.security.web.FilterChainProxy - /WEB-INF/pages/login.jsp at position 6 of 12 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter'
2016-02-17 14:24:47 DEBUG o.s.s.w.u.m.AntPathRequestMatcher - Request 'GET /web-inf/pages/login.jsp' doesn't match 'POST /login
2016-02-17 14:24:47 DEBUG o.s.security.web.FilterChainProxy - /WEB-INF/pages/login.jsp at position 7 of 12 in additional filter chain; firing Filter: 'RequestCacheAwareFilter'
2016-02-17 14:24:47 DEBUG o.s.security.web.FilterChainProxy - /WEB-INF/pages/login.jsp at position 8 of 12 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
2016-02-17 14:24:47 DEBUG o.s.security.web.FilterChainProxy - /WEB-INF/pages/login.jsp at position 9 of 12 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter'
2016-02-17 14:24:47 DEBUG o.s.s.w.a.AnonymousAuthenticationFilter - SecurityContextHolder not populated with anonymous token, as it already contained: 'org.springframework.security.authentication.AnonymousAuthenticationToken@9055c2bc: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS'
2016-02-17 14:24:47 DEBUG o.s.security.web.FilterChainProxy - /WEB-INF/pages/login.jsp at position 10 of 12 in additional filter chain; firing Filter: 'SessionManagementFilter'
2016-02-17 14:24:47 DEBUG o.s.security.web.FilterChainProxy - /WEB-INF/pages/login.jsp at position 11 of 12 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
2016-02-17 14:24:47 DEBUG o.s.security.web.FilterChainProxy - /WEB-INF/pages/login.jsp at position 12 of 12 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
2016-02-17 14:24:47 DEBUG o.s.security.web.FilterChainProxy - /WEB-INF/pages/login.jsp reached end of additional filter chain; proceeding with original chain
2016-02-17 14:24:47 DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/WEB-INF/pages/login.jsp]
2016-02-17 14:24:47 DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /WEB-INF/pages/login.jsp
2016-02-17 14:24:47 DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Returning handler method [public java.lang.String com.locker.controller.LoginController.errorHandler()]
2016-02-17 14:24:47 DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'loginController'
为了测试这一点,我添加了额外的RequestMapping
来处理新请求:
@RestController
public class LoginController {
private static final Logger logger =
LoggerFactory.getLogger(LoginController.class);
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login() {
logger.debug("welcome() is executed, value {}", "mkyong");
ModelAndView model = new ModelAndView("login");
return model;
// localhost:8080/WEB-INF/pages/login.jsp
}
@RequestMapping("WEB-INF/pages/login.jsp")
public String errorHandler() {
return "Yup, this happened.";
}
}
以下是MvcConfig
:
@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan(basePackages = { "com.locker"})
@PropertySource("classpath:application.properties")
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
registry.addViewController("/test").setViewName("test");
registry.addViewController("/login").setViewName("login");
}
@Bean
public InternalResourceViewResolver setupViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/pages/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Bean
public DefaultAnnotationHandlerMapping getDefaultAnnotationHandlerMapping() {
DefaultAnnotationHandlerMapping handlerMapping = new DefaultAnnotationHandlerMapping();
handlerMapping.setAlwaysUseFullPath(true);
handlerMapping.setDetectHandlersInAncestorContexts(true);
return handlerMapping;
}
}
和web.xml
:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
当然,login.jsp被添加到文件夹/ WEB-INF / pages /
中为什么它发送另一个请求而不是返回JSP页面?
答案 0 :(得分:1)
以@JB Nizet的评论为基础:
如果您没有在使用JSP方面投入大量资金,Thymeleaf template engine是使用与Spring Boot轻松集成的JSP的绝佳替代方案,如[使用Spring MVC提供Web内容]所示。因为Thymeleaf扩展了现有的HTML标记,所以您可以处理页面的布局并在浏览器中查看它,而无需JSP编译。使用Spring Boot,只需包含spring-boot-thymeleaf-starter依赖项将导致Spring Boot自动配置Thymeleaf视图解析器。
如果您在使用JSP方面投入了大量资金,那么您需要生成一个WAR文件,正如@Randyr链接到的文档所讨论的那样,并部署到独立的servlet容器中。在大多数具有附加配置的现代IDE中都支持这种功能。
另一件事对我来说很重要 - 您在控制器上使用@RestController
注释,但这似乎不是一个REST Web服务,而是一个Web应用程序。使用@RestController
可能会或可能不会让您感到胃灼热,但是对于身后的人来说会让您感到困惑 - 因为您返回@Controller
而使用ModelAndView
会更合适而不是请求正文转换为适当的媒体类型。