@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //將字符串賦值給request對象 request.setAttribute("hello", "hello world"); request.setAttribute("welcome", "<font color='red'>世界歡迎你!</font>"); request.setAttribute("v1", 9); request.setAttribute("v2", 10); //類結構 Group group=new Group(); group.setName("09生科一班"); //數組對象 List users=new ArrayList(); for(int i=0;i<10;i++) { User user=new User(); user.setAge(10); user.setName("李龍生"+i); user.setGroup(group); users.add(user); } //設置到request中 request.setAttribute("users", users); //Map Map map=new HashMap(); map.put("k1", "k1"); map.put("k2", "k2"); map.put("k3", "k3"); map.put("k4", "k4"); request.setAttribute("map", map); request.setAttribute("strTokens", "1,2,3,4,5"); request.getRequestDispatcher("/Jstl_Core.jsp").forward(request, response); }
<body> <h1>測試JSTL核心庫</h1> <br> <li>採用c:out標籤</li><br> hello使用:<c:out value="123"></c:out><br> hello使用:<c:out value ="hello"></c:out><br> hello使用:<c:out value="${hello }"></c:out><br> hello使用:<c:out value="${hello123 }" default="沒有值"></c:out><br> hello使用:<c:out value="${hello123 }" >沒有值</c:out><br> hello使用:<c:out value="${welcome}" ></c:out><br> hello使用(escapeXml):<c:out value="${welcome}" escapeXml="true"></c:out><br> hello使用:${welcome} //測試大於、小於符號 <li>condition action sample</li> <c:if test="${v1 lt v2 }"> v1小於v2 </c:if> <p> <li>c:when,c:choose,c:otherwise標籤</li> <c:choose> <c:when test="${v1 gt v2 }"> v1大於v2 </c:when> <c:otherwise> v1小於v2 </c:otherwise> </c:choose> <p> <li>演示循環控制標籤forEach</li> <table border="1"> <tr> <td>用戶名:</td> <td>年齡:</td> <td>班級:</td> </tr> //選擇標籤 <c:choose> //條件標籤 <c:when test="${empty users }"> <tr> <td colspan="3">沒有符合條件的數據!</td> </tr> </c:when> //不然執行這裏面內容 <c:otherwise> <c:forEach items="${users}" var="user"> <tr> <td>${user.name }</td> <td>${user.age }</td> <td>${user.group.name }</td> </tr> </c:forEach> </c:otherwise> </c:choose> </table> //循環map裏面的鍵值對 <li>Map循環</li> <c:forEach items="${map }" var="entry"> ${entry.key },${entry.value }<br> </c:forEach> //將某個字符串,用指定的符號分割 <h1>c:Tokens標籤</h1> <c:forTokens items="${strTokens }" delims="," var="a"> ${a }<br> </c:forTokens> //將某個頁面導入到當前頁 <h2>c:import標籤</h2> <c:import url="http://localhost:8080/drp4.5/login.jsp"></c:import> <h2>c:url,c:param標籤</h2> <c:url value="http://localhost:8080/drp4.5/login.jsp" var="u"> <c:param name="userId" value="long"></c:param> </c:url> ${u } </body>
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setAttribute("today", new Date()); request.setAttribute("n", 1234567.234); request.setAttribute("n", 0.01234); request.getRequestDispatcher("/jstl_fmt.jsp").forward(request, response); }
<body> <h1>test format date tag</h1> today(default)<fmt:formatDate value="${today }"/><br> today(type=date)<fmt:formatDate value="${today }" type="date"/><br> today(type=time)<fmt:formatDate value="${today }" type="time"/><br> today(type=both)<fmt:formatDate value="${today }" type="both"/><br> today(dateStype=short)<fmt:formatDate value="${today }" dateStyle="short"/><br> today(dateStype=full)<fmt:formatDate value="${today }" dateStyle="full"/><br> today(dateStype=long)<fmt:formatDate value="${today }" dateStyle="long"/><br> today(pattern="yyyy/mm/dd hh:mm:ss")<fmt:formatDate value="${today }" pattern="yyyy/mm/dd hh:mm:ss" var="u"/><br> ${u } <h2>digital format tag</h2> n(default)<fmt:formatNumber value="${n}"></fmt:formatNumber><br> n(pattern="###,###,###.####")<fmt:formatNumber value="${n}" pattern="###,###,###.####"></fmt:formatNumber><br> n(pattern="###,###,###.0000")<fmt:formatNumber value="${n}" pattern="###,###,###.0000"></fmt:formatNumber><br> n(groupingUsed="false")<fmt:formatNumber value="${n}" groupingUsed="false"></fmt:formatNumber><br> n(groupingUsed="false")<fmt:formatNumber value="${n}" maxIntegerDigits="12" minIntegerDigits="10"></fmt:formatNumber><br> n(maxFractionDigits="8" minFractionDigits="5")<fmt:formatNumber value="${n}" maxFractionDigits="8" minFractionDigits="5"></fmt:formatNumber><br> <h3>currency tag</h3> n(type="currency")<fmt:formatNumber value="${n }" type="currency"></fmt:formatNumber> n(type="currency" currencySymbol="$")<fmt:formatNumber value="${n }" type="currency" currencySymbol="$"></fmt:formatNumber> n(type="percent")<fmt:formatNumber value="${n }" type="percent"></fmt:formatNumber> </body>