1.创建项目是添加web依赖
2.在pom文件中添加jap依赖
org.apache.tomcat.embed tomcat-embed-jasperprovided javax.servlet jstl
3.创建webapp/WEB-INF/jsp(jsp不能写在templates中)
webapp有一个蓝色的圈,如果没有就在modules的web Rseource Directory设置,找到webapp目录
4.在application.properties配置前缀后缀
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
5.也可以不用application.properties而用配置类
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.rmi.registry.Registry;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/jsp/",".jsp");
}
}
创建Controlle和jspr测试
helloController
package com.example.jsp;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class helloController {
@RequestMapping("/hello")
public String hello(Model model){
model.addAttribute("msg","hello world");
return "01";
}
}
01.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
${msg}



