JSTL简介

来源:互联网 发布:程序员是吃青春饭的吗 编辑:程序博客网 时间:2024/04/19 15:57
1、JSTL简介:
        JSTL(JSP Standard Tag Library,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,其提供两组标签,一组使用 EL(Expression Language,表达式语言),而另一组支持使用请求时表达式。

2、为什么使用JSTL:
        1)应用程序服务器之间提供了一致的接口,最大程度地提高了WEB应用在各应用服务器之间的移植。
        2)可以编写没有java代码的jsp页面。使JSP页面很容易被WEB设计人员理解,表达更清晰。减少程序出错,是程序容易维护。
        如果要使用JSTL,则必须将jstl.jar和 standard.jar文件放到classpath中,如果你还需要使用XML processing及Database access (SQL)标签,还要将相关JAR文件放到classpath中,这些JAR文件全部存在于下载回来的zip文件中。
3、如何使用JSTL
        1)、Servlet代码(设置一些值到request中)
/**
 * 演示JSTL核心库
 */
public class JstlCoreServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //普通字符串
        request.setAttribute("hello", "hello world!");
        
        //html字符串
        request.setAttribute("welcome", "<font color='red'>世界多美好</font>");
        
        //条件控制标签
        request.setAttribute("v1", 10);
        request.setAttribute("v2", 20);
        
        request.setAttribute("userList", new ArrayList());
        
        //结构
        Group group=new Group();
        group.setName("TGB");
        
        List users=new ArrayList();
        for (int i=0;i<10;i++){
            User user=new User();
            user.setUserName("张三_"+i);
            user.setAge(23);
            user.setGroup(group);
            users.add(user);
        }
        request.setAttribute("users", users);
    
        //map
        Map map=new HashMap();
        map.put("k1", "v1");
        map.put("k2", "v2");
        map.put("k3", "v3");
        map.put("k4", "v4");
        request.setAttribute("map", map);
                
        //forTokens
        request.setAttribute("strTokens", "1#2#3#4#5");
        request.getRequestDispatcher("/jstl_core.jsp").forward(request, response);
    }
}

2)、JSP:使用JSTL接收
<body>
    <h1>测试JSTL核心库</h1>
    <hr>
    <li>采用c:out标签</li><br>
    hello(使用标签):<c:out value="123"></c:out><br>
    hello(使用标签):<c:out value="${hello }"></c:out>
    <br>
    <br>
    hello(default):${hello123 }<br>
    hello(使用缺省值):<c:out value="${hello123 }" default="没有值"></c:out> <br>
    hello(使用缺省值):<c:out value="${hello123 }" >没有值</c:out> <br>
    hello(使用缺省值):<c:out value="${hello }" >没有值</c:out> <br>
    
    <br>
    <br>
    welcome(使用EL表达式):${welcome }<br>
    welcome(使用标签,escapeXml):<c:out value="${welcome }" escapeXml="true"></c:out><br>
    welcome(使用标签,escapeXml):<c:out value="${welcome }" escapeXml="false"></c:out>
  
    <br>
    <hr>
    <li>测试c:set,c:remove</li><br>
    <c:set value="root" var="userid"/>
    userid:${userid }<br>
    <c:remove var="userid"/>
    userid:${userid }<br>
    
    <hr>
    <li>条件控制标签c:if</li><br>
    <c:if test="${v1 lt v2 }">
        v1小于v2
    </c:if>
    
    <br>
    <li>条件控制标签c:choose ,c:when,c:otherwise</li><br>
    <c:choose>
        <c:when test="${v1 gt v2 }">
            v1 大于v2<br>
        </c:when>
        <c:otherwise>
            v1小于v2<br>
        </c:otherwise>
    </c:choose>
    
    <c:choose>
        <c:when test="${empty userList }">
            没有符合条件的数据<br>
        </c:when>
        <c:otherwise>
            存在用户数据
        </c:otherwise>
    </c:choose>
    
    <p>
    <li>演示循环控制变迁:forEach(此处使用循环方式)</li><br>
    <h3>采用jsp脚本显示</h3>
    <table border="1">
        <tr>
            <td>用户名称</td>
            <td>年龄</td>
            <td>所属组</td>
        </tr>
        <%
            List userList=(List)request.getAttribute("users");
            if (userList==null || userList.size()==0){
        %>
            <tr>
                <td colspan="3">没有符合条件的数据</td>
            </tr>
        <%
            }else{
                for (Iterator iter=userList.iterator();iter.hasNext();){
                    User user=(User)iter.next();
        %>
            <tr>
                <td><%=user.getUserName() %></td>
                <td><%=user.getAge() %></td>
                <td><%=user.getGroup().getName() %></td>
            </tr>
        <%
                }
            }
        %>
        
    </table>
    
    <hr>
        <p>
        <h3>演示循环控制变迁:forEach(采用forEach标签)</h3>
        <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.group.name }</td>
                        </tr>
                    </c:forEach>
                </c:otherwise>
            </c:choose>
        </table>
        
        <hr>
        <p>
        <h3>采用forEach标签,varstart(设置斑马背景色)</h3>
        <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 mod 2==0 }">
                                <tr bgcolor="red">
                            </c:when>
                            <c:otherwise>
                                <tr>
                            </c:otherwise>
                        </c:choose>
                        
                                    <td>${user.userName }</td>
                                    <td>${user.age }</td>
                                    <td>${user.group.name }</td>
                                </tr>
                    </c:forEach>
                </c:otherwise>
            </c:choose>
        </table>
        
        
    <hr>
        <p>
        <h3>采用forEach标签,begin ,end</h3>
        <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" begin="2" end ="8" step="2">
                        <tr>
                            <td>${user.userName }</td>
                            <td>${user.age }</td>
                            <td>${user.group.name }</td>
                        </tr>
                    </c:forEach>
                </c:otherwise>
            </c:choose>
        </table>
        
    <p>
    <li>演示循环控制标签:forEach,演示map</li><br>
    <c:forEach items="${map }" var ="entry">
        ${entry.key },${entry.value }<br>
    </c:forEach>
    
    <p>
    <li>演示循环控制标签:forTokens</li><br>
    <c:forTokens items="${strTokens }" delims="#" var="v">
        ${v }<br>
    </c:forTokens>
    
    <p>
    <li>c:catch标签</li><br>
    <%
        try{
            Integer.parseInt("ferwg");
        }catch(Exception e){
            e.printStackTrace();
            out.println(e.getMessage());
        }
    %>
    <p>
    <c:catch var="msg">
        <%
            Integer.parseInt("ferg");
        %>
    </c:catch>
    ${msg }
    
    <p>
    <li>c:import标签</li><br>
    <c:import url="http://localhost:8080/drp4.5/login.jsp"></c:import>
    
    
    <p>
    <li>c:import标签</li><br>
    <c:url value="http://localhost:8080/drp4.5/sysmgr/validate.jsp" var="u">
        <c:param name="userId" value="zhangsan" />
        <c:param name="age" value="20" />
    </c:url>
    ${u }
    
    
    <p>
    <li>c:redirect标签</li><br>
    <!-- 
        <c:redirect url="http://localhost:8080/drp4.5/login.jsp" />
     -->
</body>
0 0
原创粉丝点击