还是用代码说明:
jstlAction.java:
package com.keith.jstl; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import com.keith.taglibs.Group; import com.keith.taglibs.User; public class JstlAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // 字符串 request.setAttribute("hello", "hello JSTL"); // HTML文本 request.setAttribute("keith", "<font color='red'>keith</font>"); // 测试条件语句 request.setAttribute("v1", 1); request.setAttribute("v2", 2); request.setAttribute("v3", new ArrayList()); request.setAttribute("v4", "keith V4"); // 对象数组 Group group = new Group(); group.setGroupName("keith"); // 测试foreach List userList = new ArrayList(); for (int i = 0; i < 10; i++) { User u = new User(); u.setName("USER_" + i); u.setAge(10 + i); u.setGroup(group); userList.add(u); } request.setAttribute("userList", userList); // 测试循环输出map Map map = new HashMap(); map.put("k1","v1"); map.put("k2","v2"); request.setAttribute("mapValue", map); //测试c:forTokens request.setAttribute("strTokens","1,2,3,4,5,6,7,8,9"); return mapping.findForward("jstl"); } }
struts-config.xml:
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> <struts-config> <action-mappings> <action path="/jstl" type="com.keith.jstl.JstlAction"> <forward name="jstl" path="/jstl.jsp"></forward> </action> </action-mappings> <message-resources parameter="MessageResources" /> </struts-config>
jstl.jsp:首先指定标签库
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<body> </body>里:
<body> c:out测试 <hr color="orange"> hello(JSTL_default):<c:out value="${hello}" /><br> hello(EL_default):${hello }<br> default(123):<c:out value="${abc}" default="123" /><br><!-- 找不到abc的值,这时的default就有了作用 --> keith(c:out输出):<c:out value="${keith}" /><br> keith(escapeXml="flase"):<c:out value="${keith}" escapeXml="flase"/><br> keith(escapeXml="true"):<c:out value="${keith}" escapeXml="true"/><br> keith(EL输出):${keith} <hr> c:set/remove测试 <hr color="orange"> <c:set var="set1" value="keithSET" /><br> set1:${set1 }<br> <c:remove var="set1"/><br> set1:${set1 }<br> <hr> c:if:条件控制 <hr color="orange"> <c:if test="${v1 lt v2}" var = "v"> v1小于v2<br/> ${v }<br/> </c:if> <c:if test="${empty v3}"> v3为空<br/> </c:if> <c:if test="${empty v4}"> v4为空<br/> </c:if> <hr> 测试C:choose,c:when,c:otherwise <hr color="orange"> <c:choose> <c:when test="v1 lt v2"> v1小于v2<br/> </c:when> <c:otherwise> v1不小于v2<br/> </c:otherwise> </c:choose> <c:choose> <c:when test="${empty v4}"> v4为空<br/> </c:when> <c:otherwise> v4不为空<br/> </c:otherwise> </c:choose> <hr> <h4>测试c:forEach</h4> <table > <tr> <td>姓名</td> <td>年龄</td> <td>组</td> </tr> <c:choose> <c:when test="${empty userList}"> <tr> <td colspan="3">没有符合条件的数据!</td> </tr> </c:when> <c:otherwise> <c:forEach items="${userList}" var="user" varStatus="vs"> <c:choose> <c:when test="${vs.count % 2 == 0 }"> <tr bgcolor="orange"> </c:when> <c:otherwise> <tr> </c:otherwise> </c:choose> <td> <c:out value="${user.name}"></c:out> </td> <td> <c:out value="${user.age}"></c:out> </td> <td> <c:out value="${user.group.groupName}"></c:out> </td> </c:forEach> </c:otherwise> </c:choose> </table> <hr> <h3>测试循环控制标签c:forEach,begin,end,step</h3> <table> <tr> <td>姓名</td> <td>年龄</td> <td>组名</td> </tr> <c:choose> <c:when test="${empty userList}"> <tr> <td colspan="3">没又符合的数据</td> </tr> </c:when> <c:otherwise> <c:forEach items="${userList}" var="user" begin="2" end="5" step="2"> <tr> <td>${user.name }</td> <td>${user.age }</td> <td>${user.group.groupName }</td> </tr> </c:forEach> </c:otherwise> </c:choose> </table> <p></p> <hr> <h4>测试循环控制标签 c:foreach 普通循环</h4> <c:forEach begin="1" end="5"> a<br/> </c:forEach><br> <h4>测试循环控制标签c:forEach,输出map</h4> <c:forEach items="${mapValue}" var="v"> ${v.key } = ${v.value }<br/> </c:forEach> <h4>测试循环控制标签c:forTokens,用于有符号的表达式</h4> <c:forTokens items="${strTokens}" delims="," var="v"> ${v }<br/> </c:forTokens> <h4>测试c:catch</h4> <% try{ Integer.parseInt("keith"); }catch(Exception e){ out.println(e.getMessage()); } %> <p></p> <c:catch var="exinfo"><!-- 获取异常变量 --> <% Integer.parseInt("keith"); %> </c:catch> ${exinfo }<br/><!-- 输出异常变量 --> <li>测试:redirect:直接跳转;context项目名称;url跳转的页面</li><br> <c:redirect context="/struts_test" url="/index.jsp"/> </body>