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

Struts_jstl完全使用
	*****Action中的处理:**************8
		//====普通字符串======//
		request.setAttribute("hello", "Hello World");
		//====对象===============//
		User user=new User();
		user.setUsername("wulihai");
		user.setAge(25);
		Group group=new Group();
		group.setName("地下党");
		user.setGroup(group);
		//=====以上是业务逻辑处理,应该单独放入一个业务处理类中======
		request.setAttribute("user", user);
		//=======Map==============//
		Map elMap=new HashMap();
		elMap.put("key1", "value1");
		elMap.put("key2", "value2");
		request.setAttribute("elMap", elMap);
		//=========集合======//
		List userlist=new ArrayList();
		for (int i=0; i<10; i++) {
			User usr = new User();
			usr.setUsername("USR_" + i);
			usr.setAge(25+i);
			userlist.add(usr);
		}
		request.setAttribute("userlist", userlist);
		
		//=========对象数组=========//
		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);
		//=========字符串数组========//
		String[] strArray = new String[]{"a", "b", "c"};
		request.setAttribute("strArray", strArray);
	
	
	
	*******Jsp页面获取值***********************
	
	//=====普通字符串,EL表达式的使用方法$和{},EL表达式的隐含对象pageScope/requestScope/sessionScope/applicationScope等等
			如果为指定scope,它的搜索顺序是pageScope~applicationScope====//	
	hello(el,scope=request): ${requestScope.hello}<br>	
	hello(el,scope=session): ${sessionscope.hello} <br>
	
	//=====对象</li>使用"."来导航
	姓名 : ${user.username}<br>
	年龄 : ${user.age}<br>
	所属组:${user.group.name}<br>
	
	//==Map</li>使用"."来导航
	elMap.value1: ${elMap.key1}<br>
	elMap.value2: ${elMap.key2}<br>
	
	//==集合</li>使用中括号[]加下标数字导航<br>
	userlist[4].username: ${userlist[4].username}<br>
	userlist[4].age: ${userlist[4].age}<br>
	
	//==对象数组</li>使用中括号[]加下标数字导航<br>
	users[5].username:${users[5].username}
	//==字符串数组</li>使用中括号[]加下标数字导航<br>
	strArray[0]:${strArray[0] }<br>
	strArray[1]:${strArray[1] }<br>
	
	//==EL表达式对运算符的支持</li><br>
	1+4:${1+4 }<br>
	9-3:${9-2 }<Br>
	2*3:${2*3 }<br>
	9/2:${9/3 }<br>
	9mod2:${9 mod 2 }<br>
	4div1:${4div 1 }<br>
===================JSTL的核心标签	===================
//==c:out的value属性支持El表达式,<font color='red'>特别注意value后面可以跟El表达式,也可以跟字符串
	 hello(value="${hello}): <c:out value="${hello}"/><br>
	 abcd:<c:out value="${abcd}" /><br>
	 c:out的default属性,此外c:out的defalut属性是默认值,在value为空的情况下取default的值<br>
	 abcd(default="为空"):<c:out value="${abcd}" default="为空"/><br>
	 c:out的escapeXml属性,默认为true,原样输出<br>
     bj(escapeXml="false")<c:out value="${bj}" escapeXml="false"/><br>
     bj(escapeXml="true")<c:out value="${bj}" escapeXml="true"/><br>
     //==c:set/c:remove
     <c:set var="temp" value="${userlist[1].username}"/>
     temp:<c:out value="${temp}"/><br>
     <c:remove var="temp"/>
     temp:<c:out value="${temp}"/><br>
      //==c:if
      <c:if test="${v1 lt v2}">
        v1 小于 v2
      </c:if>	
	//===c:forEach循环输出Map的元素==//
      <c:forEach items="${elMap}" var="m">
      ${m.key},${m.value }<br>
      </c:forEach>
	
	//====c:choose/c:when/c:otherwise============//
      <table border="1">
      <tr>
             <td>姓名</td>
              <td>年龄</td>
               <td>所属组</td>
            </tr>
      <c:choose>
        <c:when test="${(empty userlist)}">//判断userlist是否为空
          <tr>
            <td colspan="3"> 没有合适的数据</td>
          </tr>
       </c:when>
       <c:otherwise>
          //=====迭代集合userlist,从第2个开始,第8个结束,步长为2======//
         <c:forEach items="${userlist}" var="usr" begin="2" end="8" step="2" varStatus="v">
         		//====两层嵌套,c:otherwise内部又好包含又c:when,c:otherwise===//