这是我的参考项目
https://github.com/Baeldung/spring-security-registration
我使用的是springboot2,我想完全控制我的MVC。
所以我用
@EnablWebMvc
禁用自动配置为如下所示:-
@Configuration
@ComponentScan(basePackages = { "com.example.demo" })
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
public MvcConfig() {
super();
}
@Autowired
private MessageSource messageSource;
@Override
public void addViewControllers(final ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index");
registry.addViewController("/index");
registry.addViewController("/loginRememberMe");
registry.addViewController("/customLogin");
registry.addViewController("/registration.html");
registry.addViewController("/registrationCaptcha.html");
registry.addViewController("/logout.html");
registry.addViewController("/homepage.html");
registry.addViewController("/expiredAccount.html");
registry.addViewController("/badUser.html");
registry.addViewController("/emailError.html");
registry.addViewController("/home.html");
registry.addViewController("/invalidSession.html");
registry.addViewController("/console.html");
registry.addViewController("/admin.html");
registry.addViewController("/successRegister.html");
registry.addViewController("/forgetPassword.html");
registry.addViewController("/updatePassword.html");
registry.addViewController("/changePassword.html");
registry.addViewController("/users.html");
registry.addViewController("/qrcode.html");
}
@Override
public void configureDefaultServletHandling(final DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/", "/resources/");
}
@Override
public void addInterceptors(final InterceptorRegistry registry) {
final LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
registry.addInterceptor(localeChangeInterceptor);
}
// beans
@Bean
public LocaleResolver localeResolver() {
final CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
cookieLocaleResolver.setDefaultLocale(Locale.ENGLISH);
return cookieLocaleResolver;
}
// @Bean
// public MessageSource messageSource() {
// final ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
// messageSource.setBasename("classpath:messages");
// messageSource.setUseCodeAsDefaultMessage(true);
// messageSource.setDefaultEncoding("UTF-8");
// messageSource.setCacheSeconds(0);
// return messageSource;
// }
@Bean
@ConditionalOnMissingBean(RequestContextListener.class)
public RequestContextListener requestContextListener() {
return new RequestContextListener();
}
@Override
public Validator getValidator() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.setValidationMessageSource(messageSource);
return validator;
}
现在当我打开应用程序这是:-
http://localhost:8888/
javax.servlet.ServletException: Could not resolve view with name 'forward:/index' in servlet with name 'dispatcherServlet'
我试过了
registry.addViewController("/index");
registry.addViewController("/index.html");
什么都不管用。