【SpringBoot】新建springboot项目,访问jsp页面却让我下载和热部署的问题

本人菜鸡一只。

最近突然有些空闲的时间,因为最近跟数据打交道比较多(写sql,写spark,写数据质量监控,写存储过程等),慢慢的java框架的有些知识在我脑中被淡化了,这是很可怕的事情,俗话说,一天不写代码,就要上房揭瓦(这是哪门子俗话?),所以想自己搭个以前懵懵懂懂的sb(SpringBoot)框架,来重温或者复习(其实是从头学吧?)以前的知识!

首先我得创建一个sb项目吧(其实就是通过maven创建一个webapp项目)

刚开始,我就遇到了两个问题,我一个一个慢慢来说吧。

一、写好的jsp页面,用浏览器打开,老是让我下载,这是怎么回事???

应该解释为pom里面缺少jsp的解析依赖,只要添加上相关依赖,然后重新刷新pom文件,更新jar包,重启项目即可

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

感谢:https://blog.csdn.net/qq_22585453/article/details/81454200(作者:Carry_man)提供的解决方法!

二、热部署怎么在idea上实现

热部署含义就是:当我在修改代码和配置文件的时候,我的java项目会自动重启刷新我的项目,避免了我每次修改了什么东西都要手动重启tomcat(加快开发,我觉得这个功能还是蛮帅的,所以特地研究了下,搞了出来~)

有两部分要设置:

1、在pom中添加依赖(完整的pom文件,可拉到文章的最后)

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 没有该配置,devtools 不生效 -->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
<finalName>SpringBoot</finalName>
</build>

2、让idea能够支持自动更新的操作

idea需要修改两个地方

-1.勾选Build project automatically

-2.勾选compiler.automake.allow.when.app.running

Crtl+Alt+Shift+/  选择Registry

这个勾选完,直接点Close就好了,旁边那个按钮(Restore Defaults是恢复出厂设置,别把他当成确认键了),最好设置完之后,关掉,再重新打开这些配置查看下~

感谢:https://blog.csdn.net/qq_41700133/article/details/82224390(作者:无敌的我又忘起名了)提供的解决方法

(“无敌的我又忘起名了”的博客中的方法,有可能跟我写的有些许出入,我是用我博客中提供的方法解决的,所以我个人觉得以我的博客为主吧,当然如果设置之后不生效,大家可以看看其他文章,试试其他方法~)

文章的最后,附上我的pom文件!

注:我暂时没有数据库相关的代码配置连接等等,所以将所有与数据库有关的依赖全部注释掉了

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied.  See the License for the
  specific language governing permissions and limitations
  under the License.
-->
<!-- $Id: pom.xml 642118 2008-03-28 08:04:16Z reinhard $ -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <packaging>war</packaging>

  <name>SpringBoot</name>
  <groupId>SpringBoot</groupId>
  <artifactId>SpringBoot</artifactId>
  <version>1.0-SNAPSHOT</version>



  <!-- 配置spring boot所需的依赖 -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
  </parent>

  <dependencies>
    <!-- spring boot的相关启动 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- 热部署 -->

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <optional>true</optional>
      <scope>true</scope>
    </dependency>


    <!-- log4j -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>


<!--解决访问jsp文件的时候变成下载jsp的问题-->
    <!--jsp支持 -->
    <!-- servlet 依赖. -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
    </dependency>
    <!-- 使用Jasper引擎解析JSP -->
    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.tomcat</groupId>
      <artifactId>tomcat-jsp-api</artifactId>
    </dependency>
    <!-- spring boot整合mybatis -->
   <!-- <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.0</version>
    </dependency>

    &lt;!&ndash; MySQL &ndash;&gt;
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>

    &lt;!&ndash; 加入p6spy的依赖 &ndash;&gt;
    <dependency>
      <groupId>p6spy</groupId>
      <artifactId>p6spy</artifactId>
      <version>2.1.4</version>
    </dependency>

    &lt;!&ndash; druid数据库连接池 &ndash;&gt;
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid-spring-boot-starter</artifactId>
      <version>1.1.0</version>
    </dependency>

    &lt;!&ndash;pagehelper &ndash;&gt;
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper-spring-boot-starter</artifactId>
      <version>1.1.1</version>
    </dependency>

-->

  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <!-- 没有该配置,devtools 不生效 -->
          <fork>true</fork>
        </configuration>
      </plugin>
    </plugins>
    <finalName>SpringBoot</finalName>
  </build>


</project>

好了,本文还挺简单的,就是记录刚刚创建sb项目之后,遇到的两个简单的问题,大家要是遇到了,肯定也有能力可以很快解决这两个问题的。

最后的最后,本人菜鸡一只~如果有说错的,或者有小伙伴按照我的配置之后还不生效的,可以给我留言,咱们一起研究探讨看看~下次再见拜拜!

猜你喜欢

转载自blog.csdn.net/lsr40/article/details/87799708