Easyui used for background management is very convenient, these two days added a few small features one of them is to add an announcement. Can include an announcement from the front desk. There are backstage to the administrator to see the announcement. The backend implementation is still very rough. In the actual project, the location of the announcement and the timing should be fixed. So you can define these announcements in advance, creating a table structure in which the specified primary key uniquely identifies the announcements. By the time the primary key gets the announcement. But because I need it now is not clear. Don't know how many announcements are needed. So when you add an announcement, you need the administrator to specify a primary key for the announcement. When the project is completed, add the announcement can no longer let the administrator primary key, after all, he is not a programmer. 1. Announcement When an administrator clicks Post Management in the menu bar, the above window appears on the right. The previous blog has explained how to use the DataGrid control to fetch data from the background, as well as elegant implementation of paging functionality. There's not much to say here. Now all you have to do is add, edit, and delete functions. Now come on. Let's start with a new announcement. 2. Create a new announcement. 2.1 Establishing the database table structure
where notice is the primary key that marks the announcement, the name, and the programmer queries the announcement through this field. So to be set as a candidate key, must not repeat, I do not set here, in the mind to understand the line, description as with the notice with the appearance. is an explanation of notice. Indicates the location, function of this announcement. Title marks the caption of the announcement. Content indicates the contents of the announcement. Time as a simple table. There is no need to add other fields, (including the modified person to add, the required permissions "I think the permissions are best defined in the table structure, relying solely on the functionality provided by the database, or not very good", modify the ID (about the modified information, then can call up the change of the announcement), other temporarily did not think) 2.2 Define the Dao,service layer. I do not want to say more about these two layers, almost write vomit, I use Hibernate, this period of time has been encapsulated. Define the Basedao, and the standard realization of adding and deleting. Basically, each table's DAO directly inherits this class. Other functions are not required. On the Code bar
public class Basedao {public sessionfactory sessionfactory;
Private Session Session=null; /** * @param obj * @return */public void closesession () {if (session!
=null) {//رsession session.close (); }} public Basedao () {} public Basedao (sessionfactory SF) {Sessio
Nfactory = SF;
Hibernateutil.setsessionfactory (sessionfactory);
} public boolean Add (Object obj) {Transaction tran=null;
Boolean result=false;
try {Session session= sessionfactory.opensession ();
Tran=session.begintransaction ();
Session.save (obj);
Tran.commit ();
Result=true; } catch (Exception e) {if (tran!=null)
{//عtran.rollback ();
}} finally {if (session!=null) {
Session.close ();
}} return result; }/** * @return *ϊĵid */Public Boolean
Update (Object object) {//Session session=null;
Transaction Tran=null;
Boolean result=false;
try {Session session= sessionfactory.opensession ();
Tran=session.begintransaction ();
Session.update (object);
Tran.commit ();
Result=true;
} catch (Exception e) {if (tran!=null) { عTran.rollback ();
}} finally {if (session!=null) {
رsession Session.close ();
}} return result; }/** * @param c * @param idѯһݸid * @return *
/Public Object get (Class c,int ID) {//session session=null;
Object Object=null;
try {Session session= sessionfactory.opensession ();
Object=session.get (C,id);
} catch (Exception e) {} finally {
if (session!=null) {//رsession Session.close ();
}} return object; }/** * @param obj * @return ɾ*/public boolean delete (Object ob
j) {//Session session=null;
Transaction Tran=null;
Boolean result=false;
try {Session session= sessionfactory.opensession ();
Tran=session.begintransaction ();
Session.delete (obj);
Tran.commit ();
Result=true;
} catch (Exception e) {if (tran!=null) {
عtran.rollback ();
}} finally {if (session!=null) {
رsession Session.close ();
}} return result; }/** * @param <t>ѯ¼* @param SQL SQL * @param param * @return */@SuppressWarnings ("unchecked") <span style= "color: #ff0000;" > Public <T> list<t> query (String sql,object[] param) </span> {list< ;
T> list=new arraylist<t> ();
Session Session=null;
try {Session session= sessionfactory.opensession ();
Query query=session.createquery (SQL);
if (Param!=null) {for (int i= 0;i<param.length;i++)
{Query.setparameter (i,param[i]); }} list=query.list();
} catch (Exception e) {e.printstacktrace ();
} finally {if (session!=null) {
Session.close ();
}} return list; }
/**
<span style= "FONT-SIZE:24PX; White-space:pre; " > </span><span style= "font-size:24px;color: #ff0000;" ><strong>* This function has a problem </strong></span><span style= "FONT-SIZE:24PX;" > * @param SQL * @param paramѯ¼* @return */<span style= "color: #33cc00; "
>public Object Queryone (String sql,object[] param) </span> {object object=null;
Session Session=null;
try {Session session= sessionfactory.opensession ();
Query query=session.createquery (SQL);
if (Param!=null) {for (int i=0; i<param.length;i++)
{Query.setparameter (i,param[i]);
} object=query.uniqueresult (); }} catch (Exception E) {} finally {if (session!=null)
{Session.close ();
}} return object; }/** * @param <T> * @param SQL * @param param * @param page * @param size * @return ʵַҳѯ*/@SuppressWarnings ("unchecked") <span style= "COLOR: #f f0000; "
> Public <T> list<t> querybypage (String sql,string[] param,int page,int size) </span> {
List<t> list=new arraylist<t> ();
try {Session session= sessionfactory.opensession ();
Query query=session.createquery (SQL);
if (Param!=null) {for (int i= 0;i<param.length;i++) {query.setstring (i,param[i]);
}}//ɸѡquery.setfirstresult ((page-1) *size);
Query.setmaxresults (size);
List=query.list ();
} catch (Exception e) {} finally {
if (session!=null) {session.close ();
}} return list; }/** * @param hql * @param pras * @return ݸ*/<span Style = "COLOR: #ff6666;"
>public int GetCount (String hql, string[] pras) {</span> int resu = 0;
Session s = null;
try {Session session=sessionfactory.opensession ();
Query q = session.createquery (HQL); if (Pras! = null) {for (int i =0; i < pras.length; i++) {Q.SETST
Ring (I, pras[i]);
}} ResU = Q.list (). Size ();
} catch (Exception e) {e.printstacktrace ();
} finally {if (session! = NULL) {session.close ();
}} return resu;
} public Session getsession () {return session;
} public void Setsession (session session) {this.session = session;
} public static void Main (string[] args) {/* New Basedao (Hibernateutil.getsessionfactory ());
Category CA = (category) Queryone ("from Category CA where ca.category_id = 3", null); System.out.println (Ca.getcategory_remark ());
*/} public void Setsessionfactory (Sessionfactory sessionfactory) {this.sessionfactory = sessionfactory; }}</span>
This class I was from the online down, has not found the author, thank you, helped me a lot of work, but also for his headache, because in the beginning will not hibernate, down it, and then a large number of projects to use this class, This class includes a Util class that reads the XML file initialized for hibernate, then adds spring functionality later, changes all the initialization hibernate parts, and requires spring to manage hibernate. There is also session management. These two whole I'm in a burn. A little bit of a clue has recently been given to acknowledge that Hibernate has a strong mapping of the conversations we provide. (very convenient, later to buy a book of Hibernate, as a support, I heard that now is not.) )
Accents is the main use of the function, add the wrong function of green, and other additions and deletions to change the function. Basically inherit this class, the DAO layer does not have to write code. Weak DAO, strong service, put all the business code, query statements into the service. Which service I defined a Baseservice abstract template class, this is very common, do not paste Code 2.3 control layer action, basically is add,update,delete,query, and then paged query. Part of the MVC portion of code is pasted
public void Addnotice ()
{
noticeservice.addnotice (notice, title, content,description);
System.out.println ("title" +title+ "Notice:" +notice+ "Content:" +content ");
Jsonobject jsonobject = Jsonutil.newjsonobject ();
msg = "Change announcement has been saved";
Jsonobject.put ("msg", msg);
try {
jsonutil.responseprint (jsonobject);
} catch (IOException e) {
e.printstacktrace ()}
}
MSG is returned to the front end. And JSON has been explained in a previous blog post. 2.5 View Layer
The specific function has, the user first clicks the text box prompt text to disappear, this adds the announcement content the tool is CKEditor, the function is still very powerful. Can basically realize the blog function, and finally converted to HTML format text, saved in the database, the call format will not change, if it is the actual project I think it is necessary to consider the feasibility of testing, after all, the location of the announcement is different. You can't let the administrator try it over and over again, so change it, they will collapse. This is the art of the former, I do not worry about, to tell the truth I write Easyui blog is because this tool is very powerful, very convenient, after the outside rejoined backstage part completely without art, front-end workers. Also over a period of time there is a micro-site, need to support the background management of the CMS system needs. So try hackers on my little website first.
Then one click Save is sent asynchronously to the control layer via Ajax, and then saved in the database, if the successful interface is returned to the Announcements list, otherwise it will be stopped in this interface. And explain the reasons for failure, (failure reasons I did not write, too lazy to write.) I think this is the difference between writing and rejoined to do the actual enterprise-level project, the power is not enough, always want to practice technology. Do not want to study business needs)
<div id= "CC" class= "Easyui-layout" style= "width:100%;height:100%;" > <div id= ' North ' data-options= "region: ' North ', Iconcls: ' Icon-add ', title: ' Please enter Bulletin title ', Split:true ' style= ' width : 50%;height:78px; " > <textarea id= "title" style= "font-size:15pt" rows= "1" maxlength= "title=" Please enter the post title, the maximum length is 50 characters "> < ;/textarea> </div> <div id= "East" data-options= "region: ' East ', Iconcls: ' Icon-add ', title: ' Please add the name and description of the announcement ', split:true "style=" width:300px; "
> <textarea id= "notice" style= "font-size:15pt" maxlength= "title=" Please enter the name of the Bulletin > </textarea> <br/><br/><br/> <textarea id= "description" style= "font-size:15pt" maxlength= "title=" Please enter the public Description of the report. Maximum 100 characters "> </textarea> <div align=" center "> <br/><br/><br/> < Input type= "image" Id= "Save" src= "Image/save.gif" ></input> </div> </div> <div id= "Center" data-options= "Region: ' Center ', title: ' Please add announcement content ', spilt:true" style= "Padding:5px;background: #eee;" > <ckfinder:setupckeditor editor= "Content" basepath= "ckfinder/"/> <%--<ckeditor:editor B Asepath= "ckeditor/" editor= "content"/>--%> <textarea class= "CKEditor" style= "font-size:15pt" cols= "id=" Content "name=" content "rows=" > <s:property value= "#session. Content" ></s:property> </textarea >
Front-end code. JS code and the previous blog is not much different, this is to verify that the user first click the text box, let the hint text disappear code
var titleval = ' Please enter title ';
var contentval = ' Please enter content ';
var noticeval = ' Please enter the name of the announcement, the name used by the programmer to get announcements from the background through the field ';
var descriptionval = ' Please enter a description of the announcement ';
$ (' #title '). Text (titleval);
$ (' #content '). Text (contentval);
$ (' #notice '). Text (noticeval);
$ (' #description '). Text (descriptionval); $ (' #description '). Click (function () {if ($ (' #description '). Val (). replace (/\s+/g, "") ==descriptionval) {$ (' #descrip
tion '). Text ("");
}
});
$ (' #title '). Click (function () {if ($ (' #title '). Val (). replace (/\s+/g, "") ==titleval) {$ (' #title '). Text ("");
}
});
/* $ (' #content '). Click (function () {});
*/$ (' #notice '). Click (function (e) {if ($). Val (). replace (/\s+/g, "") ==noticeval) {$ (this). Text ("");
}
}); function DeleteText () {if ($ (' #content '). Val (). replace (/\s+/g, "") ==contentval) {CKEDITOR.instances.conten
T.setdata (");
}} ckeditor.instances["Content"].on ("Instanceready", function () { Set KeyUp Event/* This.document.on ("KeyUp", DeleteText);
*///and Click event This.document.on ("click", DeleteText); and select Event/* This.document.on ("select", DeleteText); */
});
This is the Add Announcement 3. Update announcement It's a little bit more complicated here. Editors and new announcements in the previous paragraph is not very different, we need to do is when the user click on the Edit button, get the corresponding bulletin ID, send it back to the background, and then the background to the program to edit the announcement interface, while in the session to add the corresponding information, in the previous paragraph to get, in the appropriate location display. The request strain for the Sava button is also the update request. to the updatenoticeaction. Then, after the update succeeds, the interface goes to the Announcements list. 4 Delete post get to the corresponding bulletin ID and then delete, previous blog has
Conclusion. Easyui enables back-office management to be elegant, leveraging jquery's powerful features to make Web sites as beautiful and even prettier as desktop apps. I should learn to design patterns, every day so look at the front-end, feel that they want to do the front-end. Follow-up may also add some Easyui blogs