spring-boot入门(二)使用JSP视图
来源:互联网 发布:哈登总得分最新数据 编辑:程序博客网 时间:2024/05/29 15:40
spring-boot入门(二)使用JSP视图
刚刚接触JAVA的时候,jsp是Javaweb项目的最常用视图。随着前端技术的不断进步,anjular的普及,前后端交互不再是那么的耦合,jsp已经渐渐的远离了我们的视线范围,但是jsp也并非完全不会接触到。下面来看看spring boot如何使用jsp视图。
1.引入相关依赖
- 新建一个maven空项目,在pom.xml继承spring-boot-starter-parent
- 引入spring boot基础组件
- 引入jstl,tomcat-embed-jasper包
我这里是直接在父工程里面继承自spring-boot-starter-parent,子工程pom文件详细依赖如下:
<packaging>war</packaging> <name>spring-boot-practice-jsp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
注意tomcat和jasper的scope是provided,使用自己的tomcat容器而不是spring boot内嵌的。
2.Controller控制层
返回一个ModelAndViews视图,视图名称是我们需要访问的资源路径。
@Controllerpublic class WelcomeController { @Value("${application.message:Hello World}") private String message = "Hello World"; @RequestMapping("/") public ModelAndView welcome() { ModelAndView modelAndView = new ModelAndView("/welcome"); modelAndView.addObject("message", this.message); modelAndView.addObject("time", new Date()); return modelAndView; }}
3.启动主类
启动主类,继承自SpringBootServletInitializer。当我们需要一war包方式发布应用到tomcat容器时,需继承自该类。
@SpringBootApplicationpublic class JspApplicationStarter extends SpringBootServletInitializer { private static final Log LOG = LogFactory.getLog(JspApplicationStarter.class); @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(JspApplicationStarter.class); } public static void main(String[] args) throws Exception { SpringApplication.run(JspApplicationStarter.class, args); LOG.info("spring-boot-practice-jsp is started."); }}
4.配置视图解析器
InternalResourceViewResolver是默认的视图解析器,在application.properties文件里面,配置访问静态资源的前后缀参数(spring.mvc.view.prefix 和 spring.mvc.view.suffix)和端口号。
#variableapplication.message=Hello JasonLin#portserver.port=8089#MVCspring.mvc.view.prefix=/WEB-INF/jspspring.mvc.view.suffix=.jsp
如上配置,代表访问端口号是8089(使用外部容器时无效),资源文件位于/WEB-INF/jsp目录下,文件后缀为.jsp
5.jsp页面
在/WEB-INF/jsp目录下新建一个文件名为welcome.jsp的jsp文件。
<!DOCTYPE html><%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><html lang="en"><body><c:url value="/resources/text.txt" var="url"/><spring:url value="/resources/text.txt" htmlEscape="true" var="springUrl" />Spring URL: ${springUrl} at ${time}<br>JSTL URL: ${url}<br>Message: ${message}</body></html>
做完上面的所有工作后需要将maven install项目。将文件发布到tomcat容器。整个工程的结构目录如下:
6.发布运行
在idea中配置好tomcat并启动项目。tomcat启动配置:
端口为8080,默认路径/
发布运行。
over !
PS:
1、jsp项目需要引入tomcat-embed-jasper和jstl
2、jsp web项目需要发布在Tomcat中以war包运行,直接打jar包,web-inf目录下的资源文件不会打包在jar包中,会使view无法访问(貌似可以通过插件实现).
3、以war包发布应用主类需实现WebApplicationInitializer接口或者继承自SpringBootServletInitializer
4、jasper的scope设置为: provided 否则可能会和tomcat容器里面的冲突。
5、spring.mvc.view.prefix 资源文件前缀
spring.mvc.view.suffix 资源文件后缀
6、相关代码:https://github.com/Json-Lin/spring-boot-practice/tree/master/spring-boot-practice-jsp
- spring-boot入门(二)使用JSP视图
- Spring Boot学习入门篇(二)--使用Spring Boot
- spring-boot入门(二)
- Spring Boot (二) Spring Boot for JSP
- Spring-Boot使用jsp
- Spring Boot 系列入门(二) web工程(识别JSP)
- spring-boot入门(三)前后端分离使用HTML视图
- <spring boot>jsp视图的一个bug
- Spring Boot入门二:使用ThymeLeaf+表单验证
- Spring boot-1(入门使用)
- Spring boot(11) 使用jsp
- 13. Spring Boot 使用JSP
- Spring Boot入门(连接MySQL, Redis 输出JSP)
- Spring boot redis的使用(二)
- Spring-boot 初使用(二)
- Spring Boot学习笔记-整合Jsp (二)
- Spring boot入门系列(二):使用Thymeleaf模板引擎开发Web应用简单示例
- Spring Boot入门使用教程
- PHP 并发下的进程间通信
- 大数据算法
- BeautifulSoup爬虫之保存CSV文件
- Spring Security开发安全的REST服务
- 北理工的恶龙
- spring-boot入门(二)使用JSP视图
- C#语法之委托和事件
- spring与dubbo分布式REST服务开发实战
- 友盟分享的工具类....主要是分享链接
- javascript
- switch语句在循环中使用break语句不会中断循环
- Android资料整理
- 登录成功跳转回原来的界面
- SSM框架实战应用教程(基础篇+web开发篇)