简单介绍下EL的常用形式,继续用程序说明:
package com.keith.el; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import com.keith.taglibs.Group; import com.keith.taglibs.User; public class ElAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // 字符串 request.setAttribute("he", "hello EL "); // 结构 Group group = new Group(); group.setGroupName("KEITH的朋友"); User user = new User(); user.setName("keith"); user.setAge(19); user.setGroup(group); request.setAttribute("user", user); // map Map mapValue = new HashMap(); mapValue.put("key1", "value1"); mapValue.put("key2", "value2"); request.setAttribute("mapValue", mapValue); // 字符串数组 String[] strArray = new String[] { "a", "b", "c" }; request.setAttribute("strArray", strArray); // 对象数组 User[] user1 = new User[10]; for (int i = 0; i < 10; i++) { User u = new User(); u.setName("U_" + i); user1[i] = u; } request.setAttribute("user1", user1); //list List userList = new ArrayList(); for (int i = 0; i < 10; i++) { User u_list = new User(); u_list.setName("u_list"+i); userList.add(u_list); } // 测试${empty } request.setAttribute("value2", ""); request.setAttribute("value3", "abcd"); request.setAttribute("value4", new ArrayList()); request.setAttribute("userList", userList); return mapping.findForward("elTest"); } }
用到了User类:
package com.keith.taglibs; public class User { private String name; private int age; private Group group; public Group getGroup() { return group; } public void setGroup(Group group) { this.group = group; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Group类:
package com.keith.taglibs; public class Group { private String groupName; public String getGroupName() { return groupName; } public void setGroupName(String groupName) { this.groupName = groupName; } }
struts-config.xml:
<action-mappings> <action path="/elTest" type="com.keith.el.ElAction"> <forward name="elTest" path="/elTest.jsp"></forward> </action> <action-mappings>
elTest.jsp页面:由于用到了logic标签所以引入下:
<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic"%>
elTest.jsp:
<body> <h2> EL表达式TEST </h2> <h3> EL(输出字符串) </h3> ${he } <hr> <h3> EL(输出结构) </h3> <table> <tr> <td> 姓名 </td> <td> 年龄 </td> <td> 班级 </td> </tr> <logic:empty name="user"> <font color="red">user不存在</font> </logic:empty> <logic:notEmpty name="user"> <tr> <td> ${user.name } </td> <td> ${user.age } </td> <td> ${user.group.groupName } </td> </tr> </logic:notEmpty> </table> <hr> <h3> EL(输出MAP) </h3> mapValue.key1的值是:${mapValue.key1 } <br /> mapValue.key2的值是:${mapValue.key2 } <br /> <hr /> <h3> EL(输出字符串数组) </h3> strArray[1]的值:${strArray[1] } <br /> strArray[2]的值:${strArray[2] } <br /> <hr /> <h3> EL(输出对象数组) </h3> user1[1].name:${user1[1].name } <hr /> <h3> EL(list输出数组) </h3> userList[1].name:${userList[1].name } <hr/> <h3>EL表达式计算</h3> 1+9=${1+9 }<br/> 1-9=${1-9 }<br/> 1*9=${1*9 }<br/> 1/9=${1/9 }<br/> <h3>测试EL{empty}</h3> value1:${empty value1 }<br/> value2:${empty value2 }<br/> value3:${empty value3 }<br/> value4:${empty value4 }<br/> </body>
很简单............