阅读电子书《Java Web从入门到精通》密码:461c,学习JavaWeb基础知识
JavaBean类似于.net的实体类,但是规则上稍复杂一些,能实现的功能也多一些
一、介绍
1、规则
1 package com.lyq.bean; 2 3 /** 4 * 新闻(乱码处理) 5 * @author Administrator 6 * 7 */ 8 public class News { 9 private String title; 10 private String content; 11 12 public String getTitle() { 13 return this.title; 14 } 15 16 public void setTitle(String title) { 17 this.title = title; 18 } 19 20 public String getContent() { 21 return this.content; 22 } 23 24 public void setContent(String content) { 25 this.content = content; 26 } 27 }
可无构造函数,每个属性都是私有,使用setXX赋值getXX取值。若有默认值,可自行添加构造函数处理
2、使用
在jsp页面中使用以下代码实例化使用
1 <jsp:useBean id="自定义id" class="javabean类的全路径"></jsp:useBean>
以下代码赋值
1 <jsp:setProperty property="属性名" name="自定义的javabean的Id" />
以下代码取值
1 <jsp:getProperty property="属性值" name="自定义的javabean的id" />
1 自定义javabea的id.getXX()
完成代码为
1 <form action="news_release.jsp" method="post"> 2 <b>新闻发布</b><br /> 3 标题:<input name="title" id="title" /><br /> 4 内容:<textarea name="content" id="content" rows="8" cols="40"></textarea><br /> 5 <input type="submit" value="发布" /> 6 </form>
1 <jsp:useBean id="news" class="com.lyq.bean.News"></jsp:useBean> 2 <jsp:useBean id="encoding" class="com.lyq.bean.CharactorEncoding"></jsp:useBean> 3 <jsp:setProperty property="*" name="news" /> 4 <div><%= encoding.toString(news.getTitle()) %></div><br /> 5 <div><%= encoding.toString(news.getContent()) %></div><br />
1 package com.lyq.bean; 2 3 import java.io.UnsupportedEncodingException; 4 5 /** 6 * 乱码处理类 7 * 8 * @author Administrator 9 * 10 */ 11 public class CharactorEncoding { 12 /** 13 * 构造方法 14 */ 15 public CharactorEncoding() { 16 17 } 18 19 /** 20 * 对字符串进行转码处理 21 * 22 * @param str 23 * 要转码的字符串 24 * @return 编码后的字符串 25 */ 26 public String toString(String str) { 27 String text = ""; 28 if (str == null || "".equals(str)) 29 return text; 30 try{ 31 text = new String(str.getBytes("ISO-8859-1"), "UTF-8"); 32 }catch(UnsupportedEncodingException e){ 33 e.printStackTrace(); 34 } 35 36 return text; 37 } 38 }
二、使用案例
1、邮箱验证
jsp页面
1 <form action="email_result.jsp" method="post"> 2 邮箱格式验证<br /> 3 邮箱地址:<input type="text" name="mailAdd" id="mailAdd" /><br /> 4 <input type="submit" value="验证" /> 5 </form>
jsp验证结果页面
1 <body> 2 <div align="center"> 3 <% 4 String mailAdd = request.getParameter("mailAdd"); 5 Email email = new Email(mailAdd); 6 if (email.isEmail()) { 7 out.print(mailAdd + " <br />是一个标准的邮箱地址!<br />"); 8 } else { 9 out.print(mailAdd + " <br />不是一个标准的邮箱地址!<br />"); 10 } 11 %> 12 <a href="email.jsp">返回</a> 13 </div> 14 </body>
javabean代码
1 package com.lyq.bean; 2 3 import java.io.Serializable; 4 5 /** 6 * 邮箱类 7 * 8 * @author Administrator 9 * 10 */ 11 public class Email implements Serializable { 12 /** 13 * serialVersionUID值 14 */ 15 private static final long serialVersionUID = 1L; 16 /** 17 * Email地址 18 */ 19 private String mailAdd; 20 /** 21 * 是否是一个标准的Email地址 22 */ 23 private boolean email; 24 25 /** 26 * 默认无参的构造函数 27 */ 28 public Email() { 29 30 } 31 32 /** 33 * 构造方法 34 * 35 * @param mailAdd 36 * Email地址 37 * @return 38 */ 39 public Email(String mailAdd) { 40 this.mailAdd = mailAdd; 41 } 42 43 /** 44 * 是否是一个标准的Email地址 45 * 46 * @return 布尔值 47 */ 48 public boolean isEmail() { 49 String regex = "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"; 50 if (mailAdd.matches(regex)) { 51 email = true; 52 } 53 return email; 54 } 55 56 public String getMailAdd() { 57 return mailAdd; 58 } 59 60 public void setMailAdd(String mailAdd) { 61 this.mailAdd = mailAdd; 62 } 63 64 }
2、日期显示
jsp页面
1 <style type="text/css"> 2 #clock { 3 420px; 4 height: 80px; 5 background: #E0E0E0; 6 font-size: 25px; 7 font-weight: bold; 8 border: solid 5px orange; 9 padding: 20px; 10 } 11 12 #week { 13 padding-top: 15px; 14 color: #0080FF; 15 } 16 </style> 17 <meta http-equiv="Refresh" content="1"><%-- 页面1秒刷新一次,显示出时间效果 --%> 18 </head> 19 <body> 20 <jsp:useBean id="date" class="com.lyq.bean.DateBean" 21 scope="application"></jsp:useBean> 22 <center> 23 <div id="clock"> 24 <div id="time"> 25 <jsp:getProperty property="dateTime" name="date" /> 26 </div> 27 <div id="week"> 28 <jsp:getProperty property="week" name="date" /> 29 </div> 30 </div> 31 </center> 32 </body>
javabean代码
1 package com.lyq.bean; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Calendar; 5 import java.util.Date; 6 7 /** 8 * 日期处理类 9 * @author Administrator 10 * 11 */ 12 public class DateBean { 13 private String dateTime; 14 private String week; 15 private Calendar calendar = Calendar.getInstance(); 16 17 public String getDateTime() { 18 Date currDate = Calendar.getInstance().getTime(); 19 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH点mm分ss秒"); 20 this.dateTime = sdf.format(currDate); 21 return this.dateTime; 22 } 23 24 public String getWeek() { 25 String[] weeks = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; 26 int index = calendar.get(Calendar.DAY_OF_WEEK); // 获取一星期的某天 27 this.week = weeks[index - 1]; 28 return this.week; 29 } 30 }
3、数组/字符串处理
jsp页面
1 <form action="paper_reg.jsp" method="post"> 2 <div> 3 <h1>调查问卷</h1> 4 <hr /> 5 <ul> 6 <li>你经常用哪些编程语言开发程序:</li> 7 <li><input type="checkbox" name="languages" value="JAVA" id="languages0" /><label for="languages0">JAVA</label> 8 <input type="checkbox" name="languages" value="PHP" id="languages1" /><label for="languages1">PHP</label> 9 <input type="checkbox" name="languages" value=".NET" id="languages2" /><label for="languages2">.NET</label> 10 <input type="checkbox" name="languages" value="VC++" id="languages3" /><label for="languages3">VC++</label> 11 </li> 12 </ul> 13 <ul> 14 <li>你目前掌握的技术:</li> 15 <li><input type="checkbox" name="technics" value="HTML" />HTML 16 <input type="checkbox" name="technics" value="JAVABEAN" />JAVABEAN 17 <input type="checkbox" name="technics" value="JSP" />JSP 18 <input type="checkbox" name="technics" value="SERVLET" />SERVLET</li> 19 </ul> 20 <ul> 21 <li>在学习中哪一部分感觉有困难:</li> 22 <li><input type="checkbox" name="parts" value="JSP" />JSP 23 <input type="checkbox" name="parts" value="STRUTS" />"STRUTS" 24 </ul> 25 <input type="submit" value="提交" /> 26 </div> 27 </form>
1 <body> 2 <jsp:useBean id="paper" class="com.lyq.bean.Paper"></jsp:useBean> 3 <jsp:useBean id="convert" class="com.lyq.bean.Convert"></jsp:useBean> 4 <jsp:setProperty property="*" name="paper" /> 5 <div> 6 <h1>调查结果</h1> 7 <hr /> 8 <li>你经常使用的编程语言:<%= convert.arr2Str(paper.getLanguages()) %></li> 9 <li>你目前掌握的技术:<%= convert.arr2Str(paper.getTechnics()) %></li> 10 <li>在学习中哪一部分感觉有困难:<%= convert.arr2Str(paper.getParts()) %></li> 11 </div> 12 </body>
javabean代码
1 package com.lyq.bean; 2 3 import java.io.Serializable; 4 5 /** 6 * 数组转字符串 7 * 8 * @author Administrator 9 * 10 */ 11 public class Paper implements Serializable { 12 private static final long serialVersionUID = 1L; 13 /** 14 * 语言 15 */ 16 private String[] languages; 17 /** 18 * 技术 19 */ 20 private String[] technics; 21 /** 22 * 难点 23 */ 24 private String[] parts; 25 26 public Paper() { 27 28 } 29 30 public String[] getLanguages() { 31 return this.languages; 32 } 33 34 public void setLanguages(String[] languages) { 35 this.languages = languages; 36 } 37 38 public String[] getTechnics() { 39 return this.technics; 40 } 41 42 public void setTechnics(String[] technics) { 43 this.technics = technics; 44 } 45 46 public String[] getParts() { 47 return this.parts; 48 } 49 50 public void setParts(String[] parts) { 51 this.parts = parts; 52 } 53 }
1 package com.lyq.bean; 2 3 public class Convert { 4 /** 5 * 将数组转成字符串 6 * 7 * @param arr 8 * 数组 9 * @return 字符串 10 */ 11 public String arr2Str(String[] arr) { 12 StringBuffer sb = new StringBuffer(); //StringBuffer是可变长的对象,比使用String拼接效率更高 13 if (arr == null || arr.length == 0) 14 return sb.toString(); 15 for (String s : arr) { 16 sb.append(s); 17 sb.append(","); 18 } 19 if (sb.length() > 0) 20 sb = sb.deleteCharAt(sb.length() - 1); 21 return sb.toString(); 22 } 23 }
4、不同方法接收页面参数
jsp页面
1 <form action="person_reg.jsp" method="post"> 2 <b>添加用户信息</b><br /> 3 姓名:<input type="text" id="username" name="username" /><br /> 4 年龄:<input type="text" id="age" name="age" /><br /> 5 性别:<input type="text" id="sex" name="sex" /><br /> 6 住址:<input type="text" id="add" name="add" /><br /> 7 <input type="submit" value="提交" /> 8 </form>
1 <body> 2 <% 3 request.setCharacterEncoding("UTF-8"); //防止中文乱码 4 %> 5 <jsp:useBean id="person" class="com.lyq.bean.Person" scope="page"> 6 <jsp:setProperty name="person" property="*" /><%-- 使用*接收所有参数,使用这种方式要求表单中的属性名与JavaBean的属性名一致 --%> 7 <jsp:setProperty name="person" property="name" param="username" /><%--如果表单中的属性名(即参数)与JavaBean的属性名一致,则用param对其赋值 --%> 8 </jsp:useBean> 9 <div> 10 <ul> 11 <li>姓名:<jsp:getProperty property="name" name="person" /></li> 12 <li>年龄:<jsp:getProperty property="age" name="person" /></li> 13 <li>性别:<jsp:getProperty property="sex" name="person" /></li> 14 <li>住址:<jsp:getProperty property="add" name="person" /></li> 15 </ul> 16 </div> 17 </body>
javabean代码
1 package com.lyq.bean; 2 3 /** 4 * 人员信息 5 * @author Administrator 6 * 7 */ 8 public class Person { 9 /** 10 * 姓名 11 */ 12 private String name; 13 /** 14 * 年龄 15 */ 16 private int age; 17 /** 18 * 性别 19 */ 20 private String sex; 21 /** 22 * 住址 23 */ 24 private String add; 25 26 public String getName() { 27 return this.name; 28 } 29 30 public void setName(String name) { 31 this.name = name; 32 } 33 34 public int getAge() { 35 return this.age; 36 } 37 38 public void setAge(int age) { 39 this.age = age; 40 } 41 42 public String getSex() { 43 return this.sex; 44 } 45 46 public void setSex(String sex) { 47 this.sex = sex; 48 } 49 50 public String getAdd() { 51 return this.add; 52 } 53 54 public void setAdd(String add) { 55 this.add = add; 56 } 57 }