How to configure spring boot mvc app for JSP?

前端 未结 5 1844
遥遥无期
遥遥无期 2020-12-15 12:45

I am new to Spring boot ( and servlet 3.0 ). I am trying to create spring mvc project with JSP as view. When I return a view from my controller it is not getting resolved a

相关标签:
5条回答
  • 2020-12-15 13:14
    @Bean
    public ViewResolver getViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }
    

    also needed and your pages should be at /webapp/WEB-INF/jsp/

    +

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
    
    0 讨论(0)
  • 2020-12-15 13:20

    If we want to use jsp as view in spring boot(which by default has tomcat server) we need to add the following to our pom.xml:

    <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
    </dependency>
    

    we need to add our pages inside

    src/main/webapp/WEB-INF/view

    After this we can specify the location for our jsp pages in application.properties

      spring.mvc.view.prefix=/WEB-INF/view/
      spring.mvc.view.suffix=.jsp
    
    0 讨论(0)
  • 2020-12-15 13:23

    You do not require the ViewResolver. pom.xml needs the mentioned dependencies as told by Yura and place the jsp files in src\main\webapp\WEB-INF\jsp.

    0 讨论(0)
  • 2020-12-15 13:25

    Assuming it is embedded Tomcat,

    You need to have followings in your pom.xml

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
    

    Embedded Tomcat core package has no JSP rendering support.

    0 讨论(0)
  • 2020-12-15 13:32
    1. Create webapp/WEB-INF/views {Name the last folder as U Like } under src/main
    2. add jstl jar

    3. add following two lines in application.properties

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

      Run As Spring Boot App ..U are good to go !

      For More U can consult my this project on github : https://github.com/SudipBiswasRana/Using-JSP-As-View-For-Spring-Project

    0 讨论(0)
提交回复
热议问题