I recently learned about Ajax asynchronous calling and refresh technologies, and I have put it into practice. I also learned from other people's methods and joined my own ideas. Now I have the following things ''''. If you have any questions, leave a message ''''
First, the front-end JSP page
About jquery POST method you can go to look at this, write very detailed: http://www.itivy.com/jquery/archive/2011/7/6/jquery-get-post-getjson-ajax.html
<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <% @ taglib uri ="/Struts-tags "prefix =" S "%> <% string Path = request. getcontextpath (); string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML>
Because the struts2 framework is used, the following code is added to struts2: because the data is transmitted and transferred, it must be written as extends = "JSON-Default"
<package name="strutsjson" extends="json-default"> <action name="checkusername" class="com.liumin.action.CheckUsername" method="CheckPerson"> <result type="json"></result> </action> </package>
The code in the action is as follows:
Package COM. liumin. action; import Java. util. list; import COM. liumin. model. person; import COM. liumin. service. impl. checkservice; import COM. opensymphony. xwork2.actionsupport; public class checkusername extends actionsupport {private string username; // data sent from the front-end: usernamepublic string GetUserName () {return username;} public void setusername (string username) {This. username = username;} private string result; Public s Tring getresult () {return result;} public void setresult (string result) {This. result = result;} Public String checkperson () throws exception {checkservice = new checkservice (); string STR = "select * From person where username = '" + username + "'"; // create an SQL statement list <person> List = (list <person>) checkservice. getuser (STR); // get the data if (list. isempty () // after the security condition query, determine whether the list is empty? {This. Result = "OK" ;}else {This. Result = "no" ;}return success ;}}
I wrote a piece of service and Dao layer:
Package COM. liumin. service. impl; import Java. util. list; import Org. hibernate. session; import Org. hibernate. transaction; import COM. liumin. model. person; import COM. liumin. util. hibernateutil; public class checkservice {@ suppresswarnings ("unchecked") public list <person> getuser (string Str) {session = hibernateutil. opensession (); transaction Tx = session. begintransaction (); List <person> List = NULL; try {Li St = session. createsqlquery (STR ). addentity (person. class ). list (); // query the data Tx. commit ();} catch (exception e) {If (null! = Tx) {Tx. rollback () ;}finally {hibernateutil. Close (Session) ;}return list ;}}
The data in util is as follows:
package com.liumin.util;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtil{private static SessionFactory sessionFactory;static{try{sessionFactory = new Configuration().configure().buildSessionFactory();}catch (Exception ex){ex.printStackTrace();}}public static Session openSession(){Session session = sessionFactory.openSession();return session;}public static void close(Session session){if(null != session){session.close();}}}
You don't need to post the cfg and HBM codes of hibernate!