Struts2绑定Jquery EasyUI

来源:互联网 发布:java工程师简历 编辑:程序博客网 时间:2024/06/07 22:52
介绍:

       jQuery EasyUI是一组基于jQuery的UI插件集合,而jQuery EasyUI的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面。对于一些不喜欢做页面的程序猿来说这是个很好的绑定数据的插件。jQuery EasyUI为我们提供了大多数UI控件的使用,如:accordion,combobox,menu,dialog,tabs,tree,validatebox,window等等 。 jQuery EasyUI绑定数据类型是JSON类型。

       1、   导入相应的jar包(非常重要,不要导入错了,我开始就是导入错了害得我整了一天)。

struts2 jar包

如果要是使用struts2-core-2.16.jar的话需要依赖的包有 json-lib2.1.jar、json-plugin-0.33.jar、ezmorph-1.0.3.jar、commons-beanutils-1.7.0.jar或1.8        这几个包的版本号必须是这样的,否则出错(  org.apache.catalina.core.StandardWrapperValve invoke

严重: Servlet.service() for servlet [default] in context with path [/TransactionSystem] threw exception [Filter execution threw an exception] with root         cause)

另外导入需要.commons-httpclient.jar  以及SSH相关jar包。

2、struts.xml文件配置:使用jsonplugin将属性转换成json对象,extends="json-default"result type要为json 
root 是只返回该内容,resultObj与action中的属性名字一样,即要返回给页面的值

[html] view plaincopy
  1. <constant name="struts.i18n.encoding" value="GBK" />  
  2.    <package name="admin" namespace="/admin" extends="json-default" >  
  3.          <action name="index">  
  4.             <result>index.jsp</result>  
  5.          </action>  
  6.       <action name="*-*" class="TransactionSystem.action.{1}Action" method="{2}">  
  7.             <result type="json" >  
  8.             <param name="root">resultObj</param>  
  9.             </result>  
  10.       </action>  
  11.    </package>   
3、*.jsp代码

[html] view plaincopy
  1. <html>  
  2.   <head>  
  3.     <base href="<%=basePath%>">  
  4.       
  5.     <title>交易码列表</title>  
  6.       
  7.     <meta http-equiv="pragma" content="no-cache">  
  8.     <meta http-equiv="cache-control" content="no-cache">  
  9.     <meta http-equiv="expires" content="0">      
  10.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  11.     <meta http-equiv="description" content="This is my page">  
  12.     <!-- 
  13.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  14.     -->  
  15. <script type="text/javascript" src="easyUI/jquery-1.6.min.js"></script>  
  16. <script type="text/javascript" src="easyUI/jquery.easyui.min.js"></script>  
  17. <link rel="stylesheet" href="easyUI/themes/default/easyui.css" type="text/css"></link>  
  18.   <link rel="stylesheet" href="easyUI/themes/icon.css" type="text/css"></link>  
  19.     <script>  
  20.         $('#tt').datagrid({  
  21.                 url: 'admin/TransCode-list',  
  22.                 title: 'DataGrid - ContextMenu',  
  23.                 width: 700,  
  24.                 height: 'auto',  
  25.                 fitColumns: true,  
  26.                 columns:[[  
  27.                     {field:'id',title:'ID',width:80},  
  28.                     {field:'tranNo',title:'交易码',width:120},  
  29.                     {field:'tranName',title:'交易名称',width:80,align:'right'},  
  30.                     {field:'cr_Num',title:'CR号',width:80,align:'right'}  
  31.                 ]],  
  32.                 onHeaderContextMenu: function(e, field){  
  33.                     e.preventDefault();  
  34.                     if (!$('#tmenu').length){  
  35.                         createColumnMenu();  
  36.                     }  
  37.                     $('#tmenu').menu('show', {  
  38.                         left:e.pageX,  
  39.                         top:e.pageY  
  40.                     });  
  41.                 }  
  42.             });  
  43.         });               
  44.     </script>  
  45.   </head>  
  46.   <body >   
  47.     <table id="tt"></table>  
  48.   </body>  
  49. </html>  
4、java代码
[java] view plaincopy
  1. public class TransCodeAction extends ActionSupport {  
  2.         private List<TransCode> transCodes;  
  3.         private TransCodeManager transCodeManager;  
  4.         private int id;  
  5.         private JSONObject resultObj;// 要返回到页面的JSON数据,一定要有getter,setter方法。  
  6.   
  7.         public JSONObject getResultObj() {  
  8.             return resultObj;  
  9.         }  
  10.   
  11.         public void setResultObj(JSONObject resultObj) {  
  12.             this.resultObj = resultObj;  
  13.         }  
  14.   
  15.         public String list() {  
  16.             HttpServletResponse resp = ServletActionContext.getResponse();  
  17.             resp.setContentType("application/json");  
  18.             transCodes = transCodeManager.getAll(); // 通过*DAO中Hibernate读取数据  
  19.             ArrayList al = new ArrayList();  
  20.             for (TransCode tc : transCodes) {  
  21.                 Map<String, Object> m = new HashMap<String, Object>();  
  22.                 m.put("id", tc.getId());  
  23.                 m.put("tranName", tc.getTranName());  
  24.                 m.put("tranNo", tc.getTranNo());  
  25.                 m.put("cr_Num", tc.getCr_Num());  
  26.                 al.add(m);  
  27.             }  
  28.             Map<String, Object> json = new HashMap<String, Object>();  
  29.             json.put("total", transCodes.size());// total键 存放总记录数,必须的json.put("rows", transCodes.size());transCodes.size()返回的数据条数  
  30.             json.put("rows", al);// rows键 存放每页记录 list,必须是“rows”关键词  
  31.             resultObj = JSONObject.fromObject(json);// 格式化result一定要是JSONObject  
  32.               
  33.             return SUCCESS;  
  34.         }  
  35.   
  36.         public List<TransCode> getTransCodes() {  
  37.             return transCodes;  
  38.         }  
  39.         public void setTransCodes(List<TransCode> transCodes) {  
  40.             this.transCodes = transCodes;  
  41.         }  
  42.         public TransCodeManager getTransCodeManager() {  
  43.             return transCodeManager;  
  44.         }  
  45.         @Resource  
  46.         public void setTransCodeManager(TransCodeManager transCodeManager) {  
  47.             this.transCodeManager = transCodeManager;  
  48.         }  
  49.     }  


// 这里边的json数据格式为:{total=4, rows=[{id=3, tranNo=401563,tranName=现金圈存, cr_Num=1117}, {id=4, tranNo=401563, tranName=现金圈存, cr_Num=1117}]}

//resultObj数据格式为:{"total":2,"rows":[{"id":2,"tranNo":401563,"tranName":"现金圈存","cr_Num":1117},{"id":3,"tranNo":401563,"tranName":"现金圈存","cr_Num":1117}]}
// 返回到页面绑定到Jquery EasyUI中datagrid的数据一定要是上面的resultObj数据格式


5、效果图

0 0
原创粉丝点击