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

正则表达式JSP实例
<%@ page language="java" import="java.util.*,cn.com.Person,cn.com.Adddress" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'El.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  	
    <%
    	String data="assd";
    	request.setAttribute("data", data);
    %>
    <!-- 正则表达式,对于 Request传过来的数据可以直接如下显示
  		相当于:pageContext.findAttribute("data");
  		这是一般的方式
  	-->
    ${data }
    <br/>
    
    <%
    	Person p=new Person();
    	p.setName("name");
    	request.setAttribute("person", p);	
    %>
    <!-- 反射name属性获取这个值输出 
    	数据通过JAVABean里传过来的如下:
    -->
    ${person.name}
    
     <br/>
    <%
    	Person p2=new Person();
    	Adddress add=new Adddress();
    	add.setCity("NewYork");
    	p2.setAddress(add);
    	request.setAttribute("p2", p2);
     %>
     <!-- 下面的是定义一个Person的类和一个Adddress的类
     	Person中私有属性:private Adddress address;
     	要获取他的地址可以如下的方式来完成
     	数据复杂的bean里面带过来的如下:
      -->
     ${p2.address.city}
     <br/>
     
     <%
     	ArrayList list=new ArrayList();
     	list.add(new Person("wy"));
     	list.add(new Person("wyy"));
     	list.add(new Person("wyyy"));
     	request.setAttribute("list", list);
      %>
      <!-- 集合带过来的怎么获取数据呢? -->
      ${list[1].name }
      <br/>
      
      <%
      	Map map=new HashMap();
      	map.put("1", new Person("aaaa"));
      	map.put("b", new Person("bbbb"));
      	map.put("c", new Person("cccc"));
      	map.put("d", new Person("dddd"));
      	request.setAttribute("map", map);
       %>
       <!-- 
       		Map集合带过来的怎么获取数据呢?
       		map集合的数据存放的时候id一般不要用数字:会出现500错误
       		但是用数字的话获取方式如第二条
       		.号取不出来就用中括号[]
        -->
       ${map.b.name}
       ${map['1'].name }
       <br/>
       <!-- 获取当前的web项目名 -->
       ${pageContext.request.contextPath}
      	<a href="${pageContext.request.contextPath}/demo1.jsp" >点</a>
  </body>
</html>