SpringBoot支持jsp

1.为项目添加servlet等依赖;

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.3.1</version>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <scope>runtime</scope>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
</dependency>


<!-- servlet 依赖. --> <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <scope>provided</scope>
</dependency>

<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
</dependency>

<!--SpringBoot默认不支持JSP,需要在项目中添加相关的依赖--> <dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

<dependency>
   <groupId>org.eclipse.jdt.core.compiler</groupId>
   <artifactId>ecj</artifactId>
   <version>4.6.1</version>
   <scope>provided</scope>
</dependency>


2.为项目添加web的Module ;

右键选中项目  ==》 open Module settings ;



然后为web添加web.xml等资源:

注意,这里的地址最好写在src/main/webapp/web.xml这样




添加完成后,就可以在main文件夹下找到web资源文件夹了;


3.配置spring boot的配置文件,使其支持jsp;我这里application.yml 配置文件:

因为我的jsp界面 直接在WEB-INF/文件夹下面,所以就/WEB-INF/  了,这里可以看你的jsp在哪个文件夹下面

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



4.编写controller和jsp页面;

controller类:

@Controller
public class PersonController {


    @RequestMapping("hello")
    public String hello(){

        System.out.println("你好");

     //返回到jsp界面
        return "index";
    }


 
}


jsp页面:

能够 返回到jsp即代表成功了!




5.配置编译时将web资源也进行编译;

在pom.xml文件的<build>节点中新增内容:


<resources>
   <!-- 打包时将jsp文件拷贝到META-INF目录下-->  <resource>
      <!-- 指定处理哪个目录下的资源文件 -->  <directory>src/main/webapp</directory>
      <!--注意此次必须要放在此目录下才能被访问到-->  <targetPath>META-INF/resources</targetPath>
      <includes>
         <include>**/**</include>
      </includes>
   </resource>

</resources>

6.启动项目,测试:

我这里,在地址栏输入 :localhost:8080/hello.action  就可以进入action 并进入到jsp界面了。


就可以了。。。