1、配置application.yml
spring:
# 配置JSP
mvc:
view:
# 配置JSP文件位置,默认位置为:src/main/webapp
prefix: /WEB-INF/views/
# 配置JSP文件后缀
suffix: .jsp
# 配置JSP视图名
view-names: '*'
# 配置html
thymeleaf:
# 配置渲染文件的位置,默认位置为:classpath:/templates
prefix: classpath:/templates/composite
# 开发时关闭缓存,不然没法看到实时页面
cache: false
# 配置渲染文件的后缀
suffix: .html
view-names: /*
# 以“/”开头访问的就是html,以文件名开头访问的就是jsp
2、配置jsp视图解析器
@Value("${spring.mvc.view.prefix}")
private String prefix = "";
@Value("${spring.mvc.view.suffix}")
private String suffix = "";
@Value("${spring.mvc.view.view-names}")
private String viewNames = "";
@Bean
public InternalResourceViewResolver jspView(){
InternalResourceViewResolver view = new InternalResourceViewResolver();
view.setPrefix(prefix);
view.setSuffix(suffix);
view.setViewClass(JstlView.class);
view.setViewNames(viewNames);
return view;
}
3、编写控制器跳转页面
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* @author yangqian
* @date 2019-07-26
*/
@Controller
public class HelloController {
@RequestMapping(value = "/helloHtml", method = RequestMethod.GET)
public String helloHtml() {
return "/helloHtml";
}
@RequestMapping(value = "/helloJsp", method = RequestMethod.GET)
public String helloJsp() {
return "helloJsp";