javaweb学习--javabean

阅读电子书《Java Web从入门到精通》密码:461c,学习JavaWeb基础知识

JavaBean类似于.net的实体类,但是规则上稍复杂一些,能实现的功能也多一些

一、介绍

1、规则

 package com.lyq.bean;

 /**
* 新闻(乱码处理)
* @author Administrator
*
*/
public class News {
private String title;
private String content; public String getTitle() {
return this.title;
} public void setTitle(String title) {
this.title = title;
} public String getContent() {
return this.content;
} public void setContent(String content) {
this.content = content;
}
}

可无构造函数,每个属性都是私有,使用setXX赋值getXX取值。若有默认值,可自行添加构造函数处理

2、使用

在jsp页面中使用以下代码实例化使用

 <jsp:useBean id="自定义id" class="javabean类的全路径"></jsp:useBean>

以下代码赋值

 <jsp:setProperty property="属性名" name="自定义的javabean的Id" />

以下代码取值

 <jsp:getProperty property="属性值" name="自定义的javabean的id" />
 自定义javabea的id.getXX()

完成代码为

 <form action="news_release.jsp" method="post">
<b>新闻发布</b><br />
标题:<input name="title" id="title" /><br />
内容:<textarea name="content" id="content" rows="8" cols="40"></textarea><br />
<input type="submit" value="发布" />
</form>
 <jsp:useBean id="news" class="com.lyq.bean.News"></jsp:useBean>
<jsp:useBean id="encoding" class="com.lyq.bean.CharactorEncoding"></jsp:useBean>
<jsp:setProperty property="*" name="news" />
<div><%= encoding.toString(news.getTitle()) %></div><br />
<div><%= encoding.toString(news.getContent()) %></div><br />
 package com.lyq.bean;

 import java.io.UnsupportedEncodingException;

 /**
* 乱码处理类
*
* @author Administrator
*
*/
public class CharactorEncoding {
/**
* 构造方法
*/
public CharactorEncoding() { } /**
* 对字符串进行转码处理
*
* @param str
* 要转码的字符串
* @return 编码后的字符串
*/
public String toString(String str) {
String text = "";
if (str == null || "".equals(str))
return text;
try{
text = new String(str.getBytes("ISO-8859-1"), "UTF-8");
}catch(UnsupportedEncodingException e){
e.printStackTrace();
} return text;
}
}

二、使用案例

1、邮箱验证

jsp页面

 <form action="email_result.jsp" method="post">
邮箱格式验证<br />
邮箱地址:<input type="text" name="mailAdd" id="mailAdd" /><br />
<input type="submit" value="验证" />
</form>

jsp验证结果页面

 <body>
<div align="center">
<%
String mailAdd = request.getParameter("mailAdd");
Email email = new Email(mailAdd);
if (email.isEmail()) {
out.print(mailAdd + " <br />是一个标准的邮箱地址!<br />");
} else {
out.print(mailAdd + " <br />不是一个标准的邮箱地址!<br />");
}
%>
<a href="email.jsp">返回</a>
</div>
</body>

javabean代码

 package com.lyq.bean;

 import java.io.Serializable;

 /**
* 邮箱类
*
* @author Administrator
*
*/
public class Email implements Serializable {
/**
* serialVersionUID值
*/
private static final long serialVersionUID = 1L;
/**
* Email地址
*/
private String mailAdd;
/**
* 是否是一个标准的Email地址
*/
private boolean email; /**
* 默认无参的构造函数
*/
public Email() { } /**
* 构造方法
*
* @param mailAdd
* Email地址
* @return
*/
public Email(String mailAdd) {
this.mailAdd = mailAdd;
} /**
* 是否是一个标准的Email地址
*
* @return 布尔值
*/
public boolean isEmail() {
String regex = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
if (mailAdd.matches(regex)) {
email = true;
}
return email;
} public String getMailAdd() {
return mailAdd;
} public void setMailAdd(String mailAdd) {
this.mailAdd = mailAdd;
} }

2、日期显示

jsp页面

 <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>
<meta http-equiv="Refresh" content="1"><%-- 页面1秒刷新一次,显示出时间效果 --%>
</head>
<body>
<jsp:useBean id="date" class="com.lyq.bean.DateBean"
scope="application"></jsp:useBean>
<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>
</center>
</body>

javabean代码

 package com.lyq.bean;

 import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; /**
* 日期处理类
* @author Administrator
*
*/
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秒");
this.dateTime = sdf.format(currDate);
return this.dateTime;
} public String getWeek() {
String[] weeks = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
int index = calendar.get(Calendar.DAY_OF_WEEK); // 获取一星期的某天
this.week = weeks[index - 1];
return this.week;
}
}

3、数组/字符串处理

jsp页面

 <form action="paper_reg.jsp" method="post">
<div>
<h1>调查问卷</h1>
<hr />
<ul>
<li>你经常用哪些编程语言开发程序:</li>
<li><input type="checkbox" name="languages" value="JAVA" id="languages0" /><label for="languages0">JAVA</label>
<input type="checkbox" name="languages" value="PHP" id="languages1" /><label for="languages1">PHP</label>
<input type="checkbox" name="languages" value=".NET" id="languages2" /><label for="languages2">.NET</label>
<input type="checkbox" name="languages" value="VC++" id="languages3" /><label for="languages3">VC++</label>
</li>
</ul>
<ul>
<li>你目前掌握的技术:</li>
<li><input type="checkbox" name="technics" value="HTML" />HTML
<input type="checkbox" name="technics" value="JAVABEAN" />JAVABEAN
<input type="checkbox" name="technics" value="JSP" />JSP
<input type="checkbox" name="technics" value="SERVLET" />SERVLET</li>
</ul>
<ul>
<li>在学习中哪一部分感觉有困难:</li>
<li><input type="checkbox" name="parts" value="JSP" />JSP
<input type="checkbox" name="parts" value="STRUTS" />"STRUTS"
</ul>
<input type="submit" value="提交" />
</div>
</form>
 <body>
<jsp:useBean id="paper" class="com.lyq.bean.Paper"></jsp:useBean>
<jsp:useBean id="convert" class="com.lyq.bean.Convert"></jsp:useBean>
<jsp:setProperty property="*" name="paper" />
<div>
<h1>调查结果</h1>
<hr />
<li>你经常使用的编程语言:<%= convert.arr2Str(paper.getLanguages()) %></li>
<li>你目前掌握的技术:<%= convert.arr2Str(paper.getTechnics()) %></li>
<li>在学习中哪一部分感觉有困难:<%= convert.arr2Str(paper.getParts()) %></li>
</div>
</body>

javabean代码

 package com.lyq.bean;

 import java.io.Serializable;

 /**
* 数组转字符串
*
* @author Administrator
*
*/
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 this.languages;
} public void setLanguages(String[] languages) {
this.languages = languages;
} public String[] getTechnics() {
return this.technics;
} public void setTechnics(String[] technics) {
this.technics = technics;
} public String[] getParts() {
return this.parts;
} public void setParts(String[] parts) {
this.parts = parts;
}
}
 package com.lyq.bean;

 public class Convert {
/**
* 将数组转成字符串
*
* @param arr
* 数组
* @return 字符串
*/
public String arr2Str(String[] arr) {
StringBuffer sb = new StringBuffer(); //StringBuffer是可变长的对象,比使用String拼接效率更高
if (arr == null || arr.length == 0)
return sb.toString();
for (String s : arr) {
sb.append(s);
sb.append(",");
}
if (sb.length() > 0)
sb = sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
}

4、不同方法接收页面参数

jsp页面

 <form action="person_reg.jsp" method="post">
<b>添加用户信息</b><br />
姓名:<input type="text" id="username" name="username" /><br />
年龄:<input type="text" id="age" name="age" /><br />
性别:<input type="text" id="sex" name="sex" /><br />
住址:<input type="text" id="add" name="add" /><br />
<input type="submit" value="提交" />
</form>
 <body>
<%
request.setCharacterEncoding("UTF-8"); //防止中文乱码
%>
<jsp:useBean id="person" class="com.lyq.bean.Person" scope="page">
<jsp:setProperty name="person" property="*" /><%-- 使用*接收所有参数,使用这种方式要求表单中的属性名与JavaBean的属性名一致 --%>
<jsp:setProperty name="person" property="name" param="username" /><%--如果表单中的属性名(即参数)与JavaBean的属性名一致,则用param对其赋值 --%>
</jsp:useBean>
<div>
<ul>
<li>姓名:<jsp:getProperty property="name" name="person" /></li>
<li>年龄:<jsp:getProperty property="age" name="person" /></li>
<li>性别:<jsp:getProperty property="sex" name="person" /></li>
<li>住址:<jsp:getProperty property="add" name="person" /></li>
</ul>
</div>
</body>

javabean代码

 package com.lyq.bean;

 /**
* 人员信息
* @author Administrator
*
*/
public class Person {
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private int age;
/**
* 性别
*/
private String sex;
/**
* 住址
*/
private String add; public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return this.age;
} public void setAge(int age) {
this.age = age;
} public String getSex() {
return this.sex;
} public void setSex(String sex) {
this.sex = sex;
} public String getAdd() {
return this.add;
} public void setAdd(String add) {
this.add = add;
}
}
上一篇:CAS无锁实现原理以及ABA问题


下一篇:KMP算法通俗解析