JAVA Web入门之JavaBean的解析及使用

创作不易觉得有帮助请点赞关注收藏

在JSP页面开发的初级阶段 并没有框架与逻辑分层概念的产生 需要将java代码嵌入到网页中 对JSP页面的一些业务逻辑进行处理

这种开发方式看似简单 但如果将大量java代码嵌入到JSP页面中 会给后期的维护及修改带来很多问题 如果能使HTML代码和java代码相分离开来,将java代码单独封装成一个处理某种业务逻辑的类,然后在JSP页面中调用此类,则可以降低HTML代码和java代码之间的耦合度,提高程序的重用性和灵活性,这种与HTML代码相分离而使用java代码封装的类,就是一个javabean组件

实例如下

package test;

import java.io.Serializable;

public class Email implements Serializable {
	private static final long serialVersionUID=1L;
	private String mailAdd;
	private boolean email;
public Email() {
	
	
	
	
}
public Email(String mailAdd) {
	this.mailAdd=mailAdd;
	
}
public boolean isEmail() {
	String regex="\\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:在javabean对象中 为了防止外部直接对javabean属性的调用 通常将javabean中的属性设置为私有的 但需要为其提供公共的访问方法 

下面创建名称为produce的类 创建商品属性并且提供相应的方法

public class Produce{
private String name="电吉他";
private double price=1880.5;
private int count=100;
private String factoryAdd="广东省珠海市";
public String getName(){
return name;
}
public String getPrice(){
return price;
}
public int getCount(){
return count;
}
public String getFactoryAdd(){
return factoryAdd;
}
}

在JSP页面中获取商品javabean中的属性信息 该操作通过JSP动作标签进行获取

<jsp:useBean id="produce" class="com.lyq.bean.Produce"></jsp:useBean>
<div>
<ul>
<li>
商品名称<jsp:getProperty property="name" name="produce"/>
</li>
<li>
价格<jsp:getProperty property="price" name="produce"/>
</li>
<li>
数量<jsp:getProperty property="count" name="produce"/>
</li>
<li>
厂址<jsp:getProperty property="factoryAdd" name="produce"/>
</li>
</ul>
</div>

3:对javabean属性赋值 

对javabean对象提供setxxx方法就可以在JSP页面中通过<jsp:setProperty>对其进行赋值

创建对应的类和方法如下

package com.lyq.bean;
public class Produce{
private String name;
private dobule price;
private int conut;
private String factoryAdd;
public String getName(){
return namel;
}
public void setName(String name){
this.name=name;
}
省略了其他的getxxx和setxxx方法 大致相同
}

创建JSP文件 在该页面中实例化Produce对象 然后对其属性进行赋值并输出

<jsp:useBean id="produce" class="com.lyq.bean.Produce"></jsp:useBean>
<jsp:setProterty proterty="name" name="produce" value="洗衣机"/>
<jsp:setProterty proterty="price" name="produce" value="88.88"/>
<jsp:setProterty proterty="count" name="produce" value="88"/>
<jsp:setProterty proterty="factoryAdd" name="produce" value="华南理工大学"/>
<div>
<ul>
<li>
商品名称<jsp:getProperty property="name" name="produce"/>
</li>
<li>
价格<jsp:getProperty property="price" name="produce"/>
</li>
<li>
数量<jsp:getProperty property="count" name="produce"/>
</li>
<li>
厂址<jsp:getProperty property="factoryAdd" name="produce"/>
</li>
</ul>
</div>

在JSP页面中应用javabean非常简单 主要通过JSP动作标签<jsp:useBean> <jsp:getProperty>

<jsp:setProterty>来实现对javabean对象的操作

在javabean对象应用到JSP页面中时,javabean的生命周期可以自行进行设置 它存在于4种范围内 分别为 page request session application 默认情况下作用于page范围内

4:解决中文乱码的JavaBean

在JSP页面中 处理中文字符经常会出现字符乱码的现象 特别是通过表单传递中文数据时容易产生 它的解决办法有很多 

创建对字符编码进行处理的javabean 在该类中编写toString方法对字符编码进行转换

package.com.lyq.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;
}
}

在JSP页面中用来显示时间的JavaBean

javabean是用java语言所写成的可重用组件 它可以是一个实体类对象 也可以是一个业务逻辑的处理  

同样javabean可以将数组转换成字符串

package con.lyq.bean;
public class Convert{
public String arr2Str(String[] arr){
String Buffer 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();
}
}

创作不易觉得有帮助请点赞关注收藏

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
好的,下面是一个简单的Java Web计算器的实现过程: 1. 首先创建一个JSP页面,用于显示计算器界面。在该页面中,需要有一些按钮,如数字、运算符、清除、等于等。 2. 创建一个JavaBean类,用于处理计算器的逻辑。在该类中,需要定义一些成员变量,如操作数1、操作数2、运算符等,以及一些方法,如加、减、乘、除等。 3. 在JSP页面中,使用`<jsp:useBean>`指令引用JavaBean类,并通过`<jsp:setProperty>`指令设置JavaBean类的属性。 4. 在JSP页面中,使用`<% %>`标签引用JavaBean类中的方法,用于处理用户的操作。例如,当用户点击加号按钮时,调用JavaBean类中的加法方法。 5. 最后,使用`<%= %>`标签输出计算结果。 下面是一个简单的Java Web计算器的代码实现: Calculator.jsp: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.example.bean.CalculatorBean" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Calculator</title> </head> <body> <h1>Calculator</h1> <form method="post"> <table> <tr> <td colspan="4"><input type="text" name="result" value="${calculatorBean.getResult()}" /></td> </tr> <tr> <td><input type="button" value="7" onclick="document.forms[0].result.value += '7'" /></td> <td><input type="button" value="8" onclick="document.forms[0].result.value += '8'" /></td> <td><input type="button" value="9" onclick="document.forms[0].result.value += '9'" /></td> <td><input type="button" value="+" onclick="document.forms[0].operator.value = '+'" /></td> </tr> <tr> <td><input type="button" value="4" onclick="document.forms[0].result.value += '4'" /></td> <td><input type="button" value="5" onclick="document.forms[0].result.value += '5'" /></td> <td><input type="button" value="6" onclick="document.forms[0].result.value += '6'" /></td> <td><input type="button" value="-" onclick="document.forms[0].operator.value = '-'" /></td> </tr> <tr> <td><input type="button" value="1" onclick="document.forms[0].result.value += '1'" /></td> <td><input type="button" value="2" onclick="document.forms[0].result.value += '2'" /></td> <td><input type="button" value="3" onclick="document.forms[0].result.value += '3'" /></td> <td><input type="button" value="*" onclick="document.forms[0].operator.value = '*'" /></td> </tr> <tr> <td><input type="button" value="0" onclick="document.forms[0].result.value += '0'" /></td> <td><input type="button" value="C" onclick="document.forms[0].result.value = ''" /></td> <td><input type="submit" value="=" /></td> <td><input type="button" value="/" onclick="document.forms[0].operator.value = '/'" /></td> </tr> <tr> <td colspan="4"><input type="hidden" name="operator" /></td> </tr> </table> </form> </body> </html> ``` CalculatorBean.java: ```java package com.example.bean; public class CalculatorBean { private double operand1; private double operand2; private String operator; private double result; public CalculatorBean() { } public double getOperand1() { return operand1; } public void setOperand1(double operand1) { this.operand1 = operand1; } public double getOperand2() { return operand2; } public void setOperand2(double operand2) { this.operand2 = operand2; } public String getOperator() { return operator; } public void setOperator(String operator) { this.operator = operator; } public double getResult() { return result; } public void setResult(double result) { this.result = result; } public void add() { result = operand1 + operand2; } public void subtract() { result = operand1 - operand2; } public void multiply() { result = operand1 * operand2; } public void divide() { result = operand1 / operand2; } } ``` 在以上代码中,我们通过`<jsp:useBean>`指令引用了CalculatorBean类,并使用`<jsp:setProperty>`指令设置了CalculatorBean类的属性。在表单提交时,我们通过JavaScript获取到用户的操作数和运算符,然后调用相应的JavaBean方法处理计算逻辑,最后通过`<%= %>`标签输出计算结果。 希望这篇文章能够帮助你实现一个简单的Java Web计算器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

showswoller

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值