当前位置:编程学习 > JAVA >>

jsp中javaBean的运用

Jsp的一个重要特性就是可以用javaBean实现功能的扩展。将大部分功能放在javaBean中完成,以使jsp页面程序更干净简洁、利于维护。JavaBean可以很方便的用来捕获页面表单的输入并完成各种业务逻辑的处理。如下就是一个Hello示例:  
testA.jsp页面:  

<%@ page contentType="text/html;charset=GBK" %>  
<html>  
<head>  
<title>示例</title>  
</head>  
<body scroll=no>  
<form name="frma" method="post" action="testB.jsp" >  
<p>  
你的姓名:   
<input type="text" size="15" name="yourName" value="" id=yourName>  
<input type="button" align="center" name="subBtn" value="[提交]" onClick="f_check()" id=subBtn>  
</p>   
</form>  
</body>  
</html>  
<script language="javascript" type="text/javascript">  
<!--  
function f_check(){  
if(document.forms(0).yourName.value==""){  
alert("请输入姓名");  
}else{  
document.forms(0).submit();  
}  
}  
-->  
</SCRIPT>  

testB.jsp页面  


<%@ page contentType="text/html;charset=GBK" %>  
<html>  
<head>  
<title>示例</title>  
</head>  
<jsp:useBean id="tBean" scope="page" class="bean.TestBean" >  
<jsp:setProperty name="tBean" property="*" />  
</jsp:useBean>  
<body scroll=no>  
<form name="frmb" method="post" action="" >   
<p>  
<%=tBean.hello()%>  
</p>  
</form>  
</body>  
</html>  


TestBean.java 代码:  


package bean;  


public class TestBean{  


private String yourName = "";   


public void setYourName(String yourName){  
this.yourName = ConvertGBK(yourName);  
}  


public String hello(){  
String strHello = "Hello:"+yourName;  
return strHello;  
}   


//汉字转换方法  
public String ConvertGBK(String str){  
String strReturn="";  
try{  
strReturn=new String(str.getBytes("ISO-8859-1"),"GBK");  
}catch(Exception ex){  
System.out.println("TestBean.ConvertGBK():ex="+ex.toString());  
}  
finally{  
return strReturn;  
}  
}  


}  
  testA.jsp页面上“提交”按钮将表单提交给testB.jsp页面,testB.jsp获得的testA.jsp中yourName文本框的值并在实例化TestBean后,执行bean中的setYourName方法,接着执行hello方法,在页面上输出对你问好的语句。  
  这个简单的示例体现了在jsp中使用javaBean的两个重要方面,一个是捕获表单的输入并保存,一个是执行逻辑功能。所以,依此两个功能还可以将用在jsp中的javaBean分为值Bean(value bean)和工具Bean (utility bean),如下:  
值Bean  


package bean;  
public class TestValueBean{  
private String yourName = "";  


public void setYourName(String yourName){  
this.yourName = ConvertGBK(yourName);   
}  
//汉字转换方法  
public String ConvertGBK(String str){  
String strReturn="";  
try{  
strReturn=new String(str.getBytes("ISO-8859-1"),"GBK");  
}catch(Exception ex){  
System.out.println("TestBean.ConvertGBK():ex="+ex.toString());  
}  
finally{  
return strReturn;  
}  
}  
}  


工具Bean  
package bean;  
public class TestUtilityBean{  
public String hello(TestValueBean tvBean){  
String strHello = "Hello:"+tvBean.getName();  
return strHello;  
}  
public String hello(String yourName){  
String strHello = "Hello:"+yourName;  
return strHello;  
}  
}  
  当然,从这个例子看是没有必要分开value bean和utility bean的,但在具有复杂业务逻辑的web应用程序中就可以用value bean实现对表单输入的捕获、保存,减少对数据库中那些值几乎不变的实体的访问,或将value bean放在一定作用域内使此作用域内的多个jsp页面共享。用utility bean完成操作数据库、数据处理等业务逻辑,以value bean 或页面传递的值为参数。
补充:Web开发 , Jsp ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,