简体   繁体   中英

Spring MVC controller returns request

I am building a Spring MVC application using Spring Boot. For some reason whenever I return a ModelAndView from my Controller , this just sends another request, instead of returning the JSP page.

This problem was made apparant by the logs.

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'

To test this, I added an extra RequestMapping to handle the new request:

@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.";
}
}

Here are the 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;
}
}

and the 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>

Ofcourse the login.jsp is added in a folder /WEB-INF/pages/

Why does it send another request instead of returning the JSP page?

To build on @JB Nizet's comments:

  • The embedded Tomcat and Jetty servlet containers do not support rendering JSPs
  • If you are running your Spring Boot application out of your IDE, there is a good chance it is being run with one of the embedded servlet containers
  • If you are using the spring-boot-web-starter dependency, then Tomcat is the default embedded container

If you don't have a heavy investment in using JSPs, the Thymeleaf template engine is an excellent alternative to using JSPs that integrates very easily with Spring Boot as shown by [Serving Web Content with Spring MVC]. Because Thymeleaf extends existing HTML tags, you can work up the layout of the page and review it in a browswer without requiring the JSP compilation. And with Spring Boot, simply including the spring-boot-thymeleaf-starter dependency will cause Spring Boot to configure the Thymeleaf view resolver automatically.

If you do have a heavy investment in using JSPs, then you will need to generate a WAR file as discussed by the documentation that @Randyr linked to, and deploy to a standalone servlet container. There is support for that in most modern IDEs with additional configuration.

One other thing does stick out for me - you are using the @RestController annotation on your controller, but this does not appear to be a REST web service so much as a web application. Using @RestController may or may not cause you heartburn here, but it would be confusing to someone coming along behind you - it would be more appropriate to use @Controller since you are returning a ModelAndView rather than a request body to be converted to the appropriate media type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM