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