jsp怎么写

jsp尽量不要写java代码,那是很早以前JSP刚出来时的写法,后来发现非常难以维护和修改,所以出现了现在的EL表达式 ${}这种类型。

上古时期写法(java代码HTML代码直接混合写):

<body>
   	<h1>购物车</h1>
   	<table border=1>
   		<tr>
			<th>商品名称</th>
			<th>商品数量</th>
   		</tr>
   		<%
   			Map<String,Integer> cart = (Map<String,Integer>)session.getAttribute(cart);
   			if(cart!=null && cart.size()>0){
   				for(Entry<String,Integer> en : cart.entrySet()){
   				%>
   				<tr>
					<td><%=en.getKey() %></td>
					<td><%=en.getValue() %></td>
		   		</tr>
   				<% }
   			}
   		 %>
   	</table>
  </body>

现代写法(JSTL标签

<table border=1>
    	<tr>
    		<th>用户名</th>
    		<th>当前遍历索引</th>
    		<th>当前遍历计数</th>
    		<th>是否是集合第一个元素</th>
    		<th>是否是集合最后一个元素</th>
    	</tr>
	     <c:forEach items=${list} var=name varStatus=st >
		    <tr class=${st.index%2==0?one:two} >
	    		<td>${name}</td>
	    		<td>${st.index}</td>
	    		<td>${st.count}</td>
	    		<td>${st.first}</td>
	    		<td>${st.last}</td>
	    	</tr>
	    </c:forEach>
    </table>
<hr> 
	<!--  数数的功能-->
	<c:forEach begin=1 end=10 step=1 var=num >
		${num}
	</c:forEach>

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...