posts - 41,  comments - 8,  trackbacks - 0
struts2json plugin的位置在:http://code.google.com/p/jsonplugin/
下载json plugin的jar包,放到/WEB-INF/lib/目录下就可以了

Spring + Struts + JPA的项目结构如其他例子中的一致
首先是web.xml
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 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  6.     <display-name>quickstart</display-name>  
  7.     <filter>  
  8.         <filter-name>struts2</filter-name>  
  9.         <filter-class>  
  10.             org.apache.struts2.dispatcher.FilterDispatcher  
  11.         </filter-class>  
  12.     </filter>  
  13.     <filter>  
  14.         <filter-name>jpaFilter</filter-name>  
  15.         <filter-class>  
  16.             org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter  
  17.         </filter-class>  
  18.         <init-param>  
  19.             <param-name>entityManagerFactory</param-name>  
  20.             <param-value>entityManagerFactory</param-value>  
  21.         </init-param>  
  22.     </filter>  
  23.     <filter-mapping>  
  24.         <filter-name>jpaFilter</filter-name>  
  25.         <url-pattern>*.action</url-pattern>  
  26.     </filter-mapping>  
  27.     <filter-mapping>  
  28.         <filter-name>struts2</filter-name>  
  29.         <url-pattern>/*</url-pattern>  
  30.     </filter-mapping>  
  31.     <welcome-file-list>  
  32.         <welcome-file>index.jsp</welcome-file>  
  33.     </welcome-file-list>  
  34.     <listener>  
  35.         <listener-class>  
  36.             org.springframework.web.context.ContextLoaderListener  
  37.         </listener-class>  
  38.     </listener>  
  39. </web-app>  

加入jpaFilter,是为了不让hibernate的session过早关闭,因为有的action会通过ajax动态调用。
下面是struts.xml,注意struts.xml需要放在源代码目录下面:

xml 代码
 
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5. <struts>  
  6.     <constant name="struts.objectFactory" value="spring" />  
  7.     <constant name="struts.devMode" value="true" />  
  8.     <constant name="struts.i18n.encoding" value="UTF-8"/>  
  9.     <package name="person" extends="json-default">  
  10.         <action name="list" method="execute" class="personaction">  
  11.             <result type="json"/>  
  12.         </action>         
  13.     </package>  
  14. </struts>  

这里注意,
struts.objectFactory告诉struts所有的action都到spring的上下文里面去找,另外还需要注意,我们自己的包要继承自json-default,这样才可以在result的type属性中使用json
下面是spring的配置文件applicationContext.xml:
xml 代码
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="  
  7.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  8.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd  
  9.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
  10.     <bean  
  11.         class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />  
  12.     <bean id="entityManagerFactory"  
  13.         class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
  14.         <property name="dataSource" ref="dataSource" />  
  15.         <property name="jpaVendorAdapter">  
  16.             <bean  
  17.                 class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">  
  18.                 <property name="database" value="MYSQL" />  
  19.                 <property name="showSql" value="true" />  
  20.             </bean>  
  21.         </property>  
  22.     </bean>  
  23.     <bean id="dataSource"  
  24.         class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  25.         <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
  26.         <property name="url" value="jdbc:mysql://localhost/extjs" />  
  27.         <property name="username" value="root" />  
  28.         <property name="password" value="" />  
  29.     </bean>  
  30.   
  31.       
  32.     <bean id="transactionManager"  
  33.         class="org.springframework.orm.jpa.JpaTransactionManager">  
  34.         <property name="entityManagerFactory"  
  35.             ref="entityManagerFactory" />  
  36.     </bean>  
  37.     <tx:annotation-driven transaction-manager="transactionManager" />  
  38.     <!--Service 开始 -->  
  39.     <bean id="personService" class="com.myext.service.impl.PersonServiceJpaImpl"/>  
  40.     <bean id="personaction" class="com.myext.action.PersonPageAction">  
  41.         <property name="person" ref="personService"/>  
  42.     </bean>  
  43. </beans>  

这里的bean personaction和strutx.xml中的action class一致就可以了,下面是代码:
action:
java 代码
  1. package com.myext.action;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.myext.service.PersonService;
  5. public class PersonPageAction {
  6. private int limit=10;
  7. private int start=0;
  8. private PersonService person;
  9. private int total=0;
  10. private List persons = new ArrayList();
  11. private boolean success=true;
  12. public boolean getSuccess(){
  13. return this.success;
  14. }
  15. public void setLimit(int limit) {
  16. this.limit = limit;
  17. }
  18. public void setStart(int start) {
  19. this.start = start;
  20. }
  21. public void setPerson(PersonService person) {
  22. this.person = person;
  23. }
  24. public int getTotal() {
  25. return total;
  26. }
  27. public void setTotal(int total) {
  28. this.total = total;
  29. }
  30. public List getPersons() {
  31. return persons;
  32. }
  33. public void setPersons(List persons) {
  34. this.persons = persons;
  35. }
  36. public String execute(){
  37. this.total = person.getTotal();
  38. this.persons = person.getPage(this.start, this.limit);
  39. return "success";
  40. }
  41. }
service:
java 代码
  1. package com.myext.service.impl;
  2. import java.util.List;
  3. import javax.persistence.EntityManager;
  4. import javax.persistence.PersistenceContext;
  5. import javax.persistence.Query;
  6. import com.myext.model.Person;
  7. import com.myext.service.PersonService;
  8. public class PersonServiceJpaImpl implements PersonService {
  9. private EntityManager em;
  10. private static String poname = Person.class.getName();
  11. @PersistenceContext
  12. public void setEntityManager(EntityManager em){
  13. this.em = em;
  14. }
  15. @SuppressWarnings("unchecked")
  16. @Override
  17. public List getPage( int start, int limit) {
  18. Query q = this.em.createQuery("from " + poname );
  19. q.setFirstResult(start);
  20. q.setMaxResults(limit);
  21. return q.getResultList();
  22. }
  23. @Override
  24. public int getTotal() {
  25. return this.em.createQuery("from " + poname).getResultList().size();
  26. }
  27. }
页面的代码:
xml 代码
  1. xml version="1.0" encoding="UTF-8" ?>
  2. <%@ page language="java" contentType="text/html; charset=UTF-8"
  3. pageEncoding="UTF-8"%>
  4. >
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  8. <title>Grid3title>
  9. <link rel="stylesheet" type="text/css" href="extjs/resources/css/ext-all.css" />
  10. <script type="text/javascript" src="extjs/adapter/ext/ext-base.js">script>
  11. <script type="text/javascript" src="extjs/ext-all.js">script>
  12. <script type="text/javascript" src="extjs/ext-lang-zh_CN.js">script>
  13. head>
  14. <body>
  15. <script type="text/javascript" src="grid3.js">script>
  16. <div id="grid3" >
  17. div>
  18. body>
  19. html>
grid3.js代码
js 代码
  1. /**
  2. * @author fox
  3. */
  4. Ext.onReady(function(){
  5. Ext.BLANK_IMAGE_URL = 'extjs/resources/images/default/s.gif';
  6. Ext.QuickTips.init();
  7. var sm = new Ext.grid.CheckboxSelectionModel(); //CheckBox选择列
  8. var cm = new Ext.grid.ColumnModel([
  9. new Ext.grid.RowNumberer(), //行号列
  10. sm,
  11. {header:'编号',dataIndex:'id'},
  12. {header:'性别',dataIndex:'sex',renderer:function(value){
  13. if(value=='male'){
  14. return "";
  15. }else{
  16. return "";
  17. }
  18. }}, //增加性别,自定义renderer,即显示的样式,可以加html代码,来显示图片等。
  19. {header:'名称',dataIndex:'name'},
  20. {header:'描述',dataIndex:'descn'}
  21. ]);
  22. var ds = new Ext.data.Store({
  23. proxy: new Ext.data.HttpProxy({url:'list.action'}),//调用的动作
  24. reader: new Ext.data.JsonReader({
  25. totalProperty: 'total',
  26. root: 'persons',
  27. successProperty :'success'
  28. }, [
  29. {name: 'id',mapping:'id',type:'int'},
  30. {name: 'sex',mapping:'sex',type:'string'},
  31. {name: 'name',mapping:'name',type:'string'},
  32. {name: 'descn',mapping:'descn',type:'string'} //列的映射
  33. ])
  34. });
  35. var grid = new Ext.grid.GridPanel({
  36. el: 'grid3',
  37. ds: ds,
  38. sm: sm,
  39. cm: cm,
  40. width:700,
  41. height:280,
  42. bbar: new Ext.PagingToolbar({
  43. pageSize: 10,
  44. store: ds,
  45. displayInfo: true,
  46. displayMsg: '显示第 {0} 条到 {1} 条记录,一共 {2} 条',
  47. emptyMsg: "没有记录"
  48. }) //页脚显示分页
  49. });
  50. //el:指定html元素用于显示grid
  51. grid.render();//渲染表格
  52. ds.load({params:{start:0, limit:10}}); //加载数据
  53. });
注意,这里的gridpanel一定要设置高度,否则数据是显示不出来的。
最后启动tomcat,在浏览器里输入http://localhost:8080/extjs/grid3.jsp,就可以看到效果
posted on 2008-10-04 14:35 Loy Fu 阅读(4521) 评论(2)  编辑  收藏 所属分类: ext

只有注册用户登录后才能发表评论。


网站导航: