1、做的是一個 三聯下拉框,並通過最后一個下拉框顯示出員工信息。
- 這是請求正確的界面
- 這是經過多次請求后會出現問題
2. 環境用的是 Spring、Struts2、Hibernate,通過實體類映射創建表,通過 原生SQL查詢出的數據,用JSON數據格式返回。
- 實體類
package com.itcat.entity; public class Location { private Integer location_id; private String city; public Integer getLocation_id() { return location_id; } public void setLocation_id(Integer location_id) { this.location_id = location_id; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } }
- 映射文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.itcat.entity.Location" table="locations"> <id name="location_id" column="location_id"> <generator class="native"></generator> </id> <property name="city" column="city"></property> </class> </hibernate-mapping>
- Struts配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="root" namespace="/" extends="json-default"> <action name="findByCity" class="com.itcat.action.UserAction" method="findByCityInfo"> <result name="SUCCESS" type="json"></result> </action> </package> </struts>
- DaoImpl方法
public List<Location> findByCity() { List<Location> list = null; try { String sql = "SELECT " + "l.location_id," + "l.city " + "FROM " + "locations l"; SessionFactory sf = hibernateTemplate.getSessionFactory(); Session session=sf.openSession(); Query query = session.createSQLQuery(sql).addEntity(Location.class); list = query.list(); } catch (Exception e) { e.printStackTrace(); } return list; }
- action類
public String findByCityInfo() { List<Location> list = warterService.findByCity(); try { this.result = mapper.writeValueAsString(list); } catch (Exception e) { e.printStackTrace(); } return "SUCCESS"; }
經過十分鍾仔細的查看和調試,發現session沒有關閉,session.close()之后,問題沒有了。