JstlElServlet:
/** * */ package com.bjpowrnode.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; /** * @ClassName:JstlElServlet * @Description:测试EL表达式 * @author wm * @date 2016年2月14日下午8:45:38 */ public class JstlElServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //普通字符串 request.setAttribute("hello", "Hello World"); //结构 Group group =new Group(); group.setName("动力节点drp 603班"); User user=new User(); user.setUsername("张三"); user.setAge(23); user.setGroup( group); request.setAttribute("user", user); //Map Map map=new HashMap(); map.put("k1", "v1"); map.put("k2", "v2"); request.setAttribute("map", map); //字符串数组 String[] strArray=new String[]{"a","b","c"}; request.setAttribute("str_array", strArray); //对象数组 User[] users=new User[10]; for (int i=0;i<users.length;i++){ users[i]=new User(); users[i].setUsername("张三"+i); } request.setAttribute("users",users); //List List userList=new ArrayList(); for(int i =0;i<10;i++){ User usr=new User(); usr.setUsername("李四" +i); userList.add(usr); } request.setAttribute("userList",userList); //empty request.setAttribute("v2","" ); request.setAttribute("v3", new ArrayList()); request.setAttribute("v4", "123456"); request.setAttribute("v5", null); request.getRequestDispatcher("/jstl_el.jsp").forward(request, response);; } }
jstl_el.jsp
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!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=GB18030"> <title>Insert title here</title> </head> <body> <h1>测试EL表达式</h1> <hr> <li>普通字符串</li><br> hello(jsp脚本):<%=request.getAttribute("hello") %></br> hello(El表达式:语法:$和{}:${hello}<br> hello(el表达式,el内置对象, pageScope,requestScope,sessionScope,applicationScope )<br> 如果不指定范围,它的搜索范围就是pageScope~applicationScope():${requestScope.hello}<br> hello(el表达式,从session中取得):${sessionScope.hello}<br> <p> <li>结构,采用。进行导航,或称为存取器</li><br> 姓名:${user.username}<br> 年龄:${user.age }<br> 所属:${user.group.name}<br> </p> <p> <li>Map取值</li><br> map.k1:${map.k1}<br> map.k2:${map.k2}<br> </p> <p> <li>字符串数组:采用[]下标</li><br> strArray[1]:${str_array[1] }<br> <p> <li>对象数组:采用[]下标</li> users[5].username:${users[5].username }<br> <p> <li>list,采用[] 下标</li> userList[6].username:${userList[6].username }<br> <p> <li>el表达式对运算符的支持</li> 1+1= ${1+1 }<br> 10/5= ${10/5}<br> 10 div 5=${10 div 5 }<br> 10%3=${10%3 }<br> 10 mod 3=${10 mod 3 }<br> <!-- ==/eq !=/ne </lt >/gt &&/and ||/or !/not //div %/mod --> <p> <li>测试empty</li><br> v1:${empty v1 }<br> v2:${empty v2 }<br> v3:${empty v3 }<br> v4:${empty v4 }<br> v5:${empty v5 }<br> </body> </html>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <servlet-name>JstlElServlet</servlet-name> <servlet-class>com.bjpowrnode.jstl.JstlElServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>JstlElServlet</servlet-name> <url-pattern>/servlet/JstlElServlet</url-pattern> </servlet-mapping> </web-app>
Group.java
/** * */ package com.bjpowrnode.jstl; /** * @ClassName:Group * @Description:TODO * @author wm * @date 2016年2月15日上午9:41:01 */ public class Group { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
User.java
/** * */ package com.bjpowrnode.jstl; /** * @ClassName:User * @Description:TODO * @author wm * @date 2016年2月15日上午9:42:15 */ public class User { private String username1; private int age; private Group group; public String getUsername() { return username1; } public void setUsername(String username1) { this.username1 = username1; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Group getGroup() { return group; } public void setGroup(Group group) { this.group = group; } }
感触:学一个东西,要小范围的系统的学,总结一下EL对常用的Map.list.数组的读取,80%学到了就可以了。这样应用级别的入门级别就ok了。