spring boot 与 thymeleaf (4): 基本对象、工具类对象

如果在前台, 我需要获取session中的信息, 或者需要获取url中的参数信息, 是不是需要在后台手动处理好, 然后放到Model中去, 在前台通过${}来取呢?

当然, 这种方式, 是可以的, 但是比较麻烦, 而且, 别人已经考虑到这个了, 我们直接用就可以了.

一. 基本对象

#ctx上下文对象
#vars上下文对象(和#ctx相同, 但是一般用#ctx)
#locale上下文区域设置
#request(仅在Web Contexts中) HttpServletRequest对象
#response(仅在Web Contexts中) HttpServletResponse对象
#session(仅在Web Contexts中) HttpSession对象
#servletContext(仅在Web Contexts中) ServletContext对象

 

1. 数据准备

package org.elvin.learn.springboot.controller;

import org.elvin.learn.springboot.pojo.Book;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.MapUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("thy")
public class ThyController {

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private HttpServletRequest request;

    @GetMapping("index")
    public String index(Model model) {

        Book book = new Book("springmvc", new DateTime().toString("yyyy-MM-dd"), 10000L);
        model.addAttribute("book", book);

        HttpSession session = request.getSession();
        session.setAttribute("session1", "lalala");

        return "thy/index";
    }
}

 

2. request / param

<div class="panel panel-primary">
    <div class="panel-heading">
        <h3 class="panel-title">reqeust</h3>
    </div>
    <div class="panel-body">
        <p th:text="${#request.getContextPath()}"></p>
        <div th:each="item : ${#request.getParameterNames()}">
            <p th:text="|name : ${item}, value : ${#request.getParameter(item)}|"></p>
        </div>
        <hr />

        <p th:text="|size : ${param.size()}, lang: ${param.lang}, arr: ${param.arr}, arr-first: ${param.arr[1]}|"></p>
    </div>
</div>

#request -> HttpServletRequest 对象. 

param : 用来获取请求参数的.  

param可以通过直接点的方式, 来访问参数. 是非常方便的

除了size()方法, 还有 isEmpty() , containsKey('...')两个常用方法

 

2. session 

html:

<div class="panel panel-primary">
    <div class="panel-heading">
        <h3 class="panel-title">session</h3>
    </div>
    <div class="panel-body">
        <p th:text="${#session.getAttribute('session1')}"></p>
        <p th:text="|size : ${session.size()} , value : ${session.session1} |"></p>

        <div th:each="item : ${#session.getAttributeNames()}">
            <p th:text="|name : ${item}, value : ${#session.getAttribute(item)}|"></p>
        </div>
    </div>
</div>

结果:

#session -> HttpSession对象

session和param一样, 都可以通过点的方式来获取session, 除了size()方法, 还有 isEmpty() , containsKey('...')两个常用方法

 

3. 上下文对象 #ctx

ctx主要看 IContext 接口. 

package org.thymeleaf.context;

import java.util.Locale;
import java.util.Set;

public interface IContext {
    Locale getLocale();

    boolean containsVariable(String var1);

    Set<String> getVariableNames();

    Object getVariable(String var1);
}

在后台写入Model的内容, 在前台都可以通过#ctx来获取

 

<div class="panel panel-primary">
    <div class="panel-heading">
        <h3 class="panel-title">上下文对象</h3>
    </div>
    <div class="panel-body">

        <p th:text="${#ctx.getVariable('book').name}"></p>

    </div>
</div>

 

 

在后台传入一个集合或者一个字符串, 在前台使用时, 如果不知道集合,字符串是否为空, 是不是会导致一些问题呢?

那如果会导致问题, 那么在前台是否有方法来解决这些问题呢?

这里需要借助一些工具类, 来辅助判断.

二. 工具类对象

#execInfo有关正在处理的模板的信息
#messages用于在变量表达式中获取外部化消息的方法, 与使用#{...}语法获得的方式相同
#uris转义 URL / URI 部分的方法
#conversions执行配置的转换服务(如果有的话)的方法
#datesjava.util.Date对象的方法: 格式化, 组件提取等
#calendarsjava.util.Calendar对象, 类似于#dates
#numbers用于格式化数字对象的方法
#stringsString对象的方法: contains, startsWith, prepending, appending等
#objects一般对象的方法
#bools布尔评估的方法
#arrays数组的方法
#lists列表的方法
#sets集合的方法
#mapsmap方法
#aggregates在数组或集合上创建聚合的方法
#ids处理可能重复的id属性的方法

具体的使用方法, 可以通过 ctrl + 鼠标左键点击  的方式, 进类里面查看. 

转载于:https://www.cnblogs.com/elvinle/p/8059949.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值