JSP标签
最近无聊,在网上看到很多JSP的博客,各种如何标签定义,恶心到不行...一怒之下写了几百行代码,常用的标签都写了实际使用的案例,效果图来一张
大致就这样了,下面贴代码
JSP页面代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.dfs.pojo.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>JSP标签</title>
</head>
<style type="text/css">
#ctrlDiv{
width: 100% ;
height: 1500px ;
background-color: yellow ;
}
.tagDiv1{
width: 40% ;
height: 1500px ;
float: left ;
background-color: #E8F2FE ;
}
.tagDiv2{
width: 30% ;
height: 1500px ;
float: left ;
background-color: #F0F0F0 ;
}
.tagDiv3{
width: 30% ;
height: 1500px ;
float: left ;
background-color: #99BFF1 ;
}
h3{
color: red ;
}
</style>
<body>
<%
int [] ages = {1,2,3,4,5,6} ;
List<String> list = new ArrayList<String>() ;
list.add("hello1") ;
list.add("hello2") ;
list.add("hello3") ;
// 重复内容不被遍历
Set<String> set = new TreeSet<String>(); // Set
set.add("One");
set.add("One");
set.add("Two");
set.add("Three");
set.add("Set");
Map<String, String> map = new HashMap<String, String>(); // Map
map.put("1", "黄彪");
map.put("2", "丫头");
map.put("3", "哥哥");
map.put("4", "笨蛋");
List<User> users = new ArrayList<User>(); // JavaBean的List
users.add(new User("黄彪", "xxxxxx"));
users.add(new User("昊天", "xxxxxx"));
users.add(new User("姐姐", "yyyyyy"));
users.add(new User("丫头", "zzzzzz"));
session.setAttribute("users", users); // 添加到session
%>
<div id="ctrlDiv">
<div class="tagDiv1">
<center>
<h1>数据显示C标签</h1>
<h3>C:OUT标签</h3>
<c:out value="${null}" default="没有数据"></c:out><br/>
<c:out value="<要显示的数据对象(未使用转义字符)>" escapeXml="true" default="默认值"/><br/>
<c:out value="<要显示的数据对象(使用转义字符)>" escapeXml="false" default="默认值"/><br/>
<h3>C:SET标签</h3>
<c:set var="salary" scope="session" value="${2000*2}"/>
<c:out value="${salary}"/><br/>
<h3>C:REMOVE标签</h3>
<c:remove var="salary"/>
<c:out value="${salary}" default="Salary数据被删除"/><br/>
<h3>C:CATCH标签</h3>
<c:catch var="catchException">
<% int i = 1 / 0 ; %>
</c:catch>
<c:if test="${catchException!=null}">
异常:${catchException}<br/>
信息:${catchException.message}
</c:if><br/>
<h3>C:IF标签</h3>
<c:set var="mysalary" scope="session" value="${3500*2}"/>
<c:if test="${mysalary>5000}">
我们涨工资了:
<c:out value="${mysalary}"/>
</c:if><br/>
<h3>C:CHOOSE标签</h3>
<c:choose>
<c:when test="${mysalary<0}">
啃老族...
</c:when>
<c:when test="${mysalary<5000}">
月光族...
</c:when>
<c:otherwise>
逆天了...
</c:otherwise>
</c:choose><br/>
<h3>C:IMPORT标签</h3>
<c:import var="data" url="http://www.baidu.com"/>
<c:out value="看代码..."/>
<!-- <c:out value="${data}"></c:out> --><br/>
<h3>C:PARAM实例</h3>
<c:url var="myURL" value="main.jsp">
<c:param name="name" value="Runoob"/>
<c:param name="url" value="www.runoob.com"/>
</c:url>
<a href="/<c:out value="${myURL}"/>">为指定URL发送两个参数</a>
<h3>C:forEach循环遍历</h3>
<c:forEach var="ageArray" items="<%=ages %>">
<c:out value="${ageArray}"/>
</c:forEach><br/>
<c:forEach var="nameList" items="<%=list %>">
<c:out value="${nameList}" />
</c:forEach><br/>
<c:forEach var="nameSet" items="<%=set %>">
<c:out value="${nameSet}" />
</c:forEach>
<table>
<tr>
<th>KEY</th>
<th>VALUE</th>
</tr>
<c:forEach var="entry" items="<%=map %>">
<tr>
<td><c:out value="${entry.key}"/></td>
<td><c:out value="${entry.value}"/></td>
</tr>
</c:forEach>
</table>
<table>
<tr>
<th>姓名</th>
<th>说明</th>
</tr>
<c:forEach var="user" items="${users}">
<tr>
<td><c:out value="${user.name}"/></td>
<td><c:out value="${user.say}"/></td>
</tr>
</c:forEach>
</table>
<h3>c:forTokens标签</h3>
<c:forTokens var="name" items="hello1,hello2,hello3" delims=",">
<c:out value="${name}" />
</c:forTokens>
<c:forTokens var="heList" items="${list}" delims=",">
<c:out value="${heList}" />
</c:forTokens>
<h3>c:REDIRECT标签</h3>
自动跳转到指定的URL
</center>
</div>
<div class="tagDiv2">
<center>
<h1>数据格式FMT标签</h1>
<h3>fmt:formatNumber标签</h3>
<c:set var="balance" value="120000.2309" />
格式化数字 (1): <fmt:formatNumber value="${balance}" type="currency"/><br/>
格式化数字 (2): <fmt:formatNumber type="number" maxIntegerDigits="3" value="${balance}" /><br/>
格式化数字 (3): <fmt:formatNumber type="number" maxFractionDigits="3" value="${balance}" /><br/>
格式化数字 (4): <fmt:formatNumber type="number" groupingUsed="false" value="${balance}" /><br/>
格式化数字 (5): <fmt:formatNumber type="percent" maxIntegerDigits="3" value="${balance}" /><br/>
格式化数字 (6): <fmt:formatNumber type="percent" minFractionDigits="10" value="${balance}" /><br/>
格式化数字 (7): <fmt:formatNumber type="percent" maxIntegerDigits="3" value="${balance}" /><br/>
格式化数字 (8): <fmt:formatNumber type="number" pattern="###.###E0" value="${balance}" /><br/>
美元 :<fmt:setLocale value="en_US"/>
<fmt:formatNumber value="${balance}" type="currency"/><br/>
<h3>fmt:parseNumber标签</h3>
<fmt:parseNumber var="i" type="number" value="${balance}" />
数字解析 (1) :<c:out value="${i}"/><br/>
<fmt:parseNumber var="j" integerOnly="true" type="number" value="${balance}" />
数字解析 (1) :<c:out value="${j}"/><br/>
<h3>fmt:formatDate标签</h3>
<c:set var="now" value="<%=new java.util.Date() %>"></c:set>
普通输出:<c:out value="${now}"/><br/>
TIME格式:<fmt:formatDate type="time" value="${now}" /><br/>
DATE格式:<fmt:formatDate type="date" value="${now}" /><br/>
BOTH格式:<fmt:formatDate type="both" value="${now}" /><br/>
<fmt:formatDate type="both" dateStyle="short" timeStyle="short" value="${now}" /><br/>
<fmt:formatDate type="both" dateStyle="medium" timeStyle="medium" value="${now}" /><br/>
<fmt:formatDate type="both" dateStyle="long" timeStyle="long" value="${now}" /><br/>
格式化日期:
<fmt:formatDate pattern="yyyy-MM-dd" value="${now}"/><br/>
<fmt:formatDate pattern="yyyy-MM-dd hh:mm:ss" value="${now}"/><br/>
<fmt:formatDate pattern="yyyy/MM/dd" value="${now}"/><br/>
<h3>fmt:parseDate标签</h3>
<c:set var="myDate" value="20-10-2015" />
<fmt:parseDate value="${myDate}" var="parsedEmpDate" pattern="dd-MM-yyyy" />
解析后的日期为: <c:out value="${parsedEmpDate}" /><br/>
<h3>fmt:bundle标签</h3>
<fmt:bundle basename="com.dfs.pojo.ExcmpleList" prefix="count.">
<fmt:message key="one" />
<fmt:message key="two" />
<fmt:message key="three" />
</fmt:bundle><br/>
<fmt:bundle basename="com.dfs.pojo.ExcmpleList">
<fmt:message key="count.one" />
<fmt:message key="count.two" />
<fmt:message key="count.three" />
</fmt:bundle><br/>
<h3>fmt:setBundle标签</h3>
<fmt:setBundle basename="com.dfs.pojo.ExcmpleList" var="lang"/>
<fmt:message key="count.one" bundle="${lang}"/><br/>
<fmt:message key="count.two" bundle="${lang}"/><br/>
<fmt:message key="count.three" bundle="${lang}"/><br/>
<h3>Request请求编码</h3>
UTF-8
<fmt:requestEncoding value="utf-8"/><br/>
<h3>fmt:timeZone标签</h3>
<c:set var="mynow" value="<%=new java.util.Date()%>" />
<table border="1" width="100%">
<tr>
<td width="100%" colspan="2" bgcolor="#0000FF">
<p align="center">
<b>
<font color="#FFFFFF" size="4">Formatting:
<fmt:formatDate value="${mynow}" type="both"
timeStyle="long" dateStyle="long" />
</font>
</b>
</p>
</td>
</tr>
<tr>
<td width="51%">
<c:out value="${zone}" />
</td>
<td width="49%">
<fmt:timeZone value="${zone}">
<fmt:formatDate value="${mynow}" timeZone="${zn}" type="both" />
</fmt:timeZone>
</td>
</tr>
</table>
</center>
</div>
<div class="tagDiv3">
<center>
<h1>JSTL函数</h1>
<h3>判断包含函数</h3>
<c:set var="theString" value="I am from China" />
<c:if test="${fn:contains(theString,'China')}">
找出China
</c:if><br/>
<c:if test="${fn:containsIgnoreCase(theString,'CHINA')}">
找出CHINA
</c:if><br/>
<h3>判断开头结尾函数</h3>
<c:if test="${fn:endsWith(theString,'na')}">
以na结尾
</c:if>
<c:if test="${fn:startsWith(theString,'I')}">
以I开头
</c:if>
<h3>判断长度函数</h3>
<c:out value="${fn:length(theString)}" />
<h3>分割字符串函数</h3>
<c:set var="newStr" value="${fn:split(theString,' ')}" />
<c:set var="strNew" value="${fn:join(newStr, '-')}" />
<c:out value="${strNew}" /><br/>
<h3>大小写转换函数</h3>
${fn:toLowerCase(strNew)}<br/>
${fn:toUpperCase(strNew)}<br/>
<h3>去空格函数</h3>
${fn:trim(theString)}<br/>
<h3>替换函数</h3>
${fn:replace(theString,' ','-')}
</center>
</div>
</div>
</body>
</html>
两个POJO类
package com.dfs.pojo;
public class User {
private String name ;
private String say ;
public User (String name,String say){
this.name = name ;
this.say = say ;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSay() {
return say;
}
public void setSay(String say) {
this.say = say;
}
}
package com.dfs.pojo;
import java.util.ListResourceBundle;
public class ExcmpleList extends ListResourceBundle {
static final Object[][] contents = {
{"count.one", "One"},
{"count.two", "Two"},
{"count.three", "Three"},
};
@Override
public Object[][] getContents() {
return contents;
}
}
代码都是运行过的应该是没有错误的...
祝大家工作顺利,天天开心!