JSTL常用部分
来源:互联网 发布:dev.mysql.com 编辑:程序博客网 时间:2024/05/22 17:12
主页<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><Title>JSTL</Title></head><body><h1>在Tomcat下,JSTL在项目中的web.xml中必须使用以下作为头部<br/> 以下是其头部<br/> <pre ><code><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> </code></pre></h1><a href="MyJsp1.jsp">流程控制,遍历,输出,赋值</a><br/><a href="MyJsp2.jsp">导入,跳转</a></body></html>
<%--流程控制,遍历,输出,赋值部分 Created by IntelliJ IDEA. User: admin Date: 2017/9/3 Time: 10:40 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" import="java.util.*,com.dixin.vo.*" language="java" pageEncoding="UTF-8" %><%@taglib prefix="xin" uri="http://java.sun.com/jsp/jstl/core" %><%@taglib prefix="di" uri="http://java.sun.com/jsp/jstl/fmt" %><html><head> <title>Title</title></head><> <h2 align="center"> 演示out赋值属性 </h2> <% session.setAttribute("test_session","测试session"); %> <% request.setAttribute("test_request","测试request"); %> <hr/> <font color="#408080"><font color="#c97356"><strong>获得一个字符串</strong> </font> </font> <%out.println("测试");%> <xin:out value="测试"/> <hr/> <font color="#408080"><font color="#c97356"><strong>获得session属性</strong> </font> </font> <%out.println(session.getAttribute("test_session"));%> <xin:out value="${sessionScope.test_session}"></xin:out> <xin:out value="${test_session}"></xin:out> ${sessionScope.test_session} ${test_session} <hr/> <font color="#408080"><font color="#c97356"><strong>获得request属性</strong> </font> </font> <%out.println(request.getAttribute("test_request"));%> <xin:out value="${requestScope.test_request}"/> <xin:out value="${test_request}"></xin:out> ${requestScope.test_request} ${test_request} <hr> <font color="#408080"><font color="#c97356"><strong>输出一个默认值</strong> </font> </font> <xin:out value="${kjsdhakdakdsajkld}" default="这个值不存在"></xin:out> <hr> <h2 align="center"> 演示set赋值属性 </h2> <hr> <font color="#408080"><strong>设置userName的属性为邸鑫,然后输出这个属性值:</strong> </font> <% String username1="very"; %> <xin:set var="username" value="邸鑫"/> <%out.println(username1);%> <xin:out value="${username}"/> <hr> <font color="#408080"><strong>设置password的属性,属性值在Body中,然后输出这个属性值:</strong> </font> <xin:set var="password"> 123456 </xin:set> <xin:out value="${password}"/> <hr> <jsp:useBean id="user" class="com.dixin.vo.User"/> <font color="#408080"><strong>设置JavaBean的属性,然后输出这个属性值:</strong></font> <% user.setUsername("123"); %> <xin:set target="${user}" value="邸鑫" property="username"/> <xin:set target="${user}" property="password"> 123456 </xin:set> <jsp:setProperty name="user" property="username" value="邸鑫"/> 用户名: <xin:out value="${user.username}"/> 密 码: <xin:out value="${user.password}"/> <!--jsp就取jsp jstl就取jstl--> <jsp:setProperty name="user" property="password"/> <hr> <xin:set value="1000" scope="application" var="maxUser"/> <% application.setAttribute("maxUser2","2000"); %> <h2 align="center"> 演示remove </h2> <hr> <font color="#8000ff"><strong>在没有调用remove之前,有这些参数值:</strong> </font> maxUser=<xin:out value="${maxUser}"/> <hr> <font color="#8000ff"><strong>在调用remove之后,这些参数值为:</strong> </font> <xin:remove var="maxUser" scope="application"/> <% application.removeAttribute("maxUser2"); %> maxUser=<xin:out value="${maxUser}"/> <hr> <h2 align="center"> 演示if标签: </h2> <hr> <font color="#8080ff"><strong>演示if标签:</strong></font> <xin:set target="${user}" value="16" property="age"/> <% if(user.getAge()<18) { out.println("对不起,你的年龄太小不可以访问"); } %> <xin:if test="${user.age<18}"> 对不起,你的年龄太小,不能访问 </xin:if> <hr> <h2 align="center"> 演示choose标签: </h2> <hr> <font color="#ff0000"><strong>演示choose标签:</strong> </font> <xin:set value="55" property="age" target="${user}"/> <xin:choose> <xin:when test="${user.age<=18}"> <font color="red"> 你的年龄是<xin:out value="${user.age}"/>,太小了 </font> </xin:when> <xin:when test="${user.age>18&&user.age<30}"> <font color="green"> 你的年龄是<xin:out value="${user.age}"/>,青年人 </font> </xin:when> <xin:otherwise> <font color="blue"> 你的年龄是<xin:out value="${user.age}"/> </font> </xin:otherwise> </xin:choose> <hr> <h2 align="center"> 演示forEach迭代标签-最简单的迭代: </h2> <% ArrayList<User> list=new ArrayList<>(); for(int i=0;i<5;i++) { User user1=new User(); user1.setAge(59+i); user1.setUsername("dixin"+i); user1.setPassword("119"+i); list.add(user1); } session.setAttribute("list",list); %><table width="200" border="1" align="center"> <tbody> <tr> <td colspan="3" align="center"> <font size="4"><strong> 应用标签 </strong></font><br/></td> </tr> <tr> <th> 用户名 </th> <th> 密码 </th> <th valign="top"> 年龄 <br> </th> </tr> <xin:forEach var="user" items="${sessionScope.list}"> <tr> <td> <xin:out value="${user.username}"/> </td> <td> <xin:out value="${user.password}"/> </td> <td> <xin:out value="${user.age}"/> </td> </tr> </xin:forEach></tbody></table> <div align="center"> </div><table width="200" border="1" align="center"> <tbody> <tr> <td colspan="3" align="center"> <font size="4"><strong>应用嵌入式Java代码</strong> </font> <br /> </td> </tr> <tr> <th> 用户名 </th> <th> 密码 </th> <th valign="top"> 年龄 <br> </th> </tr> <% Iterator<User> it=list.iterator(); while (it.hasNext()) { out.println("<tr>"); User u=it.next(); out.println("<td>"+u.getUsername()+"</td>"); out.println("<td>"+u.getPassword()+"</td>"); out.println("<td>"+u.getAge()+"</td>"); out.println("</tr>"); } %> </tbody></table><hr> <h2 align="center"> 演示forEach迭代标签-迭代固定的次数: </h2> <% for(int i=0;i<10;i++) { out.println(i); } %> <xin:forEach var="i" begin="1" end="10"> <xin:out value="${i}"/> </xin:forEach> <h2 align="center"> 演示forEach迭代标签-迭代固定的次数,并改变步长: </h2> <xin:forEach var="i" begin="1" end="10" step="3"> <xin:out value="${i}"/> </xin:forEach> <h2 align="center"> 演示forEach迭代标签-指定集合中的迭代范围: </h2><table width="200" border="1" align="center"> <tbody> <tr> <td colspan="3" align="center"> <font size="4"><strong>用户列表</strong> </font> <br /> </td> </tr> <tr> <th> 用户名 </th> <th> 密码 </th> <th valign="top"> 年龄 <br> </th> </tr> <xin:forEach var="user" begin="2" end="6" step="3" items="${list}"> <tr> <td> <xin:out value="${user.username}"/><br/> </td> <td> <xin:out value="${user.password}"/><br/> </td> <td> <xin:out value="${user.age}"/> </td> </tr> </xin:forEach> </tbody></table> <h2 align="center"> 演示forEach迭代标签-迭代的状态: </h2><table width="200" border="1" align="center"> <tbody> <tr> <td colspan="7" align="center"> <font size="4"><strong>用户列表</strong> </font> <br /> </td> </tr> <tr> <th> 用户名 </th> <th> 密码 </th> <th > 年龄 </th> <th> 位置 </th> <th> 数量 </th> <th> 首次 </th> <th> 末次 </th> </tr> <xin:forEach var="user" items="${list}" varStatus="status"> <tr> <td> <xin:out value="${user.username}" /><br> </td> <td> <xin:out value="${user.password}" /><br> </td> <td> <xin:out value="${user.age}" /><br> </td> <td> <xin:out value="${status.index}" /><br> </td> <td> <xin:out value="${status.count}" /><br> </td> <td> <font color="red"> <xin:choose> <xin:when test="${status.first}"> <i><xin:out value="${status.first}"/></i> </xin:when> <xin:otherwise> <xin:out value="${status.first}"/> </xin:otherwise> </xin:choose> </font> </td> <td> <font color="blue"> <xin:choose> <xin:when test="${status.last}"> <i><xin:out value="${status.last}"/></i> </xin:when> <xin:otherwise> <xin:out value="${status.last}"/> </xin:otherwise> </xin:choose> </font> </td> </tr> </xin:forEach> </tbody></table> <h2 align="center"> 演示forEach迭代标签-迭代不同类型的数据Java版 </h2> <% int[] myIntArray = new int[] { 1, 2, 3, 4, 5, 65, 34 }; request.setAttribute("myIntArray", myIntArray); String[] myStringArray = new String[] { "我", "是", "一", "名", "教", "育","公","司", "的", "员", "工" }; request.setAttribute("myStringArray", myStringArray); Vector v=new Vector(); v.add("我"); v.add("们"); v.add("在"); v.add("学"); v.add("习"); Enumeration en=v.elements();//找到所有元素 request.setAttribute("myEnumeration",en); request.setAttribute("myVector",v); HashMap h=new HashMap(); h.put("user1","123"); h.put("user2","456"); h.put("user3","789"); h.put("user4","134"); request.setAttribute("myHashMap",h); %> <h3 align="center"> 演示weikun:forEach迭代标签-迭代不同类型的数据Jstl版 </h3> <xin:forEach var="t" items="${myIntArray}"> <xin:out value="${t}" /> </xin:forEach><br/> <xin:forEach var="s" items="${myStringArray}"> <xin:out value="${s}" /> </xin:forEach><br/> <xin:forEach var="item" items="${myEnumeration}" > <xin:out value="${item}" /> </xin:forEach><br/> <xin:forEach var="v1" items="${myVector}" > <xin:out value="${v1}" /> </xin:forEach><br/> <xin:forEach var="map" items="${myHashMap}"> <xin:out value="${map.key}"/>:<xin:out value="${map.value}"/> </xin:forEach> <xin:forEach var="token" items="red,blue,green,yellow"> <xin:out value="${token}"/> </xin:forEach> <hr/> <h2 align="center"> 演示formatNumber </h2> <di:formatNumber value="2000" pattern="#,###.00"/> <hr/> <h2 align="center"> 演示formatDate </h2> <hr/> <jsp:useBean id="now" class="java.util.Date"/> <di:formatDate value="${now}" var="result" type="both" pattern="yyyy年MM月dd日 hh:mm:ss"/> ${result} <hr/> <h2 align="center"> 演示forToken </h2> <hr/> <xin:set var="names" value="A&B&C&D"/> <xin:forTokens items="${names}" delims="&" var="name" varStatus="status"> ${status.count}.${name} </xin:forTokens> <hr/></body></html>
导入跳转部分<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8"%><%@taglib prefix="xin" uri="http://java.sun.com/jsp/jstl/core" %><html><head> <title>URL相关标签</title></head><body> <h2 align="center"> 演示import标签 </h2> <hr> <font color="#c97356"><strong>演示绝对路径,自动包括了MyJsp的内容</strong> </font> <%-- 块缩进布局,标记可以在屏幕上用分离的块显示大段的引用--%> <blockquote><!-- URL 可相对也可绝对 --> <xin:import url="pic.jsp"></xin:import> <jsp:include page="pic.jsp"></jsp:include> </blockquote> <hr> <h2 align="center"> 演示redirect标签 </h2> <font color="#c97356"><strong>演示redirect标签</strong> </font> <!-- 可打开注释演示 --> <% //response.sendRedirect("success.jsp?username=hah"); %> <xin:redirect url="success.jsp"> <xin:param value='<%=java.net.URLEncoder.encode("中文","utf-8")%>' name="username"></xin:param> </xin:redirect></body></html>
阅读全文
1 0
- JSTL常用部分
- JSP部分,学习JSTL
- jstl部分标签整理
- JSTL常用方法
- JSTL常用标签
- JSTL 常用标签介绍
- jstl常用代码
- jstl常用标签
- JSTL常用标签
- jstl 常用方法
- JSTL标签常用
- JSTL常用函数
- JSTL常用标签用法
- JSTL常用表达式
- JSTL常用标签
- JSTL常用标签汇总
- JSTL常用标签汇总
- JSTL常用标签
- 基于Unity3D的移动端摇一摇功能的实现
- 创建maven Web项目
- HTTP消息
- JAVA命名规范
- 第五章练习
- JSTL常用部分
- java web文件的上传
- jsp中的js时间传0开头的值会出现转码问题
- 使用Phabricator做为Code Review工具
- [Java并发编程实战]基础知识
- Aerosolve 简介
- 机器学习系列(一)Windows 10下机器学习Python环境搭建
- Java中sleep,wait,yield,join的区别
- break语句的运用