JavaBean的使用
JavaBean的使用
制作人:全心全意
能通过标签操作Java类,是因为编写的类遵循了JavaBean规范
<jsp:usebean>:实例化JavaBean对象
说明:通过该标签获取类的实例,其内部是通过实例化的默认构造方法进行获取,所以,JavaBean需要有一个默认的无参的构造方法
<jsp:getProperty>:获取JavaBean中的属性信息
说明:其内部是通过调用指定属性的getXXX()方法进行获取,所以,JavaBean规范要求为属性提供公共的(public)类型的访问器。
<jsp:setProperty>:对JavaBean中的属性赋值
说明:其内部是通过调用指定属性的setXXX()方法进行赋值,所以,JavaBean规范要求为属性提供公共的(public)类型的访问器。
将JavaBean对象应用到JSP页面中,JavaBean的生命周期可以自行设置,它存在于4种范围内,分别为page、request、session、application,默认情况下,JavaBean作用于page范围内。可以通过scope属性设置JavaBean的生命周期。
在JSP页面中应用JavaBean的实例
实例1:添加获取用户信息
Person类:对用户信息的封装类
package com.zq.bean; public class Person { private String name; //姓名 private int age; //年龄 private String sex; //性别 private String add; //住址 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 String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAdd() { return add; } public void setAdd(String add) { this.add = add; } }
index.jsp页面:主页面,放置录入用户信息所需要的表单
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>在JSP页面应用JavaBean-index.jsp</title> </head> <form action="result.jsp" method="post"> <table align="center" width="400" border="1" height="200"> <tr> <td colspan="2" align="center" height="40"> <b>添加用户信息</b> </td> </tr> <tr> <td align="right"> 姓 名:</td> <td><input type="text" name="name"/></td> </tr> <tr> <td align="right"> 年 龄:</td> <td><input type="text" name="age"/></td> </tr> <tr> <td align="right"> 性 别:</td> <td><input type="text" name="sex"/></td> </tr> <tr> <td align="right"> 住 址:</td> <td><input type="text" name="add"/></td> </tr> <tr> <td align="center" colspan="2"> <input type="submit" value="添 加"/> </td> </tr> </table> </form> </body> </html>
小技巧:表单信息中的属性名称做好设置成为JavaBean中的属性名称,这样就可以通过“<jsp:setProperty property="*" />”的形式来接收所有参数,这种方式可以减少程序中的代码量。如将用户年龄文本框的name属性设置为age,它对应Person类中的age。
result.jsp页面:对index.jsp页面中表单的提交请求进行处理。该页面将获取表单提交的所有信息,然后将所获取的用户信息输出到页面中
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="com.zq.bean.Email" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>在JSP页面应用JavaBean-result.jsp</title> </head> <body> <%request.setCharacterEncoding("utf-8"); %> <jsp:useBean id="person" class="com.zq.bean.Person" scope="page"> <jsp:setProperty name="person" property="*" /> </jsp:useBean> <table align="center" width="400"> <tr> <td align="right">姓 名:</td> <td> <jsp:getProperty property="name" name="person"/> </td> </tr> <tr> <td align="right">年 龄:</td> <td> <jsp:getProperty property="age" name="person"/> </td> </tr> <tr> <td align="right">性 别:</td> <td> <jsp:getProperty property="sex" name="person"/> </td> </tr> <tr> <td align="right">住 址:</td> <td> <jsp:getProperty property="add" name="person"/> </td> </tr> </table> </body> </html>
技巧:如果所处理的表单信息中包含中文,通过JSP内置对象request获取的参数值将出现乱码现象,此时可以通过request的setCharacterEncoding()方法指定字符编码格式进行解决。
实例2:解决中文乱码的JavaBean
News类:新闻信息实体对象的封装类
package com.zq.bean; public class News { private String title; private String content; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
CharactorEncoding类:对字符编码进行转换的类
package com.zq.bean; import java.io.UnsupportedEncodingException; public class CharactorEncoding { public CharactorEncoding() { } public String toString(String str) { String text = ""; if (str != null && !"".equals(str)) { try { text = new String(str.getBytes("ISO-8859-1"), "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return text; } }
index.jsp页面:新闻信息主页面,用于放置发布新闻信息的表单
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>在JSP页面应用JavaBean-index.jsp</title> </head> <form action="release.jsp" method="post"> <table align="center" width="450" height="260" border="1"> <tr> <td align="center" colspan="2" height="40"><b>新闻发布</b></td> </tr> <tr> <td align="right">标 题:</td> <td><input type="text" name="title" size="30"></td> </tr> <tr> <td align="right">内 容:</td> <td><textarea rows="8" cols="40" name="content"></textarea></td> </tr> <tr> <td align="center" colspan="2"><input type="submit" value="发 布"></td> </tr> </table> </form> </body> </html>
release.jsp页面:对发布的新闻信息进行处理
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>在JSP页面应用JavaBean-result.jsp</title> <style type="text/css"> #container { width: 450px; border: solid 1px; padding: 20px; } #title { font-size: 16px; font-weight: bold; color: #3399FF; } #content { font-size: 12px; text-align: left; } </style> </head> <body> <jsp:useBean id="news" class="com.zq.bean.News"></jsp:useBean> <jsp:useBean id="encoding" class="com.zq.bean.CharactorEncoding"></jsp:useBean> <jsp:setProperty property="*" name="news" /> <div align="center"> <div id="container"> <div id="title"> <%=encoding.toString(news.getTitle())%> </div> <hr> <div id="content"> <%=encoding.toString(news.getContent())%> </div> </div> </div> </body> </html>
说明:通过<jsp:useBean>标签实例化的JavaBean对象,如果在JSP页面中使用Java代码调用JavaBean对象中的属性或方法,所使用的JavaBean对象的变量名为<jsp:useBean>标签中的id属性。
实例3:显示时间
DateBean类:当前时间、星期的封装类
package com.zq.bean; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateBean { //日期及时间 private String dateTime; //星期 private String week; private Calendar calendar = Calendar.getInstance(); public String getDateTime(){ Date currDate = Calendar.getInstance().getTime(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); dateTime=sdf.format(currDate); System.out.println(dateTime); return dateTime; } public String getWeek(){ String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"}; int index = calendar.get(Calendar.DAY_OF_WEEK); week = weeks[index -1]; System.out.println(week); return week; } }
index.jsp页面:程序的主页,获取当前时间及星期实现电子时钟的效果
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Refresh" content="1"> <!-- 每1秒刷新一次页面 --> <title>电子时钟</title> <style type="text/css"> #clock{ width:420px; height:80px; background: #e0e0e0; font-size: 25px; font-weight: bold; border: solid 5px orange; padding: 20px; } #week{ padding-top: 15px; color: #0080ff; } </style> </head> <body> <jsp:useBean id="date" class="com.zq.bean.DateBean" scope="application"></jsp:useBean> <div align="center"> <div id="clock"> <div id="time"> <jsp:getProperty property="dateTime" name="date"/> </div> <div id="week"> <jsp:getProperty property="week" name="date"/> </div> </div> </div> </body> </html>
技巧:因为获取当前日期时间的JavaBean对象DateBean并不涉及更多的业务逻辑,所以实例中将它的作用域设置为application。这样做的好处是,在JSP页面中第一次调用该对象时会实例化一个DateBean对象,以后再次调用时不需要再次实例化DateBean,因为它在application范围内已经存在。
实例4:问卷调查
Paper类:调查问卷的封装类
package com.zq.bean; import java.io.Serializable; public class Paper implements Serializable { private static final long serialVersionUID = 1L; private String[] languages; private String[] technics; private String[] parts; public Paper(){ } public String[] getLanguages() { return languages; } public void setLanguages(String[] languages) { this.languages = languages; } public String[] getTechnics() { return technics; } public void setTechnics(String[] technics) { this.technics = technics; } public String[] getParts() { return parts; } public void setParts(String[] parts) { this.parts = parts; } }
Convert类:数组转换成字符串的JavaBean对象
package com.zq.bean; public class Convert { public String arr2Str(String[] arr){ StringBuffer sb = new StringBuffer(); if(arr != null && arr.length > 0){ for (String s : arr){ sb.append(s); sb.append(","); } if(sb.length() > 0){ sb = sb.deleteCharAt(sb.length() - 1); } } return sb.toString(); } }
index.jsp页面:调查问卷首页,放置调查问卷所使用的表单
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>调查问卷</title> </head> <body> <form action="result.jsp" method="post"> <div> <h1>调查问卷</h1> <hr/> <ul> <li>你喜欢吃的水果:</li> <li> <input type="checkbox" name="languages" value="香蕉">香蕉 <input type="checkbox" name="languages" value="苹果">苹果 <input type="checkbox" name="languages" value="大鸭梨">大鸭梨 </li> </ul> <ul> <li>你不喜欢吃的水果:</li> <li> <input type="checkbox" name="technics" value="榴莲">榴莲 <input type="checkbox" name="technics" value="芒果">芒果 </li> </ul> <ul> <li>选择吃下面的水果:</li> <li> <input type="checkbox" name="parts" value="西瓜">西瓜 <input type="checkbox" name="parts" value="荔枝">荔枝 </li> </ul> <input type="submit" value="提 交"> </div> </form> </body> </html>
result.jsp页面:将用户提交的调查问卷结果输出到页面上
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>问卷调查</title> </head> <body> <% request.setCharacterEncoding("utf-8"); %> <jsp:useBean id="paper" class="com.zq.bean.Paper"></jsp:useBean> <jsp:useBean id="convert" class="com.zq.bean.Convert"></jsp:useBean> <jsp:setProperty property="*" name="paper"/> <div> <h1>调查结果</h1> <hr/> <ul> <li>你喜欢吃的水果:<%=convert.arr2Str(paper.getLanguages()) %></li> <li>你不喜欢吃的水果:<%=convert.arr2Str(paper.getTechnics()) %></li> <li>选择吃下面的水果:<%=convert.arr2Str(paper.getParts()) %></li> </ul> </div> </body> </html>