无法在Spring Boot MVC中解析视图名称

时间:2019-01-23 02:53:59

标签: java spring spring-boot spring-mvc

我正在使用spring boot开发spring MVC应用程序。

我的pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <!-- <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency> -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency> -->
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
</dependency>
    <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>19.0</version>
    </dependency>

我的控制器类:

@EnableWebMvc
@Controller
public class RootController {
@RequestMapping("/")
public String home()

{
    return "index";
}

@RequestMapping("/next")
public String next()
{
    return "next";
}
}

应用主类

@SpringBootApplication
@ComponentScan(basePackages= {"com.approot.controller"})
@EnableWebMvc
public class LimitBoApplication {

public static void main(String[] args) {
    SpringApplication.run(LimitBoApplication.class, args);
}

}

我的春天application.properties

server.port =8090
spring.mvc.view.suffix=.jsp
server.servlet.context-path=/AppBO

当我运行此应用程序时,出现以下错误。

   Could not resolve view with name 'index' in servlet with name 
  'dispatcherServlet'
   javax.servlet.ServletException: Could not resolve view with name 'index' 
   in servlet with name 'dispatcherServlet'
   at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1350)
   at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1116)
   at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1055)
   at  org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)

我已将jsp文件放在LimitBO\src\main\resources\templates中,spring仍然找不到jsp文件。

我在这里做错了什么?我试图将这些jsp文件添加到资源文件夹中,但仍然无法正常工作。

2 个答案:

答案 0 :(得分:0)

.jsp文件移动到src/main/webapp/WEB-INF/jsp/并将spring.mvc.view.prefix: /WEB-INF/jsp/添加到application.properties,在@EnableWebMvc中不需要RootController

希望这会有所帮助。

答案 1 :(得分:0)

创建Java配置并尝试以下操作:

@Configuration
@EnableWebMvc
public class WebConfigMvc extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver viewResolver () {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/jsp"); //this is location you put jsp
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
}

通过JSP渲染tomcat时,您需要添加dependecy。

    <!-- Tomcat for JSP rendering -->
  <dependency>
     <groupId>org.apache.tomcat.embed</groupId>
     <artifactId>tomcat-embed-jasper</artifactId>
     <scope>provided</scope>
    </dependency>

如果配置不起作用。尝试以下:

 spring.mvc.view.prefix = /WEB-INF/jsp/
   spring.mvc.view.suffix = .jsp
   spring.mvc.static-path-pattern=/resources/**