JSTL标签详解


 

package com.xzit.servlet;

import java.io.IOException;
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.xzit.data.DataBase;

/**
 * Servlet implementation class ForeachServlet
 */
@WebServlet("/foreachServlet")
public class ForeachServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
     */
    protected void service(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        request.getSession().setAttribute("cars", DataBase.getCarList());
//        response.sendRedirect("foreach.jsp");
        request.setAttribute("names", DataBase.getNames());
        request.getRequestDispatcher("fortokens.jsp").forward(request, response);
    }

}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" 
    import="com.xzit.domain.Student"%>

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


    
        
        JSTL foreach 标签
    
    
    
        if test="${sessionScope.cars ne null}">
        
                    if test="${(sta.index+1) mod 2 eq 0}">
                        if>
                    if test="${(sta.index+1) mod 2 ne 0}">
                        if>
                if>
        
序号产品名称产品单价产品颜色
${sta.count} ${car.proName} ${car.price} ${car.color}
${sta.count} ${car.proName} ${car.price} ${car.color}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

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

    
        
        Insert title here
    
    
        <%-- JSTL fortokens 标签用法 --%>
        if test="${requestScope.names ne null}">
            
                第${sta.count}个人的名字是:${sta.current}
if>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="com.xzit.domain.Student"%>
    
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>


    
        
        JSTL 标签
    
    <%
        Student stu = new Student("向问天",36);
    %>
    
        <%-- Set 标签的使用 --%>
        

        <%-- remove 标签移除变量 --%>
        
        
        <%-- Set 标签设置符合类型对象的某个属性 --%>
        
        学生的名字:<%=stu.getStuName()%>
学生的名字:<%=stu.getStuName()%>
${stuNewName}
<%-- out 标签显示作用域中的某个变量值 --%> ${pageScope.user}
default
="King" escapeXml="false">
foreach标签测试
测试redirect标签
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

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

    
        
        JSTL IF 标签
    
    
        <%-- 使用url 标签设置一个url变量 --%>
        
    
        <%-- redirect 标签的使用 --%>
        if test="${param.location ne null}">
            
                
            
        if>
        
        <%-- 使用redirect标签重定向到同一个服务器的其他应用程序 --%>
        <%-- 
        
        --%>
        
        <%-- 使用redirect标签重定向到外部url的其他网络平台 --%>
        <%-- 
        
        --%>
    
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

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

    
        
        JSTL IF 标签
    
    
        获取的参数username的值是:${param.username}
        
 
<%-- import url="http://www.baidu.com" charEncoding="utf-8">import> --%>

百度
package com.xzit.data;

import java.util.ArrayList;
import java.util.List;

import com.xzit.domain.Car;

public class DataBase {

    private static String names;
    private static List carList = new ArrayList();
    
    
    static{
        
        names = "卫青,霍去病,戚继光,岳飞,项羽";
        carList.add(new Car("雷克萨斯",67,"银灰"));
        carList.add(new Car("布加迪威龙",205,"蓝色"));
        carList.add(new Car("法拉利",150,"红色"));
        carList.add(new Car("陆虎揽胜",160,"白色"));
        carList.add(new Car("拉博基尼蝙蝠",205,"黄色"));
        carList.add(new Car("奥迪幻影",99,"黑色"));
    }
    public static String getNames() {
        return names;
    }
    
    public static List getCarList() {
        return carList;
    }
    
    
}
package com.xzit.domain;

public class Car {

    private String proName;
    private double price;
    private String color;
    
    public String getProName() {
        return proName;
    }
    public void setProName(String proName) {
        this.proName = proName;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    
    public Car() {
        
    }

    public Car(String proName, double price, String color) {
        
        this.proName = proName;
        this.price = price;
        this.color = color;
    }
}