SpringBoot整合SpringMVC

添加依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

使用JSP视图

SpringBoot官方建议我们使用Thymeleaf模板引擎取代JSP视图,如果我们就想使用JSP怎么办呢?可以,做好下面几步配置就可以了

第一步:修改pom.xml中的打包方式为war

<project>
     ...
    <packaging>war</packaging>
     ...
</project>

第二步:添加JSP依赖

<!--配置支持jsp-->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

第三步:配置InternalResourceViewResolver参数

spring:
  mvc:
    view:
      prefix: /WEB-INF/ #配置JSP文件前缀
      suffix: .jsp      #配置JSP页面后缀

注:
1、jsp页面必须放到webapp目录或webapp/WEB-INF/下
2、如果使用了jsp作为视图,则工程只能打成war包部署(因为jar包不会将webapp目录下的东西打进去)

放行静态资源

SpringBoot默认会将classpath:/static/作为静态资源处理, 我们可以通过下面的配置重写该参数:

#指定请求url符合什么格式时才会当成静态资源处理
spring.mvc.static-path-pattern=/** 

# 指定静态资源去哪里找
spring.resources.static-locations=classpath:/static/ 

默认值是:spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

这两个属性其实就相当于在xml中配置的

<mvc:resources mapping="/**" location="/static/" />

返回JSON格式数据

SpringBoot默认内部已经提供了对以下三种JSON类库的支持:

  • Gson
  • Jackson
  • JSON-B

Jackson是默认的JSON转换库

要自动以Jackson转换JSON的参数,可以通过定义ObjectMapper bean来实现,如:

@Bean
public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        return mapper;
}

也可以通过设置spring.jackson.*属性实现

文件上传

点击此处查看

Thymeleaf

添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

表达式

变量表达式

变量表达式即 OGNL 表达式或 Spring EL 表达式(在 Spring 术语中也叫 model attributes)。如下所示:
${session.user.name}

它们将以HTML标签的一个属性来表示:

<span th:text="${book.author.name}"></span>
<li th:each="book : ${books}"></li>
<h1 th:text="${msg}"></h1>

星号表达式

选择表达式很像变量表达式,不过它们用一个预先选择的对象来代替上下文变量容器(map)来执行,如下:
*{customer.name}

被指定的 object 由 th:object 属性定义:

<div th:object="${book}">  
  ...  
  <span th:text="*{title}">...</span>  
  ...  
</div>  

URL表达式

URL 表达式指的是把一个有用的上下文或回话信息添加到 URL,这个过程经常被叫做 URL 重写。
@{/order/list}

URL还可以设置参数:
@{/order/details(id=${orderId})}

相对路径:
@{../documents/report}

让我们看这些表达式:

<form th:action="@{/createOrder}">  
<a href="main.html" th:href="@{/main}">

赋值、字符串拼接

赋值操作

Thymeleaf中赋值使用th:text属性,如:

<p th:text="${message}"> description </p>

字符串拼接

方式一:
<span th:text="'Welcome to our application, ' + ${user.name} + '!'">
方式二:
<span th:text="|Welcome to our application, ${user.name}!|">

分支语句

用法示例:

<a th:if="${myself=='yes'}" > </i> </a>

上面的例子中只有th:if条件成立才显示a标签内容

用法示例:

<a th:unless=${session.user != null} th:href="@{/login}" >Login</a>

th:unless 于 th:if 恰好相反,只有表达式中的条件不成立,才会显示其内容。

循环语句

用法示例:

<tr  th:each="collect,iterStat : ${collects}"> 
   <th scope="row" th:text="${collect.id}">1</th>
   <td >
      <img th:src="${collect.webLogo}"/>
   </td>
   <td th:text="${collect.url}">Mark</td>
   <td th:text="${collect.title}">Otto</td>
   <td th:text="${collect.description}">@mdo</td>
   <td th:text="${terStat.index}">index</td>
</tr>

iterStat称作状态变量,属性有:

  • index:当前迭代对象的 index(从0开始计算)
    count: 当前迭代对象的 index(从1开始计算)
  • size:被迭代对象的大小
  • current:当前迭代变量
  • even/odd:布尔值,当前循环是否是偶数/奇数(从0开始计算)
  • first:布尔值,当前循环是否是第一个
  • last:布尔值,当前循环是否是最后一个
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值