筆記-JavaWeb學習之旅14

JSTL:JavaServer Pages Tag Library JSP標準標籤庫

if標籤

<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--導入標籤庫,使用標籤--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--if標籤中的屬性test是必需要寫的,接受boolean表達式,--%>
<%--表達式爲true則顯示if 標籤體內容,false則不顯示,--%>
<%--通常狀況下,test屬性值會結合el表達式一塊兒使用--%>
    <%
        List list = new ArrayList();
        list.add("aaa");
        request.setAttribute("list",list);

        request.setAttribute("number",5);
    %>
    <c:if test="${not empty list}">
        遍歷集合....
    </c:if>
<c:if test="${number % 2 == 0}">
    ${number}爲偶數
</c:if>

<c:if test="${number % 2 != 0}">
    ${number}奇數
</c:if>

</body>
</html>

choose標籤

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        request.setAttribute("number",8);
    %>
    <%--choose標籤取出數字--%>
    <%--when標籤作數字的判斷--%>
    <%--otherwise標籤作其餘狀況的 聲明--%>
    <c:choose>
        <c:when test="${number == 1}">星期一</c:when>
        <c:when test="${number == 2}">星期二</c:when>
        <c:when test="${number == 3}">星期三</c:when>
        <c:when test="${number == 4}">星期四</c:when>
        <c:when test="${number == 5}">星期五</c:when>
        <c:when test="${number == 6}">星期六</c:when>
        <c:when test="${number == 7}">星期天</c:when>
        <c:otherwise>數字輸入有誤</c:otherwise>
    </c:choose>

</body>
</html>

foreach完成重複的操做

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%--完成重複的操做--%>
    <%--foreach:至關於java代碼的for語句--%>
    <%--begin:開始值   end:結束值(包含)     var:臨時變量--%>
    <%--step:步長     varStatus循環狀態的對象(index :索引 count:循環次數,從1開始)--%>
    <c:forEach begin="1" end="10" var="i" step="2" varStatus="s">
        ${i} ${s.index} ${s.count}<hr>
    </c:forEach>
</body>
</html>

img

foreach完成遍歷容器

<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--遍歷容器--%>
<%--屬性: items:容器對象  var:容器中元素的臨時變量      varStatus--%>
<%
    List list = new ArrayList();
    list.add("aaa");
    list.add("bbb");
    list.add("ccc");
    request.setAttribute("list",list);
%>
<c:forEach items="${list}" var="str" varStatus="s">
    ${s.index} ${s.count} ${str}<br>
</c:forEach>
</body>
</html>

img

JSTL練習

需求:在request域中有一個存有User對象的集合,須要使用jstl+el將list集合數據展現到jsp頁面的表格table中html

package com.data.jsp;
import java.text.SimpleDateFormat;
import java.util.Date;

public class User {
    private String name;
    private int age;
    private Date birthday;

    public User() {
    }

    public User(String name, int age, Date birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    //須要顯示年月日的日期格式須要建立一個方法
    public String getBirStr(){
        //格式化日期
        if(birthday !=null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            //返回字符串
            return sdf.format(birthday);
        }else{
            return "";
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="com.data.jsp.User" %>
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        List list = new ArrayList();
        list.add(new User("張三",23,new Date()));
        list.add(new User("李四",24,new Date()));
        list.add(new User("王五",25,new Date()));
        request.setAttribute("list",list);
    %>

<table border="1" width="500" align="center">
    <tr>
        <th>編號</th>
        <th>姓名</th>
        <th>年齡</th>
        <th>生日</th>
    </tr>
    <c:forEach items="${list}" var="user" varStatus="s">
        <tr>
            <td>${s.count}</td>
            <td>${user.name}</td>
            <td>${user.age}</td>
            <td>${user.birStr}</td>
        </tr>
    </c:forEach>
</table>

</body>
</html>

img

三層架構:軟件設計架構

1.界面層(web層)(表示層):用戶看獲得的界面。用戶能夠經過界面上的組件和服務器進行交互,對應SpingMvc框架java

2.業務邏輯層(service):處理業務邏輯的,對應Spring框架web

3.數據訪問層(dao層):操做數據存儲文件,對應MyBatis框架服務器

相關文章
相關標籤/搜索