浏览 5174 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-05-16
本人写的demo比较简单,只是最简单的分页查询,以后会继续增加更改功能。并且通过IE7与Firefox测试。还是请大家查看具体代码比较好。对于使用Ext所需要的准备工作在此不再叙述。google或baidu上搜一下就会比较多,本人推荐(http://www.dojochina.com/index.php?q=node/547)。 1、前台JS文件 // JavaScript Document function sexRender(value){ if (value == '1') { return "<span style='color:red;font-weight:bold;'>男</span>"; } else if(value=='2'){ return "<span style='color:green;font-weight:bold;'>女</span>"; }else{ return "<span style='color:red;font-weight:bold;'>未定义</span>"; } } Ext.onReady(function(){ // create the Data Store var store = new Ext.data.JsonStore({ root: 'rows', totalProperty: 'results', idProperty: 'id', //remoteSort: true, fields: ['id','stuId','name','sex','phone'], // load using script tags for cross domain, if the data in on the same domain as // this page, an HttpProxy would be better proxy: new Ext.data.HttpProxy({ url: "../studentwebdemo/admin/student/Com_getStudent.action" }) }); store.setDefaultSort('id', 'asc'); var pagingBar = new Ext.PagingToolbar({ pageSize: 25, store: store, displayInfo: true, displayMsg: '显示学生信息 {0} - {1} 条,共有{2}条记录', emptyMsg: "没有学生信息" }); var grid = new Ext.grid.GridPanel({ store: store, columns:[{id:"id",header: "序号", width: 160, sortable: true, dataIndex: "id"}, {header: "学号", width: 75, sortable: true, dataIndex: "stuId"}, {header: "姓名", width: 75, sortable: true, dataIndex: "name"}, {header: "性别", width: 75, sortable: true,renderer:sexRender,dataIndex:"sex"}, {header: "电话", width: 85, sortable: true, dataIndex: "phone"}], stripeRows: true, autoExpandColumn: "id", height:350, width:600, title:"学生信息", bbar:pagingBar }); grid.render('studentDiv');//studentDiv为显示页面的指定div store.load({params:{start:0, limit:25}});//start与limit会在通过后台HttpServletRequest获取 })
2、后台代码 1)Struts Action package com.javaeye.sunjiesh.studentwebdemo.web; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; import org.apache.struts2.ServletActionContext; import com.javaeye.sunjiesh.studentwebdemo.entity.Student; import com.javaeye.sunjiesh.studentwebdemo.service.StudentManager; import com.opensymphony.xwork2.ActionSupport; public class StudentAction extends ActionSupport { private static final long serialVersionUID = -4529678295440228555L; private StudentManager studentManager; public StudentManager getStudentManager() { return studentManager; } public void setStudentManager(StudentManager studentManager) { this.studentManager = studentManager; } /** * Ext后台分页得到学生信息 * * @return */ public String getStudent() { HttpServletRequest request = ServletActionContext.getRequest(); //int page=Integer.parseInt(request.getParameter("pages")); //Enumeration params=request.getParameterNames(); int start = Integer.parseInt(request.getParameter("start")); int limit = Integer.parseInt(request.getParameter("limit")); List<Student> studentList = studentManager.getStudents(start, limit); int studentsCount=studentManager.getAllStudentsCount(); // if(studentsCount!=0){ // System.out.println("一共有"+studentsCount+"名学生"); // } JSONArray jsonArray = JSONArray.fromObject(studentList); // outputResult(jsonArray.toString()); // System.out.println("jsonArray.toString() is "+jsonArray.toString()); String allStudentsWithJson = "{\"results\":" + studentsCount + ",\"rows\":"; allStudentsWithJson += jsonArray.toString(); allStudentsWithJson += "}"; outputResult(allStudentsWithJson); System.out.println("allStudentsWithJson is " + allStudentsWithJson); return this.NONE; } public void outputResult(String result) { HttpServletResponse response = ServletActionContext.getResponse(); try { System.out.println(result); response.setHeader("ContentType", "text/json"); response.setCharacterEncoding("gbk"); PrintWriter pw = response.getWriter(); pw.write(result); pw.flush(); pw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 2)Service层 package com.javaeye.sunjiesh.studentwebdemo.service; import java.util.List; import com.javaeye.sunjiesh.studentwebdemo.dao.StudentDAO; import com.javaeye.sunjiesh.studentwebdemo.entity.Student; public class StudentManager { private StudentDAO studentDAO; public StudentDAO getStudentDAO() { return studentDAO; } public void setStudentDAO(StudentDAO studentDAO) { this.studentDAO = studentDAO; } public List<Student> getAllStudents(){ return studentDAO.getAll(); } public List<Student> getStudents(int firstResult,int maxResult){ return studentDAO.getStudents(firstResult, maxResult); } public int getAllStudentsCount(){ return studentDAO.getAllStudentsCount(); } } 3)持久层(Hibernate) package com.javaeye.sunjiesh.studentwebdemo.dao; import java.util.List; import org.hibernate.Query; import com.javaeye.sunjiesh.studentwebdemo.entity.Student; import com.opencs.common.BaseHibernateDao; public class StudentDAO extends BaseHibernateDao<Student>{ @SuppressWarnings("unchecked") public List<Student> getStudents(int firstResult,int maxResult){ String hql="from Student as student"; Query query=getSession().createQuery(hql); query.setFirstResult(firstResult); query.setMaxResults(maxResult); List<Student> students=(List<Student>)query.list(); return students; } public int getAllStudentsCount(){ String hql="select count(*) from Student as student"; Query query=getSession().createQuery(hql); Object countObj=query.uniqueResult(); Integer count=0; if(countObj!=null){ count=Integer.parseInt(String.valueOf(countObj)); } return count.intValue(); } } 3、相关配置文件 1)struts.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <include file="com/javaeye/sunjiesh/studentwebdemo/struts2/struts-admin.xml" /> </struts> 2)struts.properties struts.action.extension=do,action struts.objectFactory=spring struts.enable.DynamicMethodInvocation=false struts.devMode=true struts.locale=zh_CN struts.i18n.encoding=GBK struts.ui.theme=simple #struts.custom.i18n.resources=i18n struts.ui.templateDir=/WEB-INF/template #struts.multipart.saveDir=temp/ struts.multipart.maxSize=8388608 struts.enable.SlashesInActionNames=true #struts.objectFactory=com.opensymphony.xwork2.ObjectFactory 3)struts-admin.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="student.manager" namespace="/admin/student" extends="struts-default"> <action name="Com_*" method="{1}" class="com.javaeye.sunjiesh.studentwebdemo.web.StudentAction"> <result name="list">/admin/user/list.html </result> <result name="add">/admin/user/add.html</result> <result name="edit">/admin/user/edit.html </result> <result name="editPassword">/admin/user/edit_password.html </result> </action> </package> </struts> 4)spring-base.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <!-- 属性文件读入 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:/jdbc.properties</value> </list> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mappingDirectoryLocations"> <list> <value>classpath:/com/javaeye/sunjiesh/studentwebdemo/spring/hbm/</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.connection.release_mode"> auto </prop> <prop key="hibernate.show_sql">true</prop> <prop key="connection.useUnicode">true</prop> <prop key="connection.characterEncoding">gbk</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!--scope需要注明,否则会报错 --> <bean id="baseTxService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" scope="prototype" abstract="true" > <property name="transactionManager" ref="transactionManager" /> <property name="proxyTargetClass" value="true" /> <property name="transactionAttributes"> <props> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="remove*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> </beans> 5)spring-dao.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans default-autowire="byName" default-lazy-init="true"> <bean id="studentDAO" parent="baseTxService" autowire="byName" lazy-init="true"> <property name="target"> <bean class="com.javaeye.sunjiesh.studentwebdemo.dao.StudentDAO"/> </property> </bean> </beans> 6)spring-beans.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean id="studentManager" class="com.javaeye.sunjiesh.studentwebdemo.service.StudentManager"> <property name="studentDAO" ref="studentDAO"></property> </bean> </beans> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-08-17
spencer 写道
softherk 写道
麻烦能不能把源码发给我一份啊,不胜感激 hrkai@163.com
能不能给我也发一份?谢谢! laodaozei@163.com
|
|
返回顶楼 | |