JSP页面常用操作

2020-03-02  本文已影响0人  HeloWxl

1、获取后端传递的过来的字符串

  @GetMapping("/test")
    public String toDetail( Model model) {
        model.addAttribute("str","我是传递过来的字符串");
        return "test";
    }
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<html>
<head>
    <title>JSP测试</title>
    <link rel="shortcut icon" href="${ctx}/res/img/logo.ico" type="image/x-icon"/>
</head>
<body>
    <p>${str}</p>
    <input value="${str}" type="text">
</body>
</html>

2、获取后端传递的过来的List

  @GetMapping("/test")
    public String toDetail( Model model) {
        model.addAttribute("cartList",cartService.getAllCartByUserId(1));
        return "test";
    }
<table>
    <thead>
    <tr>
        <th>
            图片
        </th>
        <th>
            商品名称
        </th>

        <th>
            数量
        </th>
        <th>
            价格
        </th>
    </tr>
    </thead>
    <tbody>
    <c:forEach items="${cartList}" var="product">
        <tr class="info">
            <td align="center">
                <img src="${ctx}/product/getLocalImg?path=${product.path}" style="width:50px;height:50px">
            </td>
            <td align="center">
                    ${product.productName}
            </td>
            <td align="center">
                    ${product.amout}
            </td>
            <td align="center">
                    ${product.nowPrice}元
            </td>
        </tr>
    </c:forEach>
    </tbody>
</table>

3、获取后端传递的过来的Map

 @GetMapping("/test")
    public String toDetail( Model model) {
        Map<String,Object>  map = new HashMap<>();
        map.put("name","xiaoming");
        map.put("age",20);
        map.put("address","anhui");
        model.addAttribute("map",map);
        return "test";
    }
 <p>
        ${map.name}
    </p>
    <p>
        ${map.age}
    </p>
    <p>
        ${map.address}
    </p>

4、计算

<p>
        <c:set var="sum" value="${a+b}"></c:set>
        a+b =${sum}
    </p>
    <p>
        <c:set var="res" value="${a-b}"></c:set>
        a-b =${res}
    </p>
    <p>
        <c:set var="res1" value="${a*b}"></c:set>
        a*b =${res1}
    </p>
    <p>
        <c:set var="res2" value="${a/b}"></c:set>
        a/b =${res2}
    </p>
 @GetMapping("/test")
    public String toDetail( Model model) {
        model.addAttribute("a",10);
        model.addAttribute("b",20);
        return "test";
    }

-界面


image.png

5、日期格式化

   <p>
        不进行格式化输出:${data}
    </p>
    <p>
        格式化输出:<fmt:formatDate value="${data}" pattern="yyyy-MM-dd hh:mm:ss" />
    </p>
 @GetMapping("/test")
    public String toDetail( Model model) {
        model.addAttribute("data",new Date());
        return "test";
    }
#默认导入的是:
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
#修改后的:加上/jsp即可
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

6、判断是否为空

   @GetMapping("/test")
    public String toDetail( Model model) {
        model.addAttribute("str1","");
        model.addAttribute("str2","Hello JSP");
        return "test";
    }
 <p>
       判断str1是否为空?
        <c:out value="${empty str1}"></c:out>
    </p>
    <p>
        判断str2是否为空?
        <c:out value="${empty str2}"></c:out>
    </p>
上一篇 下一篇

猜你喜欢

热点阅读