Springboot快速上手- 第九篇 Web应用开发

1 应用开发基础 1.1 静态文件 1: Spring Boot默认为我们提供了静态资源处理,使用 WebMvcAutoConfiguration 来配置各种属性,建议使用默认配置方式,提供的静态资源映射,按照优先级顺序如下...

1 应用开发基础

1.1 静态文件

1: Spring Boot默认为我们提供了静态资源处理,使用 WebMvcAutoConfiguration 来配置各种属性,建议使用默认配置方式,提供的静态资源映射,按照优先级顺序如下:

classpath:/META-INF/resources
classpath:/resources
classpath:/static
classpath:/public

2:可以通过修改spring.mvc.static-path-pattern来修改默认的映射路径
3:注意:如果应用将被打包成jar,就不要使用src/main/webapp文件夹。尽管该文件夹是一个共同的标准,但它仅在打包成war的情况下起作用
4:SpringMVC使用ResourceHttpRequestHandler 来进行资源映射,所以可以通过添加自己的WebMvcConfigurerAdapter并覆写addResourceHandlers方法,来改变这个行为,也就是自定义加载静态文件

1.2 自定义加载静态文件示例

@Configuration
public class MyWebMvcConfig extends WebMvcConfigurerAdapter {
 public void addResourceHandlers(ResourceHandlerRegistry registry) {
   registry.addResourceHandler("/static2/**")
   .addResourceLocations("classpath:/static2/");
 super.addResourceHandlers(registry);
 }
}

1:也可以指定外部的路径,直接addResourceLocations指定即可,示例如下:

把.addResourceLocations("classpath:/static/")变换成
.addResourceLocations("file:D:/my/")

1.3 添加拦截器配置

1:先按照SpringMVC的自定义拦截器的写法,写好拦截器类
2:然后在重写WebMvcConfigurerAdapter中的addInterceptors方法,示例如下:

public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/toLogin","/login");
    super.addInterceptors(registry);
}

1.4 国际化

1:定义国际化资源文件,放到resource下面,默认名字是messages.properties
2:在前面的MyWebMvcConfig中添加读取消息文件的bean

@Bean  
public MessageSource messageSource() {  
   ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();  
   messageSource.setBasename("messages");  
   messageSource.setDefaultEncoding("UTF-8");  
   return messageSource;  
}

3:程序里面就可以直接注入MessageSource并使用 @Autowired
private MessageSource messageSource;
4:一样可以向消息文件中传入参数

1.5 支持的模板引擎

Spring Boot支持多种模版引擎包括:FreeMarker、Groovy、Thymeleaf(官方推荐)

1:JSP技术,Spring Boot官方是不推荐的,原因可能有:
(1)Tomcat只支持war的打包方式,不支持可执行的jar
(2)Jetty 嵌套的容器不支持jsp
2:默认的模板配置路径为:src/main/resources/templates

2 Thymeleaf

2.1 概述

Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎。类似JSP,Velocity,FreeMaker等,它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎。

与其它模板引擎相比,Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用。它的功能特性如下:

1:Spring MVC中@Controller中的方法可以直接返回模板名称,接下来Thymeleaf模板引擎会自动进行渲染
2:模板中的表达式支持Spring表达式语言(Spring EL)
3:表单支持,并兼容Spring MVC的数据绑定与验证机制
4:国际化支持

2.2 基本语法和使用

具体的请参见官网:http://www.thymeleaf.org/
升级到使用thymeleaf 3.x:
<thymeleaf.version>3.0.8.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>

3 Springboot集成Jsp

image.png
image.png
image.png
image.png

3.1 配置依赖

 <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>

注意把前面thymeleaf的配置去掉

3.2 配置application.properties

spring.mvc.view.prefix=/WEB-INF/pages/
spring.mvc.view.suffix=.jsp
然后就可以进行jsp开发了,跟以前一样

3.3 部署运行

1:打成jar运行,会报错,因为jsp没有包含进来
2:打成war运行,可以用java –jar的方式来运行
3:如果要部署到外部服务器中:

(1) 添加一个类来继承SpringBootServletInitializer,类似于web.xml文件配置的方式来启动Spring应用上下文,形如:

@Component
public class ServletInitializer extends SpringBootServletInitializer {
 @Override
 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
   return application.sources(Application.class);
 }
}

(2)修改web.xml的头,设置为servlert3以上,否则默认的jsp用1.2的,默认没有开启el,要jsp2.0以上才可以,形如:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1" metadata-complete="true">

(3)用maven打完包过后,拷贝后缀为.war.original的这个war,当然要修改一下名字,这个是springboot没有包装内嵌服务器的war包

(4)然后就可以去启动外部的tomcat了

  • 发表于 2020-07-09 16:00
  • 阅读 ( 152 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除