springboot使用jsp的设置和问题

Springboot中使用jsp的设置和问题

jsp的基本使用

  • 先在main下创建webapp->WEB-INF,在里面创建jsp页面:jsp文件路径

  • 在pom.xml文件中的dependencies标签内加入以下依赖

    <!--用于编译jsp-->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <!-- servlet依赖 -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <scope>provided</scope><!--只参与编译测试,不参与打包,避免冲突-->
    </dependency>
    <!-- jstl依赖 -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    
  • 在yml中加入下面的配置

    spring:
      mvc:
        view: #Spring boot视图配置
          prefix: /WEB-INF/
          suffix: .jsp
    #    static-path-pattern: /static/** #静态文件访问配置 类路径下;例:/static/hello.html 或 /static/login.css
          # 默认值为 classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
    
  • 通过以上配置,再写controller来请求jsp页面,只会通过controller来请求jsp页面,直接访问的不会

    @Controller
    public class helloController {
        @RequestMapping("/main")
        public String main() {
            return "main";
        }
    
        @RequestMapping("index")
        public String index() {
            return "mmm/index";
        }
    }
    

关于使用jsp页面打成jar包启动访问不了的问题

  • 要在pom文件中build标签进行以下配置

    <resources>   
    	<!-- 打包时将jsp文件拷贝到META-INF目录下 -->
        <resource>
            <!-- 指定resources插件处理哪个目录下的资源文件 -->
            <directory>src/main/webapp</directory>
            <!--这里必须是META-INF/resources-->
            <targetPath>META-INF/resources</targetPath>
            <includes>
                <include>**/**</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
    
  • 在pom文件插件中,将maven插件修改版本,高版本不兼容

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    
        <version>1.4.2.RELEASE</version><!--maven打包插件,版本高了不兼容,所以用这个-->
        <configuration>
            <!--启动类-->
            <mainClass>com.wu.supermarket.SupermarketApplication</mainClass>
        </configuration>
    </plugin>
    

    修改后打包成jar时就会将jsp一起打包进jar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值