`
summer_021
  • 浏览: 55426 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

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年前较为常用的环境

Group类:成员属性:name
User类:成员属性:username、age、group

Action:
public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//普通属性
		request.setAttribute("hello", "Hello World");
	
		//测试条件控制标签
		request.setAttribute("v1", 1);
		request.setAttribute("v2", 2);
		request.setAttribute("v3", new ArrayList());
		request.setAttribute("v4", "test");
		
		//html文本
		request.setAttribute("sh", "<font color='red'>上海欢迎您</font>");
		
		//测试c:forEach
		Group group = new Group();
		group.setName("和盈");
		List userList = new ArrayList();
		for (int i=0; i<10; i++) {
			User user = new User();
			user.setUsername("user_" + i);
			user.setAge(18+i);
			user.setGroup(group);
			userList.add(user);
		}
		request.setAttribute("userlist", userList);
		//测试循环输出map
		Map map = new HashMap();
		map.put("k1", "v1");
		map.put("k2", "v2");
		request.setAttribute("mapvalue", map);	
		//测试c:forTokens
		request.setAttribute("strTokens", "1,2,3,4,5,6");
		return mapping.findForward("success");
	}


Struts1配置文件:
<action path="/jstlcore" type="com.aowin.struts.JstlCoreAction">
			<forward name="success" path="/jstl_core.jsp" />
		</action>


页面显示:
<h1>测试jstl核心库</h1>
	<hr>
	<li>测试c:out</li><br>
	hello(default):<c:out value="${hello}"/><br>
	hello(el表达式):${hello }<br>
	hello(default="123"):<c:out value="${abc}" default="123"/><br>
	<!-- 去不到就为空串 -->
	hello(default="123"):<c:out value="${abc}">123</c:out><br>
	sh(defalut):<c:out value="${sh}"/><br>
	<!-- 取scope里东西value里面要用el表达式 -->
	bj(escapeXml="true"):<c:out value="${sh}" escapeXml="true"/><br>
	bj(escapeXml="false"):<c:out value="${sh}" escapeXml="false"/><br>
	bj(el表达式):${sh }<br>
	<p>
	<li>测试c:set和c:remove</li><br>
	<!-- 生命一个变量 默认scope为page -->
	<c:set value="123" var="temp"/>
	temp:${temp }<br>
	<c:remove var="temp"/>
	temp:${temp }<br>
	<p>
	<li>测试条件控制标签c:if</li><br>
	<c:if test="${v1 lt v2}" var="v">
		v1小于v2<br>v=${v }<br>
	</c:if>
	<!-- 为空或者为null test结果都为true -->
	<c:if test="${empty v3}">
		v3为空<br>
	</c:if>
	<c:if test="${empty v4}">
		v4为空<br>
	</c:if>
	<p>
	<li>测试条件控制标签c:choose,c:when,c:otherwise</li><br>
	<!-- when 1个或者多个 -->
	<!--otherwise 0个或者1个 -->
	<!-- if() else if() else if() else() -->
	<c:choose>
		<c:when test="${v2 gt v1}">
			v1小于v22<br>
		</c:when>
		
		<c:when test="${v1 lt v2}">
			v1小于v23<br>
		</c:when>
		
		<c:otherwise>
			v1大于v2<br>
		</c:otherwise>
	</c:choose>
	<c:choose>
		<c:when test="${empty v4}">
			v4为空<br>
		</c:when>
		<c:otherwise>
			v4不为空<br>
		</c:otherwise>
	</c:choose>
	<p>
	<li>测试循环控制标签c:forEach</li><br>
	<table border="1">
		<tr>
			<td>姓名</td>
			<td>年龄</td>
			<td>所属组</td>
		</tr>
		<c:choose>
			<c:when test="${empty userlist}">
				<tr>
					<td colspan="3">没有符合条件的数据!</td>
				</tr>
			</c:when>
			<c:otherwise>
				<c:forEach items="${userlist}" var="u">
					<tr>
						<td>${u.username }</td>
						<td>${u.age }</td>
						<td>${u.group.name }</td>
					</tr>
				</c:forEach>
			</c:otherwise>
		</c:choose>
	</table>	
	<p>	
	<li>测试循环控制标签c:forEach,varstatus</li><br>
	<table border="1">
		<tr>
			<td>姓名</td>
			<td>年龄</td>
			<td>所属组</td>
		</tr>
		<c:choose>
			<c:when test="${empty userlist}">
				<tr>
					<td colspan="3">没有符合条件的数据!</td>
				</tr>
			</c:when>
			<c:otherwise>
				<%--varStatus:变量状态 --%>
				<c:forEach items="${userlist}" var="user" varStatus="vs">
					<c:choose>
						<c:when test="${vs.count % 2 == 0}">
							<tr bgcolor="red">
						</c:when>
						<c:otherwise>
							<tr>
						</c:otherwise>
					</c:choose>
								<td>
									<c:out value="${user.username}"/>
								</td>
								<td>
									<c:out value="${user.age}"/>
								</td>
								<td>
									<c:out value="${user.group.name}"/>
								</td>
							</tr>					
				</c:forEach>
			</c:otherwise>
		</c:choose>
	</table>
	<p>
	<li>测试循环控制标签c:forEach,begin,end,step</li><br>
	<table border="1">
		<tr>
			<td>姓名</td>
			<td>年龄</td>
			<td>所属组</td>
		</tr>
		<c:choose>
			<c:when test="${empty userlist}">
				<tr>
					<td colspan="3">没有符合条件的数据!</td>
				</tr>
			</c:when>
			<c:otherwise>
				<c:forEach items="${userlist}" var="user" begin="2" end="8" step="2">
					<tr>
						<td>${user.username}</td>
						<td>${user.age}</td>
						<td>${user.group.name }</td>
					</tr>
				</c:forEach>
			</c:otherwise>
		</c:choose>
	</table>	
	<p>
	<li>测试循环控制标签c:forEach,普通循环</li><br>
	<c:forEach begin="1" end="10">
		a<br>
	</c:forEach>
	<p>
	<li>测试循环控制标签c:forEach,输出map</li><br>
	<!-- v:entity对象 -->
	<c:forEach  items="${mapvalue}" var="v">
		${v.key }=${v.value }<br>
	</c:forEach>
	<p>
	<li>测试循环控制标签c:forTokens(逗号间隔或者其他符号间隔的输出)</li><br>
	<c:forTokens items="${strTokens}" delims="," var="v">
		${v }<br>
	</c:forTokens>
	<p>
	<li>测试c:catch</li><br>
	<%
		try {
			Integer.parseInt("asdfsdf");
		}catch(Exception e) {
			out.println(e.getMessage());
		}	
	%>
	<p>
	<!-- var:放异常信息的变量 -->
	<c:catch var="exinfo">
		<%
			Integer.parseInt("asdfsdf");
		%>
	</c:catch>
	${exinfo }<br>
	<p>
	<!-- 相当于include -->
	<li>测试c:import</li><br>
	<c:import url="http://icafebar-pc:8080/struts0400_jstl/"/>
	<p>
	<li>测试c:url和c:param</li><br>
	<!-- 生成一个url串 -->
	<c:url value="http://www.baidu.com" var="v">
		<c:param name="username" value="Jack"/>
		<c:param name="age" value="20"/>
	</c:url>
	${v }<br>
	<li>测试:redirect</li><br>
	<%--如果不用context 则url里面写全路径
		<c:redirect context="/struts0400_jstl" url="/index.jsp"/>
	 --%>


页面显示效果:
测试jstl核心库
测试c:out

hello(default):Hello World
hello(el表达式):Hello World
hello(default="123"):123
hello(default="123"):123
sh(defalut):<font color='red'>上海欢迎您</font>
bj(escapeXml="true"):<font color='red'>上海欢迎您</font>
bj(escapeXml="false"):上海欢迎您
bj(el表达式):上海欢迎您

测试c:set和c:remove

temp:123
temp:

测试条件控制标签c:if

v1小于v2
v=true
v3为空

测试条件控制标签c:choose,c:when,c:otherwise

v1小于v22
v4不为空

测试循环控制标签c:forEach

姓名 	年龄 	所属组
user_0 	18 	和盈
user_1 	19 	和盈
user_2 	20 	和盈
user_3 	21 	和盈
user_4 	22 	和盈
user_5 	23 	和盈
user_6 	24 	和盈
user_7 	25 	和盈
user_8 	26 	和盈
user_9 	27 	和盈

测试循环控制标签c:forEach,varstatus

姓名 	年龄 	所属组
user_0 	18 	和盈
user_1 	19 	和盈
user_2 	20 	和盈
user_3 	21 	和盈
user_4 	22 	和盈
user_5 	23 	和盈
user_6 	24 	和盈
user_7 	25 	和盈
user_8 	26 	和盈
user_9 	27 	和盈

测试循环控制标签c:forEach,begin,end,step

姓名 	年龄 	所属组
user_2 	20 	和盈
user_4 	22 	和盈
user_6 	24 	和盈
user_8 	26 	和盈

测试循环控制标签c:forEach,普通循环

a
a
a
a
a
a
a
a
a
a

测试循环控制标签c:forEach,输出map

k1=v1
k2=v2

测试循环控制标签c:forTokens(逗号间隔或者其他符号间隔的输出)

1
2
3
4
5
6

测试c:catch

For input string: "asdfsdf"

java.lang.NumberFormatException: For input string: "asdfsdf"

测试c:import

测试JSTL
测试EL表达式
测试jstl核心库
测试jstl格式化库
测试jstl函数库

测试c:url和c:param

http://www.baidu.com?username=Jack&age=20
测试:redirect
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics