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+"https://www.jb51.net/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="https://www.jb51.net/<%=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 type="text/css" href="https://www.jb51.net/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="https://www.jb51.net/${pageContext.request.contextPath}/demo1.jsp" >点</a> </body> </html>

PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

JavaScript正则表达式在线测试工具:

正则表达式在线生成工具:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzfwzd.html