used to thinkJavabenanare someJavaclass.later viewa number of Wikipedia .JavaBeanis aJavaComponent Technology,and follow some conventions ..Not very understanding.
What is javabean?
? In the JSP program used to encapsulate business logic , database operations and classes for entities .
How to understand the above component technology ?
these are writtenJavaclass,be placed in a specific package,the ability to package these classes asJarand other formats,Strong reusability,Compile once,can be arbitrarily executed.
What to follow attribute constraints?
1 by Property , Method,event composition .
2 must have Public the non-participating constructors
3 class naming Xxxbean
In a comprehensive view , JavaBean is the class that we used to encapsulate a non-participating constructor , This class has attribute methods, etc. .
We use examples to see
JavaBean in User class
Package Com.bjpower.drp.sysmgr.domain;import Java.util.date;public class User {public String getUserId () {return userId ;} public void Setuserid (String userId) {this.userid = userId;} Public String GetUserName () {return userName;} public void Setusername (String userName) {this.username = UserName;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;} Public String Getcontacttel () {return contacttel== null? "": Contacttel;} public void Setcontacttel (String contacttel) {This.contacttel = Contacttel;} Public String Getemail () {return email = = NULL?"": email;} public void Setemail (String email) {this.email = email;} Public Date Getcreatedate () {return createdate;} public void Setcreatedate (Date createdate) {this.createdate = CreateDate;} Useridprivate string userid;//usernameprivate string username;//passwordprivate string Password;private string conta Cttel;private String email;//util dateprivate Date createdate;}
in this class We set the member variable to private, public And for getxxxx ( variable name ).
Control class
Package Com.bjpower.sysmgr;//c+f+oimport Java.sql.connection;import Java.sql.preparedstatement;import Java.sql.resultset;import Java.sql.sqlexception;import Java.sql.statement;import Java.sql.Timestamp;import Java.util.arraylist;import Java.util.date;import Java.util.list;import Com.bjpower.drp.sysmgr.domain.user;import Com.bjpower.drp.util.dbutil;import com.bjpower.drp.util.pagemodel;/** * Using a single case mode * @author V-hanyk * */public class Usermanager {private static Usermanager instance=null;private Usermanager () {}//Portal method public static synchronized Usermanager Getusermangwer () {if (instance== null) {instance = new Usermanager ();} return instance;} @Overrideprotected void Doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {/ /must show call parent class method if (Constants.ADD.equals (GetCommand ())) {ADD (req, resp);} else if (Constants.DEL.equals (GetCommand ())) { Delete (req, resp);} else if (Constants.MOIDFY.equals (GetCommand ())) {Modify (req, resp);} else if (Constants.SHOW_ADD.equals (GetCommand ())) {Showadd (req, resp);} else if (Constants.QUERY.equals (GetCommand ())) {//Query Finduserbyid (req, resp);}} /** * User code query User * @param req* @param resq * @return * @throws SQLException */public void Finduserbyid (httpservletrequest Req, HttpServletResponse resp) throws Exception {StringBuffer sql= new StringBuffer (); Sql.append ("Select user_id, User_ Name, password, Contact_tel, email, create_date from T_user where user_id=? "); Connection Conn=null; PreparedStatement Pstmt=null; ResultSet rs= null; User User=null; String userid=req.getparameter ("UserId"), try{conn= dbutil.getconnection ();p stmt=conn.preparestatement ( Sql.tostring ());p stmt.setstring (1, userId); Rs=pstmt.executequery (); if (Rs.next ()) {user=new user (); User.setuserid ( Rs.getstring ("user_id")), User.setusername (rs.getstring ("user_name")), User.setpassword (rs.getstring ("password") ); User.setcontacttel (rs.getstring ("Contact_tel")); User.setemail (rs.getstring ("email")); User.setcreatedate ( Rs.gettimestamp ("Create_date"));} Req.setattribute ("User", user);//Forwarding. With Resq and Resq, if redirected. There is no data resp.getrequestdispatcher ("flowcard/modifyuser.jsp") if there is no req,resq. Forward (req, resp);} catch (SQLException e) {e.printstacktrace ();} Finally{dbutil.close (RS);D butil.close (pstmt);D butil.close (conn);} return user;}}
?
Control class is a simple business processing class .
View page Get JavaBean the Data
through EL An expression
<table width= "95%" border= "0" cellpadding= "0" cellspacing= "0" ><tr><td width= "22%" height= "><" Div align= "Right" > user code:? </div></td><td width= "78%" ><input name= "userId" type= "text" class= " Text1 "id=" userId "size=" "Maxlength=" readonly= "true" Value=${user. ID}></TD></TR><TR><TD height= "><div" align= "right" ><font color= "#FF0000" >*</font> user name:? </div></td><td><input name= "UserName" type= "text" class= "Text1" id= " UserName "size=" "Maxlength=" value=${user.username}></td></tr>
page Show we used the EL expression, than ever getting a user value=<%=user.getusername ()%>> It's going to be easy. But one thing to understand is that we are using the Private member Variable of the class in JavaBean , which is no longer the getUserId property.
Summary:
through some specific understanding. Recognize javabean mvc
? in the MVC in the design model is model, also called model , in a general program , We call it the data layer . , is to set the properties of the data and some behavior Get with the set.
?
?
Three questions JavaBean