Struts 1 学习笔记-3-2(JSTL核心标签库测试)

本文详细介绍并演示了JavaServer Pages Standard Tag Library (JSTL) 核心标签的使用方法,包括c:out、c:set、c:remove、c:if、c:choose、c:when、c:otherwise、c:forEach等标签的功能与应用实例。

 1.引入以下两个jar包

2.按惯例,先上struts-config.xml,(其中包含了我上一个EL测试的action)

<?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="/jstlel" type="com.codedestiny.struts.JSTLELAction" scope="request">
            
<forward name="success" path="/jstlel.jsp"></forward>
        
</action>
        
<action path="/jstlcore" type="com.codedestiny.struts.JSTLCoreAction" scope="request">
            
<forward name="success" path="/jstlcore.jsp"></forward>
        
</action>
    
</action-mappings>
    
<message-resources parameter="MessageResources" />
</struts-config>

 

3.接下来JSTLCoreAction.java:

package com.codedestiny.struts;

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;

public class JSTLCoreAction extends Action {

    
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        
        request.setAttribute(
"hello""Hello World");
        request.setAttribute(
"cd""<font color='red' size='6'>成都欢迎大家</font>");
        request.setAttribute(
"v1"10);
        request.setAttribute(
"v2"20);
        
        List
<User> users = new ArrayList<User>();
        Group relatedGroup 
= new Group();
        relatedGroup.setName(
"Xihua Univercity");
        
for(int i=1; i<10; i++{
            User u 
= new User();
            u.setUsername(
"user" + i);
            u.setAge(
20 + i);
            u.setRelatedGroup(relatedGroup);
            users.add(u);
        }

        
        Map
<String, Integer> hashMap = new HashMap<String, Integer>();
        hashMap.put(
"key1"1);
        hashMap.put(
"key2"2);
        hashMap.put(
"key3"3);
        hashMap.put(
"key4"4);
        request.setAttribute(
"map", hashMap);
        
            request.setAttribute("users", users);
        request.setAttribute(
"tokenstr""1,2,3,4,5");
        
        
return mapping.findForward("success");
    }


}

 

4.展示页面jstlcore.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
<head>
  
</head>
  
  
<body>
    
<h1>JSTL核心库标签测试</h1>
    
    
<hr>
      
<li>测试c:out</li><br>
    hello(default) : 
<c:out value="${hello}"></c:out><br>
    b(default value) : 
<c:out value="${b}" default="no value"></c:out><br>
    b(default value) : 
<c:out value="${b}">no value</c:out><br>
    cd(default) : 
<c:out value="${cd}"></c:out><br>
    cd(escapeXml=true) : 
<c:out value="${cd}" escapeXml="true"></c:out><br>
    cd(escapeXml=false) : 
<c:out value="${cd}" escapeXml="false"></c:out><br>
    
    
<hr>
    
<li>测试c:set, c:remove</li><br>
    
<c:set var="temp" value="123"></c:set>
    temp : ${ temp } 
<br>
    
<c:remove var="temp"/>
    temp : ${ temp } 
<br>
    
    
<hr>
    
<li>测试条件控制标签c:if</li><br>
    
<c:if test="${v1 < v2}" var="boolean">
        v1 
< v2 ? ${boolean} <br> 
    
</c:if>
    
    
<hr>
    
<li>测试条件控制标签c:choose, c:when, c:otherwise</li><br>
    
<c:choose>
        
<c:when test="${v1 < v2}"> 
            v1 
< v2 <br> 
        
</c:when>
        
<c:otherwise>
            v1 > v2 
<br>
        
</c:otherwise>
    
</c:choose>
    
    
<hr>
    
<li>测试循环访问标签c:forEach</li><br>
    
<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.username}</td>
                        
<td>${user.age}</td>
                        
<td>${user.relatedGroup.name}</td>
                    
</tr>
                
</c:forEach>
            
</c:otherwise>
           
</c:choose>
    
</table>
    
    
<hr>
    
<li>测试循环访问标签c:forEach中varStatus的用法</li><br>
    
<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" varStatus="vs">
                    
<c:choose>
                        
<c:when test="${vs.count % 2 == 0}">
                            
<tr bgcolor="red">
                                
<td>${user.username}</td>
                                
<td>${user.age}</td>
                                
<td>${user.relatedGroup.name}</td>
                            
</tr>
                        
</c:when>
                        
<c:otherwise>
                            
<tr>
                                
<td>${user.username}</td>
                                
<td>${user.age}</td>
                                
<td>${user.relatedGroup.name}</td>
                            
<tr>
                        
</c:otherwise>
                    
</c:choose>
                
</c:forEach>
            
</c:otherwise>
           
</c:choose>
    
</table>
    
    
<hr>
    
<li>测试循环访问标签c:forEach中begin, end, step的用法</li><br>
    
<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" varStatus="vs" begin="1" end="8" step="2">
                    
<c:choose>
                        
<c:when test="${vs.count % 2 == 0}">
                            
<tr bgcolor="red">
                                
<td>${user.username}</td>
                                
<td>${user.age}</td>
                                
<td>${user.relatedGroup.name}</td>
                            
</tr>
                        
</c:when>
                        
<c:otherwise>
                            
<tr>
                                
<td>${user.username}</td>
                                
<td>${user.age}</td>
                                
<td>${user.relatedGroup.name}</td>
                            
<tr>
                        
</c:otherwise>
                    
</c:choose>
                
</c:forEach>
            
</c:otherwise>
           
</c:choose>
    
</table>
    
    
<hr>
    
<li>测试循环访问标签c:forEach,输出一个map</li><br>
    
<c:forEach items="${map}" var="v">
        
<c:out value="${v.key}"/> = <c:out value="${v.value}"/><br>
    
</c:forEach>
    
    
<hr>
    
<li>测试循环访问标签c:forEach,普通标签</li><br>
    
<c:forEach begin="0" end="5">
        aaa
<br>        
    
</c:forEach>
    
    
<hr>
    
<li>测试循环控制标签c:fortokens</li><br>
    
<c:forTokens items="${tokenstr}" delims="," var="v">
        
<c:out value="${v}"></c:out>
    
</c:forTokens>
    
  
</body>
</html>

 

5.最后来几张测试结果的截图:

 

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值