JSTL入门实例

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="bean.Person"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>jstl+el表达式示例代码</title>
  </head>
  
  <body>  
    
    <!-- 使用jstl+el表达式可以迭代list集合 -->
    <% 
    	Person p1 = new Person();
    	p1.setName("aa111");
    	
    	Person p2 = new Person();
    	p2.setName("bb");
    	
    	List list = new ArrayList();
    	list.add(p1);
    	list.add(p2);
    	
    	request.setAttribute("list",list);
    %>    
    <c:forEach var="person" items="${list}">
    	${person.name }<br/>
    </c:forEach>   
    
    <!-- 使用jstl+el表达式可以迭代map集合 -->
    <% 
    	Map map = new HashMap();
    	map.put("a","aaaaxxx");
    	map.put("b","bbbb");
    	map.put("c","cccc");
    	map.put("1","aaaa1111");
    	
    	request.setAttribute("map",map);
    %>    
    <br/>    
   	<%-- Set<Map.Entry> set = map.entrySet()--%>
    <c:forEach var="me" items="${map}">  
    	${me.key } = ${me.value }<br/>
    </c:forEach>  
    
    <br/><br/>    
    <c:if test="${user!=null}">
    	欢迎您:${user.uesrname }
    </c:if>    
     <c:if test="${user==null}">
    	用户名:<input type="text" name="username">
    	密码:<input type="password" name="password">
    	<input type="submit" value="登陆">
    </c:if>
  </body>
</html>
package bean;

public class Person {
	public String name;
	public int age;
	public Address address;

	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;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

}


JSTL入门实例,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。