JSTL、EL是页面渲染比较常用的基础的技术。然而,因完成的项目大多是通过JSON或XML返回页面,然后Javascript渲染。
所以,JSTL、EL的使用语法,到现在也很不熟练,不可信手拈来。
借最近做页面的权限展现,用到他们,记录一些简单的实践。
所有数据在Servlet中设置(为了模拟实际情况),而非页面设置。
package com.nicchagil.study.jstl; import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class JSTLServlet extends HttpServlet {
private static final long serialVersionUID = 1L; public JSTLServlet() {
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("hello", "Nick Huang!"); Function f1 = new Function("USER", "/user/main");
Function f2 = new Function("ROLE", "/role/main");
Function f3 = new Function("FUNCTION", "/role/main"); List<Function> fl = new ArrayList<Function>();
fl.add(f1);
fl.add(f2);
fl.add(f3);
request.setAttribute("fl", fl); Map<String, Function> fm = new HashMap<String, Function>();
fm.put("USER", f1);
fm.put("ROLE", f2);
fm.put("FUNCTION", f3);
request.setAttribute("fm", fm); request.getRequestDispatcher("show.jsp").forward(request, response);
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
} }
Servlet
用于测试JSTL、EL的JSP页面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="com.nicchagil.study.jstl.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>c:out</title>
</head>
<body> c:out<br/>
打印常量:<c:out value="${'`1234567890-=[]'}"/><br/>
打印普通字符:<c:out value="${hello}"/><br/>
打印Map的key为USER的Value:<c:out value="${fm['USER']}"/><br/>
打印Map的key为XXX的Value:<c:out value="${fm['XXX']}"/><br/> 打印静态常量:
<c:set var="F3" value="<%=Constants.F3%>"/>
<c:out value="${F3}"/><br/>
打印Map的key为静态常量的Value:<c:out value="${fm[F3]}"/><br/> <br/><br/><br/> c:set and c:out<br/>
<c:set var="salary" value="${2000*2}" scope="session"/>
<c:out value="${salary}"/> <br/><br/><br/> c:forEach<br/>
打印List集合:<br/>
<c:forEach items="${fl}" var="i" varStatus="s">
Item <c:out value="${s['index']}"></c:out> : <c:out value="${i}"/><br/>
</c:forEach><br/>
打印List集合字段:<br/>
1、要引入该JAVA类才能打印类细节<br/>
<c:forEach items="${fl}" var="i" varStatus="s">
Item <c:out value="${s['index']}"></c:out> : <c:out value="${i.functionCode}"/> - <c:out value="${i.functionURL}"/><br/>
</c:forEach><br/>
打印Map集合字段:<br/>
<c:forEach items="${fm}" var="i" varStatus="s">
Item <c:out value="${s['index']}"></c:out> : <c:out value="${i}"/> - <c:out value="${i.key}"/> - <c:out value="${i.value.functionURL}"/><br/>
</c:forEach><br/>
<br/> <br/><br/><br/> c:choose<br/>
<c:choose>
<c:when test="${not empty fm[F3]}">
fm[F3] is not empty
</c:when>
<c:otherwise>
fm[F3] is not empty
</c:otherwise>
</c:choose>
<br/>
<c:choose>
<c:when test="${not empty fm['xxx']}">
fm['xxx'] is not empty
</c:when>
<c:otherwise>
fm['xxx'] is empty
</c:otherwise>
</c:choose>
</body>
</html>
展现结果
c:out
打印常量:`1234567890-=[]
打印普通字符:Nick Huang!
打印Map的key为USER的Value:com.nicchagil.study.jstl.Function@1c7d56b
打印Map的key为XXX的Value:
打印静态常量: ROLE
打印Map的key为静态常量的Value:com.nicchagil.study.jstl.Function@19c9f16 c:set and c:out
4000 c:forEach
打印List集合:
Item 0 : com.nicchagil.study.jstl.Function@1c7d56b
Item 1 : com.nicchagil.study.jstl.Function@19c9f16
Item 2 : com.nicchagil.study.jstl.Function@180f545 打印List集合字段:
1、要引入该JAVA类才能打印类细节
Item 0 : USER - /user/main
Item 1 : ROLE - /role/main
Item 2 : FUNCTION - /role/main 打印Map集合字段:
Item 0 : FUNCTION=com.nicchagil.study.jstl.Function@180f545 - FUNCTION - /role/main
Item 1 : ROLE=com.nicchagil.study.jstl.Function@19c9f16 - ROLE - /role/main
Item 2 : USER=com.nicchagil.study.jstl.Function@1c7d56b - USER - /user/main c:choose
fm[F3] is not empty
fm['xxx'] is empty
资料参考:
- http://www.w3cschool.cc/jsp/jsp-jstl.html
- http://stackoverflow.com/questions/3978551/jstl-foreach-on-enum