1. JavaBean规范
(1) 有一个无参构造
(2) 提供get和set方法
(3) 字段属性就是定义的private等修饰的字段
(4) Bean字段为get和set方法中取出get方法中首字符改为小写
Javabean的自省机制
通过Introsppector的getBeanInfo方法得到bean
通过BeanUtil中的copyProperties()方法把一个对象的所以属性复制给另一个对象
可以把map中的属性也给对象
package cn.jiedada.beantest; import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import org.apache.commons.beanutils.BeanUtils; import org.junit.Test; import cn.jiedada.domain.Person; public class BeanTest { @Test public void f1() throws IntrospectionException{ BeanInfo beanInfo = Introspector.getBeanInfo(Person.class, Object.class); //属性描述器 PropertyDescriptor[] dd = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor pd : dd) { System.out.println(pd); //获得所有的读的方法 Method readMethod = pd.getReadMethod(); } } @Test public void f2() throws IllegalAccessException, InvocationTargetException{ Person p1 = new Person("tom", 18, 2, "资中"); Person p2 = new Person(); BeanUtils.copyProperties(p2, p1); System.out.println(p2); } //还可以通过map获得 }
person方法自己定义就可以了
2. EL表达式(其实就是就是一个三目运算符)
(1) ${属性名 }可以得到pageContext,requset,session,application中依此获得属性名中的值,也可以通过...的Scope如requsetScope等
可以通过${属性名.bean字段 }获得bean属性值
(2) 通过pageContext.request.contextpath获得上下文路径
3. jstl表达式
(1) 导如包jstl中的包
(2) 在jsp中使用标签库为<%@ taglib uri=”http://java.sun.com/jsp/jstl/core prefix=”c” %>
这样就可以使用jstl表达式了
其中有<%c:if test=”${age>18 }” %>这就是if表达式
Foreach的使用
<%c:forEach item=”${数组或者集合名 } ” var=”单个的对象属性” %>
<%@ page pageEncoding="UTF-8"%> <!DOCTYPE html > <html> <head> <title>Insert title here</title> </head> <body> <% pageContext.setAttribute("name","tom"); request.setAttribute("age",18); session.setAttribute("name","jack"); application.setAttribute("age",20); %> ${name } ${age } ${sessionScope.name } </body> </html>
<%@ page pageEncoding="UTF-8"%>
<!DOCTYPE html >
<html>
<head>
<title>Insert title here</title>
</head>
<body>
${name }
${age }
${sessionScope.name }
</body>
</html>
<%@page import="cn.jiedada.domain.Person"%> <%@ page pageEncoding="UTF-8"%> <!DOCTYPE html > <html> <head> <title>Insert title here</title> </head> <body> <% Person p = new Person("tom",18,1,"成都"); request.setAttribute("p1",p); %> ${p1 } </br> ${p1.name } ${p1.age } ${pageContext.request.contextPath } </body> </html>
<%@page import="cn.jiedada.domain.Person"%> <%@ page pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html > <html> <head> <title>Insert title here</title> </head> <body> <% Person p = new Person("tom",18,1,"成都"); request.setAttribute("p1",p); %> <c:if test="${p1.age>=18 }" var="s"> <h1>成年</h1> </c:if> <c:if test="${!s }" > <h1>未成年</h1> </c:if> </body> </html>
<%@page import="java.util.ArrayList"%> <%@page import="java.util.List"%> <%@page import="cn.jiedada.domain.Person"%> <%@ page pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html > <html> <head> <title>Insert title here</title> </head> <body> <% List<Person> list=new ArrayList<Person>(); list.add(new Person("tom",18,1,"成都")); list.add(new Person("tom",18,2,"成都")); list.add(new Person("tom",18,3,"成都")); list.add(new Person("tom",18,4,"成都")); list.add(new Person("tom",18,5,"成都")); request.setAttribute("list", list); %> <table border="1" style="border-collapse: collapse;"> <tr> <td>编号</td> <td>姓名</td> <td>年龄</td> <td>地址</td> </tr> <c:forEach items="${list }" var="p"> <tr> <td>${p.id }</td> <td>${p.name }</td> <td>${p.age }</td> <td>${p.address }</td> </tr> </c:forEach> </table> </body> </html>
4. mvc概念
是一种概念m:model模型就是写所有的java代码的地方,v:view视图为前端界面的展示,c:Contreller为现阶段为servlet代码