歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Struts2+jQuery+Hibernate自動提示框

Struts2+jQuery+Hibernate自動提示框

日期:2017/3/1 10:07:35   编辑:Linux編程

使用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. <package name="default" extends="json-default" namespace="/">
  7. <action name="ajaxStudentName"
  8. class="com.test.action.AjaxAction" method="getStudentName">
  9. </action>
  10. </package>
  11. </struts>

struts action:

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

hibernate實現的dao:

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

hibernate實現的po:

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

相應的配置文件為:

  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. <hibernate-mapping>
  6. <class name="com.test.po.Student"
  7. table="D_STUDENT">
  8. <id name="studentId" column="studentId" type="java.lang.Integer">
  9. <generator class="assigned"/>
  10. </id>
  11. <property
  12. name="studentName"
  13. column="studentName"
  14. update="true"
  15. insert="true"
  16. type="java.lang.String"
  17. not-null="true"
  18. unique="false"
  19. length="10"
  20. />
  21. </class>
  22. </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. <hibernate-configuration>
  6. <session-factory>
  7. <property name="show_sql">false</property>
  8. <property name="dialect">org.hibernate.dialect.OracleDialect</property>
  9. <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  10. <property name="connection.url">jdbc:oracle:thin:@192.168.1.1:1522:TESTDB</property>
  11. <property name="connection.username">123</property>
  12. <property name="connection.password">123</property>
  13. <property name=" hibernate.jdbc.batch_size">100</property>
  14. <mapping resource="com/test/po/student.hbm.xml" />
  15. </session-factory>
  16. </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. <welcome-file-list>
  21. <welcome-file>index.jsp</welcome-file>
  22. </welcome-file-list>
  23. </web-app>

student表的結構只有2列,一列為編號,一列為姓名:

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

備注:

1,中文亂碼需要特別注意,出現亂碼要從3方面查找原因:1,前台傳遞到後台的數據是否是亂碼。2,數據庫中查詢出的數據是否是亂碼。3,後台返回給前台的查詢結果是否是亂碼。

2,輸入需要過濾掉特殊字符或者其他的處理以防止sql注入。

Copyright © Linux教程網 All Rights Reserved