美文网首页
SpringBoot整合jsp+静态资源笔记

SpringBoot整合jsp+静态资源笔记

作者: landlord_ | 来源:发表于2019-12-31 11:07 被阅读0次

    一、首先搭建SpringBoot项目。

    此处不介绍,可参照:https://www.jianshu.com/p/3f25c3ca1480

    二、整合访问jsp

    1、maven引入相关 依赖(不包含其他Springboot项目依赖)
        <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
            </dependency>
            <!-- servlet支持 -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
            </dependency>      
            <!-- jstl 支持 -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
            </dependency>   
        <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
    
    2、application.properties配置文件中配置jsp前置路径和后缀
    #Spring Mvc 配置
    spring.mvc.view.prefix=/WEB-INF/jsp/
    spring.mvc.view.suffix=.jsp
    
    3、启动类,继承SpringBootServletInitializer,重载方法
    @SpringBootApplication
    public class App extends SpringBootServletInitializer{
      @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
                return application.sources(App.class);
            }
            
        public static void main(String[] args) {
                SpringApplication.run(App.class, args);
            }
        }
    
    4、在java/main路径下新建webapp/WEB-INF/jsp文件夹,下面放jsp文件 index.jsp
    5、controller中访问,切记,不可加@ResponseBody,或者类上只可用
    #@Controller,不可用ResController,因为这样会直接返回 index 字符串。
    # 方法上也不可用@ResponseBody
    @Controller
    public class  TestController{
        @RequestMapping("/index")
        public String getIndex(){
            return "index";
        }
    }
    
    6、访问:ip+端口:/index 可访问到index.jsp

    静态资源访问

    1、在以上基础,新增静态资源路径配置
    #访问静态资源配置
    spring.resources.static-locations=classpath:/WEB-INF/static
    
    2、WEB-INF下建立文件夹 static 储存静态文件 如image
    3、将webapp 添加到biuldpath路径。
    4、在jsp中可直接从 image 开始引用文件 如src="image/test.jpg"

    相关文章

      网友评论

          本文标题:SpringBoot整合jsp+静态资源笔记

          本文链接:https://www.haomeiwen.com/subject/cjlboctx.html