jstl标签库(1)

jstl标签库(1)


jstl标签库的配置
 * 将jstl.jar和standard.jar拷贝到WEB-INF/lib下(如果使用el表达式,不用拷贝这两个jar)
 
 注意:jstl必须在能够支持j2ee1.4/servlet2.4/jsp2.0版本上的容器才能运行,这个环境
      是目前较为常用的环境

     
标签库的使用
 * 采用taglib指令引入
 <%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core"%> 
 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>


jstl核心库(core)


Action
package  com.lanjh.struts.action;

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.lanjh.struts.po.Group;
import  com.lanjh.struts.po.User;

public   class  JstlCoreAction  extends  Action {

    @Override
    
public  ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            
throws  Exception {
        request.setAttribute(
" hello " " hello world " );
        
        
// html文本
        request.setAttribute( " bj " " <font color='red'>北京欢迎您</font> " );
        
        
// 测试条件控制标签
        request.setAttribute( " v1 " 1 );
        request.setAttribute(
" v2 " 2 );
        request.setAttribute(
" v3 " new  ArrayList());
        request.setAttribute(
" v4 " " test " );
        
        
        
// 测试c:forEach
        Group group  =   new  Group();
        group.setName(
" 尚学堂 " );
        
        List userList 
=   new  ArrayList();
        
for  ( int  i = 0 ; i < 10 ; i ++ ) {
            User user 
=   new  User();
            user.setUsername(
" user_ "   +  i);
            user.setAge(
18 + i);
            user.setGroup(group);
            userList.add(user);
        }
        
        request.setAttribute(
" userlist " , userList);
        
        
// 测试循环输出map
        Map < String, String >  map  =   new  HashMap < String, String > ();
        map.put(
" k1 " " lanjh " );
        map.put(
" k2 " " chenj " );
        request.setAttribute(
" mapvalue " , map);
        
        
// 测试c:forTokens
        request.setAttribute( " strTokens " " 1,2,3,4,5,6 " );
        
        
        
return  mapping.findForward( " success " );
    }
    
}

struts配置
< action  path ="/jstlcore"
            type
="com.lanjh.struts.action.JstlCoreAction" >
            
< forward  name ="success"  path ="/jstl_core.jsp" ></ forward >
        
</ action >

JSP页面
<% @ page language = " java "  import = " java.util.* "  pageEncoding = " utf-8 " %>
<% @ taglib prefix = " c "  uri = " http://java.sun.com/jsp/jstl/core " %>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
    
< head >
        
< title > 测试jstl核心库 </ title >
    
</ head >
    
< body >
        
< h1 >
            测试EL表达式
        
</ h1 >
        
< br >
        
< hr >
        
< li >
            测试c:out
        
</ li >
        
< br >
        hello(default):
        
< c:out  value ="${hello}" ></ c:out >
        
< br >
        hello(default=123):
        
< c:out  value ="${hello}"  default ="123" ></ c:out >
        
< br >
        
<!--  value值为空或者null时,取default的默认值  -->
        hello(default=123):
        
< c:out  value ="${hello1}" > 123 </ c:out >
        
< br >

        bj(default):
        
< c:out  value ="${bj}" ></ c:out >
        
< br >
        bj(escapeXml="true"):
        
< c:out  value ="${bj}"  escapeXml ="true" ></ c:out >
        
< br >
        
<!--  解析xml格式的数据  -->
        bj(escapeXml="false"):
        
< c:out  value ="${bj}"  escapeXml ="false" ></ c:out >
        
< br >
        
< p >
        
< li >
            测试c:set和c:remove
        
</ li >
        
< br >
        
< c:set  value ="123"  var ="temp" ></ c:set >
        temp:${temp }
        
< br >
        
< c:remove  var ="temp"   />
        temp:${temp }
        
< br >

        
< p >
        
< li >
            测试条件控制标签c:if
        
</ li >
        
< br >
        
< c:if  test ="${v1 lt v2}"  var ="v" >
        v1小于v2
< br >
        v=${v }
    
</ c:if >
        
< c:if  test ="${empty v3}" >
        v3为空
< br >
        
</ c:if >
        
< c:if  test ="${!empty v4}" >
        v4不为空
< br >
        
</ c:if >

        
< p >
        
< li >
            测试条件控制标签c:choose,c:when,c:otherwise
        
</ li >
        
< br >

        
< 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 >

        
< p >
        
< li >
            测试循环控制标签c:forEach
        
</ li >
        
< br >
        
< table  border ="1" >
            
< 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 ="u" >
                        
< tr >
                            
< td >
                                ${u.username }
                            
</ td >
                            
< td >
                                ${u.age }
                            
</ td >
                            
< td >
                                ${u.group.name }
                            
</ td >
                        
</ tr >
                    
</ c:forEach >
                
</ c:otherwise >

            
</ c:choose >
        
</ table >

        
< p >
        
< li >
            测试循环控制标签c:forEach,varstatus
        
</ li >
        
< br >
        
< table  border ="1" >
            
< 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 ="red" >
                            
</ c:when >
                            
< c:otherwise >
                                
< tr >
                            
</ c:otherwise >
                        
</ c:choose >
                        
< td >
                            
< c:out  value ="${user.username}"   />
                        
</ td >
                        
< td >
                            
< c:out  value ="${user.age}"   />
                        
</ td >
                        
< td >
                            
< c:out  value ="${user.group.name}"   />
                        
</ td >
                        
</ tr >
                    
</ c:forEach >
                
</ c:otherwise >
            
</ c:choose >
        
</ table >
        
        
< p >
    
< li > 测试循环控制标签c:forEach,普通循环 </ li >< br >
    
< c:forEach  begin ="1"  end ="10" >
        a
< br >
    
</ c:forEach >
    
    
< p >
    
< li > 测试循环控制标签c:forEach,输出map </ li >< br >
    
< c:forEach   items ="${mapvalue}"  var ="v" >
        ${v.key }=${v.value }
< br >
    
</ c:forEach >
    
    
    
< p >
    
< li > 测试循环控制标签c:forTokens </ li >< br >
    
< c:forTokens  items ="${strTokens}"  delims =","  var ="v" >
        ${v }
< br >
    
</ c:forTokens >
    
    
    
< p >
    
< li > 测试c:catch </ li >< br >
    
<%
        try {
            
Integer .parseInt( " asdfsdf " );
        }catch(Exception e) {
            out.println(e.getMessage());
        }    
    
%>
    
< p >
    
< c:catch  var ="exinfo" >
        
<%
            
Integer .parseInt( " asdfsdf " );
        
%>
    
</ c:catch >
    ${exinfo }
< br >
    
    
< p >
    
< li > 测试c:import </ li >< br >
    
<!--  引入外部url地址  -->
    
< c:import  url ="http://localhost:8080/struts_login" />
    
< p >
    
< li > 测试c:url和c:param </ li >< br >
    
<!--  给url后面加参数  -->
    
< c:url  value ="http://localhost:8080/drp/sysmgr/user_add.jsp"  var ="v" >
        
< c:param  name ="username"  value ="Jack" />
        
< c:param  name ="age"  value ="20" />
    
</ c:url >
    ${v }
< br >


    
        
    
</ body >
</ html >

你可能感兴趣的:(jstl标签库(1))