日期:2014-05-16  浏览次数:20397 次

Struts1_学习笔记4_struts0400_jstl_EL表达式_核心库
1、使用JSTL,Servlet最低版本:2.4,查看Servlet版本方式:web.xml

2、JSTL可以操作数据库,XML

3、表达式一:EL表达式:
Action
Actionpublic ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//普通字符串
		request.setAttribute("hello", "hello world");
		
		//结构
		Group group = new Group();
		group.setName("和盈");
		
		User user = new User();
		user.setUsername("张三");
		user.setAge(18);
		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[] users = new User[10];
		for (int i=0; i<10; i++) {
			User u = new User();
			u.setUsername("U_" + i);
			users[i] = u;
		}
		request.setAttribute("users", users);
		
		List userList = new ArrayList();
		for (int i=0; i<10; i++) {
			User uu = new User();
			uu.setUsername("UU_" + i);
			userList.add(uu);
		}
		request.setAttribute("userlist", userList);
		
		//empty
		request.setAttribute("value1", null);
		request.setAttribute("value2", "");
		request.setAttribute("value3", new ArrayList());
		request.setAttribute("value4", "123456");
		return mapping.findForward("success");
	}


Struts1配置
	<action path="/jstlel" type="com.aowin.struts.JstlElAction">
			<forward name="success" path="/jstl_el.jsp" />
		</action>


JSP:
<h1>测试EL表达式</h1><br>
	<hr>
	<li>普通字符串</li><br>
	hello(jsp脚本):<%=request.getAttribute("hello") %><br>
	hello(el表达式,el表达式的使用方法$和{}):${hello }<br>
	hello(el表达式,el的隐含对象pageScope,requestScope,sessionScope,applicationScope,<br> 如果未指定scope,它的搜索顺序为pageScope~applicationScope):${requestScope.hello }<br>
	hello(el表达式,scope=session):${sessionScope.hello }<br>
	<p>
	<li>结构,采用.进行导航,也称存取器</li><br>
	姓名:${user.username }<br>
	年龄:${user.age }<br>
	所属组:${user.group.name }<br>
	<p>
	<li>输出map,采用.进行导航,也称存取器</li><br>
	mapvalue.key1:${mapvalue.key1 }<br>
	mapvalue.key2:${mapvalue.key2 }<br>
	<p>
	<li>输出数组,采用[]和下标</li><br>
	strarray[2]:${strarray[1] }<br>
	<p>
	<li>输出对象数组,采用[]和下标</li><br>
	userarray[3].username:${users[2].username }<br>
	<p>
	<li>输出list,采用[]和下标</li><br>
	userlist[5].username:${userlist[4].username }<br>
	<p>
	<li>el表达式对运算符的支持</li><br>
	1+2=${1+2 }<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
		 <=/le
		 >=/ge
		 &&/and
		 ||/or
		 !/not
		 //div
		 %/mod
	 -->  
	 <li>测试empty</li><br>
	 value1:${empty value1 }<br>
	 value2:${empty value2 }<br>
	 value3:${empty value3 }<br>
	 value4:${empty value4 }<br>
	 value4:${!empty value4 }<br>


结果:
value1:true
value2:true
value3:true
value4:false
value4:true





4、表达式二:JSTL核心库:

jstl标签库的配置
* 将jstl.jar和standard.jar拷贝到WEB-INF/lib下(如果使用el表达式,不用拷贝这两个jar)

注意:jstl必须在能够支持j2ee1.4/servlet2.4/jsp2.0版本上的容器才能运行,这个环境是3年前较为常用的环境

G