方法一、放在src/main/webapp下面
1、在properties中加入如下配置:
#返回的前缀 目录对应src/main/webapp下
spring.mvc.view.prefix:/WEB-INF/jsp/
#返回的后缀
spring.mvc.view.suffix: .jsp
2、在pom中添加以下依赖:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>7.0.59</version>
</dependency>
说明:springboot自带的tomcat识别不了jsp文件,访问后无法处理会出现浏览器下载jsp文件的情况,加上这个依赖就可以了。
3、controller:
@RequestMapping("/index")
public String index(){
System.out.println("jsp");
return "Aaa";
}
项目结构如下:
再例如:
方法二、放在classpath下且不使用template
1. controller中定义对应的访问路由及返回的页面(使用Controller,不要使用RestController),如:
1 2 3 4 |
|
2.在SpringBoot配置文件中配置SpringMVC
spring:
mvc:
view:
prefix: /
suffix: .html
3.html文件配置路径。
静态文件要放在SpringBoot默认的加载路径下(SpringBoot中的src/main/resources/文件夹对应classpath:):
classpath:/META-INF/resources、classpath:/resources、classpath:/static、classpath:/public
方法三、放在classpath下且使用thymeleaf:
1.引入thymeleaf依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.html放在classpath:/templates下。如果html都是放在templates下,SpringBoot的配置文件不要配置,因为默认配置就是这个路径。
3.如果要自定义需要在SpringBoot配置文件中自定义配置。
spring:
thymeleaf:
suffix: .html
prefix: classpath:/xx/xx/
如果有更深层的路径,可以在controller的返回值拼上对应的html路径。
如配置为:prefix: classpath:/templates/,现要返回templates/order/order.html,controller就要返回 "order/order"
例:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>sso-login</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.html
server.port=8081
server.context-path=/ssologin
package com.sso.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/index")
@Controller
public class IndexController {
@RequestMapping("/index")
public String index(){
System.out.println("jsp");
return "login";
}
}