JSP 标准标签库(JSTL)

JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能。
JSTL支持通用的、结构化的任务,比如迭代,条件判断,XML文档操作,国际化标签,SQL标签。 除了这些,它还提供了一个框架来使用集成JSTL的自定义标签。

获取请求参数中的值使用${param.username}

JSTL taglib 支持的jar包地址:http://tomcat.apache.org/download-taglibs.cgi

1. 简单的web例子

0.需要的jar包

taglibs-standard-compat-1.2.5.jar
taglibs-standard-impl-1.2.5.jar
taglibs-standard-jstlel-1.2.5.jar
taglibs-standard-spec-1.2.5.jar

1.工程预览
JSP 标准标签库(JSTL)_第1张图片
2.Viewer层的jsp代码
WebContent/01/01.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>


<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ page isELIgnored="false"%>


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    <%pageContext.setAttribute("username", "

张三

"
); %>
${hello }<br /> ${param.hello}<br /> ${username }<br /> <c:out value="${username}" escapeXml="true">c:out><br/> <c:out value="${username}" escapeXml="false">c:out><br/> <c:out value="${username1 }" default="查无此人" escapeXml="false">c:out><br /> body> html>

结果:
JSP 标准标签库(JSTL)_第2张图片
WebContent/WEB-INF/01/user.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>user.jsptitle>
<style type="text/css">
    table{
        border-right: 1px solid #000;
        border-bottom: 1px solid #000;
    }
    table td{
        border-left: 1px solid #000;
        border-top: 1px solid #000;
        padding: 4px;
    }
style>
head>
<body>
    <c:if test="${empty aa}">no "aa" property!c:if>
    <c:if test="${not empty user}">have "user" propertyc:if> <br />
    ${user.nickname },${user.age }<br/>
    <c:if test="${user.age lt 18}">未成年c:if><br />
    <c:choose>
        <c:when test="${user.age eq 18}">18 yearc:when>
        <c:when test="${user.age lt 18}">nonagec:when>
        <c:otherwise>adultc:otherwise>
    c:choose>
    <table align="center" border="1" width="900" cellpadding="0" cellspacing="0">
        <thead>
            <tr><td>用户名td><td>昵称td><td>年龄td>tr>
        thead>
        <tbody>
            <c:forEach var="u" items="${userList}" begin="0" end="4" step="1" varStatus ="cur">
                <tr <c:if test="${cur.index%2==0}">style="background:#ff0;color:#45f;"c:if>>
                    <td>${u.name}td><td>${u.nickname}--${cur.index}td><td>${u.age}td>
                tr>
            c:forEach>
        tbody>
    table>
body>
html>

结果:
JSP 标准标签库(JSTL)_第3张图片
3.controller层[servlet基于Annotation]
HelloServlet.java:

package com.my.mvc.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
    private static final long serialVersionUID = 3997354772023831828L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("HelloServlet doGet");
        request.setAttribute("hello", "world!");

        //客户端跳转
//      request.getSession().setAttribute("hello", "world");
//      response.sendRedirect(request.getContextPath()+"/01/01.jsp");

        //服务器端跳转
        //dispatcher 会自动加上 request.getContextPath()路径
        RequestDispatcher dispatcher= request.getRequestDispatcher("/01/01.jsp");
        dispatcher.forward(request, response);
//      response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

UserServlet.java:

package com.my.mvc.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.my.mvc.model.User;

@WebServlet("/user.do")
public class UserServlet extends HttpServlet {
    private static final long serialVersionUID = 8293399267378109285L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doGet");
        User user=new User("zs", "张三", 14);
        req.setAttribute("user", user);
        List userList=new ArrayList<>();
        for(int i=0;i<5;++i) {
            userList.add(new User("wukong", "悟空"+(i==0? "":""+i), 500));
        }
        req.setAttribute("userList", userList);
        RequestDispatcher requestDispatcher=req.getRequestDispatcher("/WEB-INF/01/user.jsp");
        requestDispatcher.forward(req, resp);
        System.out.println(user);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doPost");
        this.doGet(req, resp);
    }

}

model层:
User.java:

package com.my.mvc.model;

public class User {
    private String name;
    private String nickname;
    private int age;
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getNickname() {
        return nickname;
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    public User(String name, String nickname, int age) {
        super();
        this.name = name;
        this.nickname = nickname;
        this.age = age;
    }
    public User() {
    }
    @Override
    public String toString() {
        return "User [name=" + name + ", nickname=" + nickname + ", age=" + age + "]";
    }

}

你可能感兴趣的:(JSTL)