JSTL标签-核心标签库
1.JSTL标签库
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set />用来存放变量
属性:
var表示定义的变量名
value表示存入的值
scope表示存入的容器:page request session application
<c:out />用来取出变量
属性:
value="${timo}" EL表达式取值
default="没找到" 当取不到值时,给一个默认值
escapeXml="true" 是否对取出的值进行Xml解析;默认为true,不进行Xml解析。
<c:if test=""></c:if>
属性:
test表示条件语句,如果为真则执行标签内的语句
<c:choose>
<c:when test="">
语句1
</c:when>
<c:when test="">
语句2
</c:when>
<c:otherwise>
语句3
</c:otherwise>
</c:choose>
属性: test表示条件语句,为真则执行。
choose语句很像java中的switch语句。
<c:forEach items="${list}" var="i">
${i.name}-----${i.price}<br/>
</c:forEach>
属性: items表示取出的集合,var表示当前的循环变量i
用EL表达式 ${i.属性名}可以取出值。(i的属性必须有get方法,不然无法取值)
<h1>foreach begin 循环</h1>
<c:forEach begin="1" end="10" varStatus="i" step="2">
${i.index} ------ ${i.count} <br/>
</c:forEach>
属性: begin表示从哪开始,end表示到哪结束;
varStatus表示当前的循环变量,step表示循环变量i每次加2。
i.index表示当前i的值;i.count表示当前i的记录数(一共有几个i)
上面的运行结果:
1-----1
3-----2
5-----3
7-----4
9-----5
<h1>JSTL学习</h1>
<h3>放变量</h3>
<c:set var="timo" value="<h2>提莫</h2>" scope="page"/>
EL --- ${timo} <br/>
out标签:<c:out value="${timo}" default="没找到" escapeXml="true" />
<c:set var="week" value="4" scope="page"/>
<h3>if判断</h3>
<c:if test="${week== 3}">
开黑
</c:if>
<br/>
<c:choose>
<c:when test="${week==1}">
看延熙攻略
</c:when>
<c:when test="${week==2}">
看电影
</c:when>
<c:when test="${week==3}">
去网吧LOL
</c:when>
<c:otherwise>
睡觉
</c:otherwise>
</c:choose>
<h1>forEach循环</h1>
<%
List equips = new ArrayList();
Equip dlj = new Equip("多兰戒",400);
Equip wj = new Equip("无尽之刃",3400);
Equip srs = new Equip("梅德尔的窃魂卷",1400);
equips.add(dlj);
equips.add(wj);
equips.add(srs);
request.setAttribute("equips", equips);
Map emap = new HashMap();
emap.put("1", dlj);
emap.put("2", wj);
emap.put("3", srs);
request.setAttribute("emap", emap);
%>
<c:forEach items="${equips}" var="equip">
${equip.name}-----${equip.price}<br/>
</c:forEach>
<c:forEach items="${emap}" var="emap">
${emap.key}-----${emap.value.name}-----${emap.value.price}<br/>
</c:forEach>
<h1>foreach begin 循环</h1>
<c:forEach begin="1" end="10" varStatus="i" step="2">
${i.index} ------ ${i.count} <br/>
</c:forEach>
2.JSTL标签库——fmt
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
fmt包中的格式化标签
<h1>测试格式化标签 formatDate formatNumber</h1>
<h2>formatDate</h2>
<%
request.setAttribute("now", new Date());
%>
默认的时间:${now}
<br/>
格式化的时间:<fmt:formatDate value="${now}" pattern="yyyy-MM-dd HH:mm:ss"/>
<h2>formatNumber</h2>
<c:set var="balance" value="12306.2309" />
<p>格式化数字 (1):<fmt:formatNumber value="${balance}" type="currency"/>
<p>格式化数字 (2): <fmt:formatNumber type="number"
maxIntegerDigits="3" value="${balance}" /></p>
<p>格式化数字 (3): <fmt:formatNumber type="number"
maxFractionDigits="3" value="${balance}" /></p>
<p>格式化数字 (4): <fmt:formatNumber type="number"
groupingUsed="false" value="${balance}" /></p>
<p>格式化数字 (5): <fmt:formatNumber type="percent"
maxIntegerDigits="3" value="${balance}" /></p>
<p>格式化数字 (6): <fmt:formatNumber type="percent"
minFractionDigits="10" value="${balance}" /></p>
<p>格式化数字 (7): <fmt:formatNumber type="percent"
maxIntegerDigits="3" value="${balance}" /></p>
<p>格式化数字 (8): <fmt:formatNumber type="number"
pattern="###.###E0" value="${balance}" /></p>
<p>美元 :
<fmt:setLocale value="en_US"/>
<fmt:formatNumber value="${balance}" type="currency"/></p>
fmt包中的国际化支持
需要在项目的src目录下写properties配置文件
文件命名格式:前缀(随便写)_语言_国家代码.properties
例子:
gyx_en_US.properties:
title=Internationalized website
welcome=Welcome to login
username=User Name
pass=Pass Word
submit=Login
gyx_ja_JP.properties:
title=\u56FD\u969B\u5316\u30B5\u30A4\u30C8
welcome=\u767B\u9332\u3092\u6B53\u8FCE\u3059\u308B
username=\u3086\u30FC\u3056\u3081\u3044
pass=\u30D1\u30B9\u30EF\u30FC\u30C9
submit=\u767B\u9332\u3059\u308B
gyx_zh_CN.properties
title=\u56FD\u9645\u5316\u7F51\u7AD9
welcome=\u6B22\u8FCE\u767B\u5F55
username=\u7528\u6237\u540D
pass=\u5BC6\u7801
submit=\u767B\u5F55
jsp页面代码:
<c:choose>
<c:when test="${param.lang == 'zh'}">
<fmt:setLocale value="zh_CN"/>
</c:when>
<c:when test="${param.lang == 'en'}">
<fmt:setLocale value="en_US"/>
</c:when>
<c:when test="${param.lang == 'jp'}">
<fmt:setLocale value="ja_JP"/>
</c:when>
<c:otherwise>
<fmt:setLocale value="zh_CN"/>
</c:otherwise>
</c:choose>
<fmt:setBundle basename="gyx"/>
<h1><fmt:message key="title"/></h1>
<a href="MyJsp.jsp?lang=zh">中文</a> <a href="MyJsp.jsp?lang=en">English</a> <a href="MyJsp.jsp?lang=jp">ほうぶん</a>
<h2><fmt:message key="welcome"/></h2>
<form>
<fmt:message key="username"/>:<input type="text" name="username" />
<fmt:message key="pass"/>: <input type="password" name="password" />
<input type="submit" value="<fmt:message key='submit'/>" />
</form>
response.setCharacterEncoding("utf-8");
//项目真实路径
String realPath = getServletContext().getRealPath("/upload");
//声明disk
DiskFileItemFactory disk = new DiskFileItemFactory();
File file = new File("d://tmp");
if (!file.exists()) { //判断文件是否存在
file.mkdir();
}
disk.setRepository(file); //临时文件目录
//声明一个解析request的servlet
ServletFileUpload upload = new ServletFileUpload(disk);
//解析request
List<FileItem> list = null;
try {
list = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
//声明一个map用于封装信息
Map<String, Object> maps = new HashMap<String, Object>();
for (FileItem f : list) {
if (f.isFormField()) {
//可以读取多个普通的input
String fileName = f.getFieldName();
String value = f.getString("utf-8");
System.out.println(fileName+"="+value);
//放入map集合
maps.put(fileName, value);
}else{
if (f.getSize()<=5120000) { //文件大小不能超过 5M
//说明是一个文件
String fileName = f.getName();//以前的名称
//处理文件名
fileName = fileName.substring(fileName.lastIndexOf("\\") + 1);
maps.put("oldName", fileName);
// 修改名称
String extName = fileName.substring(fileName.lastIndexOf("."));
String newName = UUID.randomUUID().toString().replace("-", "")
+ extName;
// 保存新的名称
maps.put("newName", newName);
try {
f.write(new File(realPath+"/"+fileName));
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("文件名是:" + fileName);
System.out.println("文件大小是:" + f.getSize());
maps.put("size", f.getSize());
f.delete();
}else{
System.out.println("文件超过5M !!!");
response.sendRedirect("upload.jsp");
}
}
}
本文深入探讨了JSTL(JavaServer Pages Standard Tag Library)标签库的使用,包括核心标签库和fmt包的功能。详细讲解了<c:set>、<c:out>、<c:if>、<c:choose>、<c:forEach>等标签的属性和用法,以及如何使用fmt包进行日期和数字的格式化,并支持国际化。
5378

被折叠的 条评论
为什么被折叠?



