提问者:小点点

Spring引导jsp未解析


我有一个工作启动MVC应用程序,我需要添加jsp视图。

我已经添加了所有必需的罐子

<dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jdt.core.compiler</groupId>
        <artifactId>ecj</artifactId>
        <version>4.6.1</version>
    </dependency>

我在application. yml中的视图配置

spring:
 mvc:
view:
  prefix: /WEB-INF/jsp/
  suffix: .jsp

将我的jsp添加到src/main/webapp/WEB-INF

我的控制器

@Controller
public class PreAuthController {

@RequestMapping( value="/" , method = RequestMethod.GET  )
public String index(){
    return "dashboard";
}

@RequestMapping( value="/dashboard" , method = RequestMethod.GET  )
public String dashboard(){
    return "dashboard";
}

@RequestMapping( value="/login" , method = RequestMethod.GET  )
    public String login(){
        return "login";
}

}

日志显示正确的docroot

文档根目录: /Users/myhome/workspace/wi-preauth/wi-preauth-用户界面/src/main/webapp

当我试图击中http://localhost:8080/

我一直收到这个错误

Could not resolve view with name 'login' in servlet with name 
'dispatcherServlet'

我已经尝试了从论坛上问的其他问题到无济于事的一切…

任何帮助都非常感谢。


共2个答案

匿名用户

我发现了我的问题。我有一个扩展WebMvcConfigurationSupport的配置类。这反过来又禁用了WebMvcAutoConfiguration。所以我不得不添加自己的

@Bean
  public UrlBasedViewResolver viewResolver() {
  UrlBasedViewResolver resolver
    = new UrlBasedViewResolver();
  resolver.setPrefix("/WEB-INF/jsp/");
  resolver.setSuffix(".jsp");
  resolver.setViewClass(JstlView.class);
  return resolver;
 }

在我的配置中。

匿名用户

创建ApplicationContextjava配置类。

位置有助于在JSP中注册加载静态文件(如css、图像、字体和js文件)的路径。

@Configuration
@EnableWebMvc
@ComponentScan
public class ApplicationContext extends WebMvcConfigurerAdapter
{
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/view/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        registry.viewResolver(resolver);
    }
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/" };
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (!registry.hasMappingForPattern("/webjars/**")) {
            registry.addResourceHandler("/webjars/**").addResourceLocations(
                    "classpath:/META-INF/resources/webjars/");
        }
        if (!registry.hasMappingForPattern("/**")) {
            registry.addResourceHandler("/**").addResourceLocations(
                    CLASSPATH_RESOURCE_LOCATIONS);
        }
    }


}