Hibernate+struts+mysql+dwr(/*接受返回对象List)绑定下拉框
Hibernate+struts+mysql+dwr(/*接受返回对象List)绑定下拉框
<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. --><hibernate-configuration><session-factory><property name="connection.username">root</property><property name="connection.url">jdbc:mysql://localhost:3306/test</property><property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="myeclipse.connection.profile">dd</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="show_sql">true</property><mapping resource="com/accp/entity/Student.hbm.xml" /></session-factory></hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> </servlet> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd"><dwr><allow><convert match="com.accp.entity.Student" converter="bean"><param name="include" value="id,username,password,age" /></convert><create creator="new" javascript="test"><param name="class" value="com.accp.test.test"/><include method="selList"/> </create></allow><signatures><![CDATA[import java.util.List;import com.accp.entity.Student;import com.accp.test.test;test.selList(List<Student>);]]></signatures></dwr>
package com.accp.test;import java.util.List;import org.hibernate.Transaction;import com.accp.entity.Student;import com.accp.entity.StudentDAO;public class test {/** * @param args */public int Mytest(){StudentDAO stuDao=new StudentDAO();try{Student stu=new Student();stu.setPassword("123");stu.setAge(22);stu.setUsername("chen");Transaction tran=stuDao.getSession().beginTransaction();stuDao.save(stu);tran.commit();return 1;}catch(Exception ex){ex.printStackTrace();return 0;}finally{stuDao.getSession().close();}}public List selList(){StudentDAO stuDao=new StudentDAO();try{Transaction tra=stuDao.getSession().beginTransaction();List<Student> list=stuDao.findAll();return list;}catch(Exception ex){ex.printStackTrace();return null;}finally{stuDao.getSession().close();}}public static void main(String[] args) {// TODO Auto-generated method stub}}和JavaBeanpackage com.accp.entity;/** * Student generated by MyEclipse Persistence Tools */public class Student implements java.io.Serializable {// Fieldsprivate Integer id;private String username;private String password;private Integer age;// Constructors/** default constructor */public Student() {}/** minimal constructor */public Student(String username, String password) {this.username = username;this.password = password;}/** full constructor */public Student(String username, String password, Integer age) {this.username = username;this.password = password;this.age = age;}// Property accessorspublic Integer getId() {return this.id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return this.username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return this.password;}public void setPassword(String password) {this.password = password;}public Integer getAge() {return this.age;}public void setAge(Integer age) {this.age = age;}}
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%><%@ taglib uri="/WEB-INF/struts-html.tld" prefix="h" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <script type='text/javascript' src='/Dwr/dwr/interface/test.js'></script> <script type='text/javascript' src='/Dwr/dwr/engine.js'></script> <script type='text/javascript' src='/Dwr/dwr/util.js'></script> <script type="text/javascript"> function selShow(){ test.selList(call); } function call(data){ DWRUtil.addOptions("chen",[ 'Africa','America','Asia','Australasia','Europe' ]); DWRUtil.addOptions("shu",data,"id","username"); DWRUtil.addOptions("liu",data,"id","username"); DWRUtil.addOptions("group",data,"id","username"); } </script> </head> <body> <h:form action="stu.do"> <h:text property="stu.id"></h:text> <h:select property="stu.id" styleId="group"></h:select> <input type="button" value="List" onclick="selShow()"/> <select id="chen"> </select> <select id="shu"> </select> <select name="liu"></select> </h:form> </body></html>