分享

Jmaki 集成到 struts 一起使用

 chanvy 2008-11-25
Jmaki 集成到 struts 一起使用
Author: bsspirit
Source: http://gocom./blog4324_14478.htm

其实只要明白,list不是通过UserAction传到UserList.jsp,而是UserList.jsp再访问另外的Action去取的就行了。

jmaki 是SUN一直大力推进的Ajax框架,利用widget的实现原理,封装了dojo,google,yahoo之类ajax库。使用jmaki的同时,又可以用到许多其他开源库的,非常好的ajax组件。我在这里演示一下,集成dojo.table的demo.

首先,先要明确怎么样,才能让jmaki和struts集成。这两个同时是web前台的框架。我们在开发struts的时候,User.jsp --> UserAction --> UserList.jsp,比如是上面的一个流程,一般是通过在UserAction里面request.setAttribte("list",list),设置一个request属性,然后UserList.jsp通过request.getAttribute("list")读取出来,或者直接用struts标签或jstl标签读取list对象,然后显示这个list的内容。

现在我们有了ajax技术,他其实可以不这样去传递对象了。还是这样的流程,User.jsp --> UserAction --> UserList.jsp。这时我们不用在UserAction设置属性,只是用struts控制流程。等打开UserList.jsp的页面以后,再设定一个url,去读取这个list的内容。比如:<a:ajax name="dojo.table" service="http://localhost:8080/test/ListServlet" />。这个过程其他和读二进制流的概念差不多。取回来的可能是一个JSon串,也可能是XML文件。JSON串的方式就叫做Rest,XML方式可以直接调度Web Services。

其实只要明白,list不是通过UserAction传到UserList.jsp,而是UserList.jsp再访问另外的Action去取的就行了。

下面贴一些代码,是项目里的,还杂着Xdoclet,spring等等你的其他的框架的东西,有点懒得挑出来了,凑合着看。


流程的Action

  1. package com.dvs.biz.web.action.equipcategory;
  2.  
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5.  
  6. import org.apache.commons.logging.Log;
  7. import org.apache.commons.logging.LogFactory;
  8. import org.apache.struts.action.ActionForm;
  9. import org.apache.struts.action.ActionForward;
  10. import org.apache.struts.action.ActionMapping;
  11.  
  12. import com.dvs.common.web.action.BaseAction;
  13. /**
  14. *
  15. * @struts.action path="/jsp/biz/equipcategory/category"
  16. * type="org.springframework.web.struts.DelegatingActionProxy"
  17. * scope="request"
  18. * parameter="action"
  19. * @struts.action-forward name="list" path="/jsp/biz/equipcategory/CategoryList.jsp" redirect="true"
  20. *
  21. * @spring.bean name="/jsp/biz/equipcategory/category"
  22. *
  23. * @author Conan
  24. *
  25. */
  26. public class CategoryAction extends BaseAction{
  27. private final static Log log = LogFactory.getLog(CategoryAction.class);
  28.  
  29. /**
  30. * http://localhost:8880/DVS/jsp/biz/equipcategory/category.do?action=list
  31. * @param mapping
  32. * @param form
  33. * @param request
  34. * @param response
  35. * @return
  36. * @throws Exception
  37. */
  38. public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
  39. log.info("CategoryAction : list");
  40. return mapping.findForward("list");
  41. }
  42. }

转向CategoryList.jsp

  1. <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2. <%@ include file="../../common/taglibs.jsp"%>
  3.  
  4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  5. <html:html lang="true">
  6. <head>
  7.       <html:base />
  8.       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  9.       <title>设备信息查询</title>
  10.       <link rel="stylesheet" type="text/css" href="../../../css/main.css">
  11. </head>
  12. <body>
  13.       <html:errors />
  14.       <h2 align="center">设备大类查询</h2>
  15.       <div id="m1" align="center">
  16.             <a:ajax name="dojo.table" service="http://localhost:8880/DVS/jsp/biz/equipcategory/jmaki.do?action=categoryList" />
  17.             <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="table7">
  18.                   <tr>
  19.                         <td width="273" height="30">数据共2条,第1页,共1页</td>
  20.                         <td width="171">
  21.                               <div align="right">
  22.                                     <a href="#"><img src="../../../pic/qj.gif" width="13" height="13" border="0" align="absmiddle"></a> 
  23.                                     <a href="#"><img src="../../../pic/qj1.gif" width="13" height="13" border="0" align="absmiddle"></a> 
  24.                                     <a href="#"><img src="../../../pic/qj1-1.gif" width="13" height="13" border="0" align="absmiddle"></a> 
  25.                                     <a href="#"><img src="../../../pic/qj-1.gif" width="13" height="13"      border="0" align="absmiddle"></a>
  26.                               </div>
  27.                         </td>
  28.                   </tr>
  29.             </table>
  30.       </div>
  31. </body>
  32. </html:html>

(上面的分页,还有一些东西,还没有做完。只是为了演示demo用的)
<a:ajax name="dojo.table" service="http://localhost:8880/DVS/jsp/biz/equipcategory/jmaki.do?action=categoryList" />
jsp页面,通过链接再去启动table.

TableAction,这里是提供JSON串的类。

  1. package com.dvs.biz.web.action.ajax;
  2.  
  3. import java.io.PrintWriter;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10.  
  11. import org.apache.commons.logging.Log;
  12. import org.apache.commons.logging.LogFactory;
  13. import org.apache.struts.action.ActionForm;
  14. import org.apache.struts.action.ActionForward;
  15. import org.apache.struts.action.ActionMapping;
  16. import org.json.JSONArray;
  17. import org.json.JSONObject;
  18.  
  19. import com.dvs.biz.model.equipcategory.CategoryDTO;
  20. import com.dvs.biz.model.equipcategory.CategorySubcategoryDTO;
  21. import com.dvs.biz.model.equipgroup.EquipFatherGroupDTO;
  22. import com.dvs.biz.service.equipcategory.EquipCategoryMamtService;
  23. import com.dvs.biz.service.equipcategory.EquipSubcategoryMamtService;
  24. import com.dvs.biz.service.equipgroup.EquipGroupMamtService;
  25. import com.dvs.common.web.action.BaseAction;
  26.  
  27. /**
  28. * @struts.action path="/jsp/biz/equipcategory/jmaki"
  29. * type="org.springframework.web.struts.DelegatingActionProxy"
  30. * scope="request" parameter="action"
  31. *
  32. *
  33. * @spring.bean name="/jsp/biz/equipcategory/jmaki"
  34. * @spring.property name="equipCategoryMamtService" ref="equipCategoryMamtService"
  35. * @author Conan
  36. *
  37. */
  38. public class DojoTableAction extends BaseAction {
  39. private final static Log log = LogFactory.getLog(DojoTableAction.class);
  40.  
  41. private EquipCategoryMamtService equipCategoryMamtService;
  42.  
  43. /**
  44. * http://localhost:8880/DVS/jsp/biz/equipcategory/jmaki.do?action=categoryList
  45. *
  46. * @param mapping
  47. * @param form
  48. * @param request
  49. * @param response
  50. * @return
  51. * @throws Exception
  52. */
  53. public ActionForward categoryList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
  54. log.info("JMakiAction : CategoryList");
  55. List<CategoryDTO> list = equipCategoryMamtService.queryEquipCategory();
  56. PrintWriter out = response.getWriter();
  57. String str = categoryTable(list);
  58. log.info("Json :" + str);
  59. out.print(str);
  60. out.flush();
  61. out.close();
  62. return null;
  63. }
  64.  
  65. @SuppressWarnings("unchecked")
  66. private String categoryTable(List<CategoryDTO> list) {
  67. JSONArray row = new JSONArray();
  68. JSONArray rows = new JSONArray();
  69.  
  70. Map map = new HashMap();
  71. map.put("code", "设备大类代码");
  72. map.put("categoryname", "设备大类名称");
  73. map.put("description", "设备大类描述");
  74. map.put("standard", "分类标准");
  75. map.put("detail", "详情");
  76.  
  77. // input columns
  78. JSONObject columns = DojoTableCommon.setColumns(map);
  79.  
  80. for (CategoryDTO dto : list) {
  81. row.put(dto.getCode());
  82. row.put(dto.getCategoryname());
  83. row.put(dto.getDescription());
  84. row.put(dto.getCode());
  85. row.put("<a href="#">详情</a>");
  86.  
  87. rows.put(row);
  88. }
  89.  
  90. // input columns,rows
  91. JSONObject table = DojoTableCommon.setTable(columns, rows);
  92. return table.toString();
  93. }
  94.  
  95. public void setEquipCategoryMamtService(EquipCategoryMamtService equipCategoryMamtService) {
  96. this.equipCategoryMamtService = equipCategoryMamtService;
  97. }
  98.  
  99. }

CategoryAction转发请求给CategoryList.jsp

CategoryList.jsp从DojoTableAction中取回JSON组成table

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多