官方为啥不推荐jsp,参考:https://spring.io/blog/2012/10/30/spring-mvc-from-jsp-and-tiles-to-thymeleaf,里面写了很多,其中我觉得最重要的应该是jsp不利于前端开发,如果是jsp页面前端要怎么运行?怎么看写出来的效果?反正各种不利于前端开发吧,spring 推荐Thymeleaf,它的模版文件就是html,可以直接在浏览器打开,闲话不多说,先看非要集成jsp的话该怎么做
1. 在pom.xm中加入支持JSP的依赖
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
2. 创建src/main/webapp/WEB-INF/views目录,JSP文件就放这里
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
Hello ${name}
</body>
</html>
3. 在src/main/resources/application.properties文件中进行解析器的配置
# MVC
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
4. 编写Controller
@Controller
public class SampleController {
@RequestMapping("/hello")
public String getListaUtentiView(ModelMap map){
map.put("name", "Spring Boot");
return "home";
}
}
5. 编写Application类
@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WebApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(WebApplication.class, args);
}
}
6. 以java application方式运行后,就可以访问http://locahost:8080/hello
注意:在IDE中可以java application方式运行,可以打成war包,需要修改pom中packaging为<packaging>war</packaging>,把war把放入tomcat运行
转载请注明原文地址: https://ju.6miu.com/read-5073.html