Struts2+jQuery+Hibernate自动提示框


使用struts2+jquery+hibernate实现了一个自动提示框

工程里使用了json插件(注意使用的json插件版本要和struts版本相对应,这里我用的是jsonplugin-0.33.jar和struts2-core-2.3.1.1.jar)

jsp页面为(注意这里引入了jquery-1.6.js和jquery-ui-1.8.10.custom.min.js):

  1. <%@ page contentType="text/html; charset=gbk" pageEncoding="gbk"%> 
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  3. <html> 
  4. <head> 
  5. <meta http-equiv="Content-Type" content="text/html; charset=gbk"> 
  6. <title>自动提示框</title> 
  7. <script type="text/javascript" src="js/jquery-1.6.js"></script> 
  8. <script type="text/javascript" src="js/jquery-ui-1.8.10.custom.min.js"></script> 
  9. <script type="text/javascript"> 
  10. $(function(){ 
  11.     $("#autoName").autocomplete({ 
  12.         minLength : 1, 
  13.         source : function(request,response){ 
  14.             var studentNameIndex =$("#autoName").val(); 
  15.             var url = "ajaxStudentName.action"
  16.             var params = { 
  17.                 //增加encodeURI以支持中文 
  18.                 'studentNameIndex':encodeURI(studentNameIndex) 
  19.             }; 
  20.             $.post(url, params, function callback(result,textStatus){ 
  21.                 alert(result); 
  22.                 if(testStatus = 'success'){ 
  23.                     if(result!=''){ 
  24.                         var tmp = result.split(","); 
  25.                         response(tmp); 
  26.                     }else{ 
  27.                         response(result); 
  28.                     } 
  29.                 } 
  30.             }); 
  31.         } 
  32.     }); 
  33. }); 
  34. </script> 
  35. </head> 
  36. <body> 
  37. <input id="autoName" name="studentName" maxlength="10"/> 
  38. </body> 
  39. </html> 

struts的配置文件为(注意这里继承的是extends="json-default"):

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 
  3.     "http://struts.apache.org/dtds/struts-2.1.dtd"> 
  4. <struts> 
  5.     <constant name="struts.devMode" value="true" /> 
  6.  
  7.     <package name="default" extends="json-default" namespace="/"> 
  8.         <action name="ajaxStudentName"   
  9.                  class="com.test.action.AjaxAction" method="getStudentName">   
  10.         </action>   
  11.     </package> 
  12. </struts> 

struts action:

  1. package com.test.action; 
  2.  
  3. import java.net.URLDecoder; 
  4. import java.util.Iterator; 
  5. import java.util.List; 
  6. import org.apache.struts2.ServletActionContext; 
  7. import com.opensymphony.xwork2.ActionSupport; 
  8. import com.test.dao.StudentDAO; 
  9.  
  10. public class AjaxAction extends ActionSupport{ 
  11.     private static final long serialVersionUID = 1L; 
  12.     private String studentNameIndex; 
  13.     private String result; 
  14.      
  15.     public String execute() throws Exception{ 
  16.         return SUCCESS; 
  17.     } 
  18.      
  19.     public String getStudentName() throws Exception{ 
  20.         studentNameIndex = URLDecoder.decode(studentNameIndex, "utf-8"); 
  21.         StudentDAO studentDAO = new StudentDAO(); 
  22.         List<String> re = studentDAO.getStudentName(studentNameIndex); 
  23.         re.add("123"); 
  24.         result = ""
  25.         if(re!=null && re.size()>0){ 
  26.             Iterator<String> it = re.iterator(); 
  27.             while(it.hasNext()){ 
  28.                 String tmp = it.next(); 
  29.                 result = result + tmp + ","
  30.             } 
  31.             result = result.substring(0, result.length()-1); 
  32.         } 
  33.         ServletActionContext.getResponse().setContentType("gbk"); 
  34.         ServletActionContext.getResponse().setCharacterEncoding("gbk"); 
  35.                 ServletActionContext.getResponse().getWriter().print(result); 
  36.         return null
  37.     } 
  38.  
  39.     public String getResult() { 
  40.         return result; 
  41.     } 
  42.  
  43.     public void setResult(String result) { 
  44.         this.result = result; 
  45.     } 
  46.  
  47.     public String getStudentNameIndex() { 
  48.         return studentNameIndex; 
  49.     } 
  50.  
  51.     public void setStudentNameIndex(String studentNameIndex) { 
  52.         this.studentNameIndex = studentNameIndex; 
  53.     } 

hibernate实现的dao:

  1. package com.test.dao; 
  2.  
  3. import java.util.List; 
  4. import org.hibernate.Query; 
  5. import org.hibernate.Session; 
  6. import org.hibernate.SessionFactory; 
  7. import org.hibernate.cfg.Configuration; 
  8.  
  9. public class StudentDAO { 
  10.     public List<String> getStudentName(String studentName){ 
  11.         Configuration conf = new Configuration();   
  12.         SessionFactory sessionFactory = conf.configure().buildSessionFactory();   
  13.         Session session = sessionFactory.openSession();   
  14.         String sql =   
  15.         "select studentName from Student where studentName like '"+studentName+"%'"
  16.         Query query = session.createQuery(sql); 
  17.         List<String> result = null
  18.         result = query.list(); 
  19.         return result; 
  20.     } 

hibernate实现的po:

  1. package com.test.po; 
  2.  
  3. import java.io.Serializable; 
  4.  
  5. public class Student implements Serializable { 
  6.  
  7.     private static final long serialVersionUID = 1L; 
  8.     private Integer studentId; 
  9.     private Integer studentName; 
  10.  
  11.     public Student(){ 
  12.     } 
  13.  
  14.     public Integer getStudentId() { 
  15.         return studentId; 
  16.     } 
  17.  
  18.     public void setStudentId(Integer studentId) { 
  19.         this.studentId = studentId; 
  20.     } 
  21.  
  22.     public Integer getStudentName() { 
  23.         return studentName; 
  24.     } 
  25.  
  26.     public void setStudentName(Integer studentName) { 
  27.         this.studentName = studentName; 
  28.     } 

相应的配置文件为:

  1. <?xml version="1.0"?> 
  2. <!DOCTYPE hibernate-mapping PUBLIC 
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > 
  5.  
  6. <hibernate-mapping> 
  7.     <class name="com.test.po.Student"   
  8.         table="D_STUDENT"> 
  9.         <id name="studentId" column="studentId" type="java.lang.Integer"> 
  10.             <generator class="assigned"/> 
  11.         </id> 
  12.         <property   
  13.             name="studentName" 
  14.             column="studentName" 
  15.             update="true" 
  16.             insert="true" 
  17.             type="java.lang.String" 
  18.             not-null="true" 
  19.             unique="false" 
  20.             length="10" 
  21.         /> 
  22.     </class> 
  23. </hibernate-mapping> 

hibernate配置文件:

  1. <?xml version='1.0' encoding='utf-8'?> 
  2. <!DOCTYPE hibernate-configuration PUBLIC 
  3.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
  4.         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
  5.  
  6. <hibernate-configuration> 
  7.     <session-factory> 
  8.         <property name="show_sql">false</property> 
  9.      
  10.         <property name="dialect">org.hibernate.dialect.OracleDialect</property> 
  11.         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 
  12.         <property name="connection.url">jdbc:oracle:thin:@192.168.1.1:1522:TESTDB</property> 
  13.         <property name="connection.username">123</property> 
  14.         <property name="connection.password">123</property> 
  15.         <property name=" hibernate.jdbc.batch_size">100</property> 
  16.  
  17.         <mapping resource="com/test/po/student.hbm.xml" /> 
  18.     </session-factory> 
  19. </hibernate-configuration> 

web.xml:

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <web-app id="WebApp_ID" version="2.4"   
  3.         xmlns="http://java.sun.com/xml/ns/j2ee"   
  4.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  6.         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd'> 
  7.     <display-name>test</display-name> 
  8.     <filter>   
  9.         <filter-name>struts2</filter-name>   
  10.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>   
  11.     </filter>   
  12.     <filter-mapping>   
  13.         <filter-name>struts2</filter-name>   
  14.         <url-pattern>*.action</url-pattern>   
  15.     </filter-mapping>   
  16.     <filter-mapping>   
  17.         <filter-name>struts2</filter-name>   
  18.         <url-pattern>*.jsp</url-pattern>   
  19.     </filter-mapping>   
  20.      
  21.     <welcome-file-list> 
  22.         <welcome-file>index.jsp</welcome-file> 
  23.     </welcome-file-list> 
  24. </web-app> 

student表的结构只有2列,一列为编号,一列为姓名:

  1. -- Create table  
  2. create table D_STUDENT 
  3.   STUDENTID   NUMBER not null
  4.   STUDENTNAME VARCHAR2(10) not null 

备注:

1,中文乱码需要特别注意,出现乱码要从3方面查找原因:1,前台传递到后台的数据是否是乱码。2,数据库中查询出的数据是否是乱码。3,后台返回给前台的查询结果是否是乱码。

2,输入需要过滤掉特殊字符或者其他的处理以防止sql注入。

相关内容