本文實(shí)例講述了JSP中正則表達(dá)式用法。分享給大家供大家參考,具體如下:
<%@ 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); %> <!-- 正則表達(dá)式,對(duì)于 Request傳過(guò)來(lái)的數(shù)據(jù)可以直接如下顯示 相當(dāng)于:pageContext.findAttribute("data"); 這是一般的方式 --> ${data } <br/> <% Person p=new Person(); p.setName("name"); request.setAttribute("person", p); %> <!-- 反射name屬性獲取這個(gè)值輸出 數(shù)據(jù)通過(guò)JAVABean里傳過(guò)來(lái)的如下: --> ${person.name} <br/> <% Person p2=new Person(); Adddress add=new Adddress(); add.setCity("NewYork"); p2.setAddress(add); request.setAttribute("p2", p2); %> <!-- 下面的是定義一個(gè)Person的類和一個(gè)Adddress的類 Person中私有屬性:private Adddress address; 要獲取他的地址可以如下的方式來(lái)完成 數(shù)據(jù)復(fù)雜的bean里面帶過(guò)來(lái)的如下: --> ${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); %> <!-- 集合帶過(guò)來(lái)的怎么獲取數(shù)據(jù)呢? --> ${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集合帶過(guò)來(lái)的怎么獲取數(shù)據(jù)呢? map集合的數(shù)據(jù)存放的時(shí)候id一般不要用數(shù)字:會(huì)出現(xiàn)500錯(cuò)誤 但是用數(shù)字的話獲取方式如第二條 .號(hào)取不出來(lái)就用中括號(hào)[] --> ${map.b.name} ${map['1'].name } <br/> <!-- 獲取當(dāng)前的web項(xiàng)目名 --> ${pageContext.request.contextPath} <a href="${pageContext.request.contextPath}/demo1.jsp" >點(diǎn)</a> </body></html>PS:這里再為大家提供2款非常方便的正則表達(dá)式工具供大家參考使用:
JavaScript正則表達(dá)式在線測(cè)試工具:
http://tools.VeVB.COm/regex/javascript
正則表達(dá)式在線生成工具:
http://tools.VeVB.COm/regex/create_reg
希望本文所述對(duì)大家JSP程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選