previously thoughtJavabenanare someJavaclass.later viewa number of Wikipedia .JavaBeanis aJavaComponent Technology,and follow some conventions ..I don't understand ..
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 written Span lang= "en-US" style= "Font-family:calibri" >java Span lang= "en-US" style= "Font-family:calibri", is placed in a package of attributes , You can package these bracketing jar Format Strong reusability Compile once Feel free to run
What to follow attribute constraints?
1 by Property , Method,event composition .
2 must have Public the parameter-free constructor
3 class naming Xxxbean
In a comprehensive view , JavaBean is the class that we previously encapsulated with a parameterless 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, set the property to 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 singleton 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);}} /** * Query users according to user code * @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),//redirect without Resq and Resq, if forwarding with req and reqs (inside) resp.sendredirect (Req.getcontextpath () + "/servlet/flowcard/ Flowcardservlet?command=q ");} 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, not JavaBean class in the.
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 An expression that is more than the previous fetch user value=<%=user.getusername ()%>> to be simple, but to be clear, we are using JavaBean Private member variables in class , is no longer getUserId property.
Summary:
through some detailed understanding , recognizing that javabean mvc javabean
in the MVC Design model, it is model, also known as models , in the 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