1:java中经常使用的组件,实际上是一个java类,用于封装数据的。
2: 5条规范:
必须是一个public的类;
必须有私有的属性,
提供set和get方法;
提供无参的构造参数,编译器自动添加的也算,最好本身在写一个无参构造;
implement Serializable 实现序列化。
3: javabean中的成员变量和属性
在普通类中,成员变量就是属性,可是在javabean中,成员变量是private int age中的age,而属性是它的set和get方法后面的字段名称(首字母小写)html
public class JavaBean { public String name; public int age; // private boolean sex; 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 boolean isSex() { // return sex; // } // public void setSex(boolean sex) { // this.sex = sex; // } public JavaBean() { super(); }
beanUtils是将数据封装到javaBean中的工具,须要引入依赖
有不少的方法,例如:
setProperty(Object javabean,String name,Object value);
getProperty(Object javabean,String name);
populate(Object javabean ,Map<String,Object value>)java
public class BeanUtilsDemo { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub JavaBean test = new JavaBean(); //使用BeanUtils为属性赋值 BeanUtils.setProperty(test, "name", "小红"); BeanUtils.setProperty(test, "age", 12); //获得属性值 String name = BeanUtils.getProperty(test, "name"); int age = Integer.parseInt(BeanUtils.getProperty(test, "age")); System.out.println("名字是:"+ name + "年龄是:"+age); //建立map,用于存放属性名和属性值 Map<String,Object> map = new HashMap<String,Object>(); map.put("name", "dada"); map.put("age", 14); //使用populate()方法为对象赋值 BeanUtils.populate(test, map); System.out.println("名字" + test.getName() + "年龄" + test.getAge()); } }
也能够用于接受前台页面的参数,使用这个能够达到一键封装。web
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="/chapter07/BeanUtilsServlet" method="post"> <input type="text" name="name" >用户名 <br/> <input type="text" name="age" >年龄<br/> <input type="submit">登陆 </form> </body> </html>
这是servlet使用beanUtils来获取属性spring
public class BeanUtilsServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { JavaBean j = new JavaBean(); try { BeanUtils.populate(j, req.getParameterMap()); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } resp.getWriter().println(j); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub doGet(req, resp); } }
这是日常的获取属性数组
public class TestServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub String name = (String) req.getAttribute("name"); String age = (String) req.getAttribute("age"); int ageNew = Integer.parseInt(age); JavaBean pararm = new JavaBean(); pararm.setName(name); pararm.setAge(ageNew); resp.getWriter().print(pararm); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub doGet(req, resp); } /** * */ }
${表达式}服务器
public class MyServlet extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub req.setAttribute("username", "消息"); req.setAttribute("age", 34); RequestDispatcher dispatcher = (RequestDispatcher) req.getRequestDispatcher("/myjsp.jsp"); dispatcher.forward(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub this.doGet(req, resp); } }
public class MyServlet extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub req.setAttribute("username", "消息"); req.setAttribute("age", 34); RequestDispatcher dispatcher = (RequestDispatcher) req.getRequestDispatcher("/myjsp.jsp"); dispatcher.forward(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub this.doGet(req, resp); } }
在书写El表达式的时候,会使用一些符号来标记变量或函数,这些称之为标识符。
规范:
不能以数字开头;不能使用保留字;不能使用特殊符号;不能使用隐式对象。cookie
就是${表达式},就是这里面的表达式session
Boolean类型的常量,整型,浮点,字符串,null。app
${person.username}curl
${person[aa–bb]},这种aa–bb,存在特殊符号的使用。
算数运算符,比较运算符,逻辑运算符,三目运算符,empty 运算符。
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <%-- 隐式对象pageContext--%> ${pageContext.request.requestURI} ${pageContext.response.ContextType} </body> </html>
存在四种域:pageContext(存在一个jsp的页面中)->HttpRequest(存在一次请求中)->HttpSession(存在一次会话中)->application(ServletContext,存在整个项目中,共享域)。这是按照从小到大的范围排的。
EL的隐式对象提供了四个:pageScope,requestScope,applicationScope,sessionScope.
举例:${pageScope.username}
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% request.setAttribute("userName","我是");%> <% pageContext.setAttribute("ageName","的");%> <% session.setAttribute("sexName","男");%> <% application.setAttribute("sing","歌");%> ${requestScope.userName} ${pageScope.ageName} ${applicationScope.sing} ${sessionScope.sexName} </body> </html>
用于获取客户段传递的请求参数,是map类型,至关于request.getParameter()方法同样。为空返回的是空字符串。
例:${param.num};
${paramValues.nums[0]}
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="${pageContext.request.contextPath}/params.jsp" > 1:<input type="text" name="num"><br/> 2:<input type="text" name="nums"><br/> 3:<input type="text" name="nums"><br/> <input type="submit" value="提交"> <input type="reset" value="重置"> <hr/> 1:${param.num} 2:${paramValues.nums[0]} 3:${paramValues.nums[1]} </form> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" import="javax." %> <html> <head> <title>Title</title> </head> <body> ${cookie.userName} ${cookie.userName.name} ${cookie.userName.value} <% response.addCookie(new Cookie("age",12));%> </body> </html>
JSTL (javaserverpage standard tag library) 标签库
主要使用核心标签库;
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Title</title> </head> <body> <%-- value是输出到页面的文本内容 dafult:是value属性为空时,输出的值 escapeXml:是否将特殊字符进行HTML编码后在进行输出文本 <c:out value="value" escapeXml="true|false">defaulatValue</c:out> --%> <c:out value="${param.userName}" default="unknown" escapeXml="false"></c:out> <c:out value="${param.userName}">unKnown</c:out> <c:out value="${param.userName}" escapeXml="true"> <a href="www.baidu.com"></a> </c:out> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>> <html> <head> <title>Title</title> </head> <body> <%-- value:设置了一个值。 var:指定了逻辑表达式中的变量的名字 test:设置逻辑表达式 scope:设置var变量的做用域的范围 --%> <c:set value="1" var="num" property="num" scope="application"></c:set> <c:if test="${num == 1}" > 这是正确的! </c:if> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>> <html> <head> <title>Title</title> </head> <body> <%-- 至关于java中的if elseif elseif else; 注意:在el表达式中,再写一个字符串只能使用单引号。 --%> <c:choose> <c:when test="${empty param.userName}"> unKnown user </c:when> <c:when test="${param.userName = 'itcast'}"> ${param.userName} is a User! </c:when> <c:otherwise> ${param.userName} is a Administer! </c:otherwise> </c:choose> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>> <html> <head> <title>Title</title> </head> <body> <%-- <c:forEach var="varName" items="collection" begin="begin" end="end" step="step" varStatus="varStatusName"></c:forEach> var:指定当前迭代到的元素保存到page中的名称 items:指定用于迭代的集合对象 varStatus:指定当前迭代信息状态的名称保存到page域中 begin:表示从第几个元素开始迭代 end:表示到几个元素迭代结束 setp:迭代的步长,就是能够跳这迭代 注意:在items接受是不能直接写如items="map",而应该写成items="<%=list%>" --%> <%--数组--%> <% String [] books = {"java","javaWeb","spring"}; %> 书籍有: <c:forEach var="user" items="<%=books%>"> ${user}<br/> </c:forEach> <%--map--%> <% Map map = new HashMap<>(); map.put("name","小"); map.put("age",12); %> <c:forEach var="user" items="<%=map%>"> ${user.key} ${user.value} </c:forEach> <%--list--%> <% List list = new ArrayList(); list.add("re"); list.add("ra"); list.add("rb"); list.add("rc"); list.add("rd"); %> <c:forEach var="user" items="<%=list%>" begin="0" end="3" step="2"> ${user} </c:forEach> <%-- 返回状态信息,有几个方面,count:序号;index:索引;first:是不是第一个元素;last:是不是最后一个元素 --%> <% List user1 = new ArrayList(); user1.add("a"); user1.add("b"); user1.add("c"); user1.add("d"); %> <table border="1"> <tr> <td>count:序号</td> <td>index:索引</td> <td>first</td> <td>last</td> <td>value</td> </tr> <c:forEach var="user" items="<%=user1%>" varStatus="status"></c:forEach> <tr> <td>${status.count}</td> <td>${status.index}</td> <td>${status.first}</td> <td>${status.last}</td> <td>${user}</td> </tr> </table> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>> <html> <head> <title>Title</title> </head> <body> <%-- var:表示将构造的url的地址保存到域对象中的名称 value:表示构造好的url的地址 scope:表示将构造好的url保存到指定的与对象中 context:表示指定导入同一服务器下的web应用的名称 --%> <%-- 第一种绝对路径--%> <c:url var="url" value="http://localhost:8080/chapter07/login.jsp" > <c:param name="name" value="张三"/> <c:param name="age" value="12"/> </c:url> <a href="${url}">login.jsp</a> <%-- 第二种相对路径--%> <c:url var="url" value="/login.jsp?name=tim&age=12" > </c:url> <a href="${url}">login.jsp</a> </body> </html>