在WEB-INF中将控制器URL映射到JSP

时间:2014-08-11 12:38:31

标签: spring spring-mvc spring-boot

我是Spring新手,我想将一些URL映射到JSP页面。我现在尝试了2个小时,但我无法正常工作。我确信这很简单,但我是Spring的新手。我正在使用Spring Boot。

(是的,我找到了像How can I map my Spring URL to a JSP file in /WEB-INF/views?这样的话题,但在我看来,我做的一切都是正确的......)

这是我的控制器。如果我在那里放置断点,它就会被调用。因此,我认为ViewResolver有问题......

@Controller
@RequestMapping("customers")
public class WebController {

    @RequestMapping(method=RequestMethod.GET)
    public String customers(Locale locale, Model model) {
        return "customers";
    }
}

这是我的WebSecurityConfigurerAdapter(我正在使用Spring Security):

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

//Some other methods, not relevant for this

    @Bean
    public InternalResourceViewResolver getViewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
   }
}

我的JSP文件放在WEB-INF / views / customers.jsp中。 当我调用localhost:8080 / customers /我得到(这是唯一的错误。服务器日志中没有其他人...):

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Aug 11 14:18:47 CEST 2014
There was an unexpected error (type=Not Found, status=404).

1 个答案:

答案 0 :(得分:3)

感谢Serge Ballesta我明白了。 : - )

我需要一个扩展WebMvcConfigurerAdapter的配置类,并使用@EnableWebMvc进行注释。我需要覆盖下面的方法。仅使用@EnableWebMvc注释WebSecurityConfigurerAdapter是不够的,并导致“找不到带有URI的HTTP请求的映射....”

@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public InternalResourceViewResolver getViewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
   }
}