Jmaki 集成到 struts 一起使用
Author: bsspirit
Source: http://gocom.primeton.com/blog4324_14478.htm
其實(shí)只要明白,list不是通過(guò)UserAction傳到UserList.jsp,而是UserList.jsp再訪(fǎng)問(wèn)另外的Action去取的就行了。
jmaki 是SUN一直大力推進(jìn)的Ajax框架,利用widget的實(shí)現原理,封裝了dojo,google,yahoo之類(lèi)ajax庫。使用jmaki的同時(shí),又可以用到許多其他開(kāi)源庫的,非常好的ajax組件。我在這里演示一下,集成dojo.table的demo.
首先,先要明確怎么樣,才能讓jmaki和struts集成。這兩個(gè)同時(shí)是web前臺的框架。我們在開(kāi)發(fā)struts的時(shí)候,User.jsp --> UserAction --> UserList.jsp,比如是上面的一個(gè)流程,一般是通過(guò)在UserAction里面request.setAttribte("list",list),設置一個(gè)request屬性,然后UserList.jsp通過(guò)request.getAttribute("list")讀取出來(lái),或者直接用struts標簽或jstl標簽讀取list對象,然后顯示這個(gè)list的內容。
現在我們有了ajax技術(shù),他其實(shí)可以不這樣去傳遞對象了。還是這樣的流程,User.jsp --> UserAction --> UserList.jsp。這時(shí)我們不用在UserAction設置屬性,只是用struts控制流程。等打開(kāi)UserList.jsp的頁(yè)面以后,再設定一個(gè)url,去讀取這個(gè)list的內容。比如:<a:ajax name="dojo.table" service="http://localhost:8080/test/ListServlet" />。這個(gè)過(guò)程其他和讀二進(jìn)制流的概念差不多。取回來(lái)的可能是一個(gè)JSon串,也可能是XML文件。JSON串的方式就叫做Rest,XML方式可以直接調度Web Services。
其實(shí)只要明白,list不是通過(guò)UserAction傳到UserList.jsp,而是UserList.jsp再訪(fǎng)問(wèn)另外的Action去取的就行了。
下面貼一些代碼,是項目里的,還雜著(zhù)Xdoclet,spring等等你的其他的框架的東西,有點(diǎn)懶得挑出來(lái)了,湊合著(zhù)看。
流程的Action
- package com.dvs.biz.web.action.equipcategory;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
-
- import com.dvs.common.web.action.BaseAction;
- /**
- *
- * @struts.action path="/jsp/biz/equipcategory/category"
- * type="org.springframework.web.struts.DelegatingActionProxy"
- * scope="request"
- * parameter="action"
- * @struts.action-forward name="list" path="/jsp/biz/equipcategory/CategoryList.jsp" redirect="true"
- *
- * @spring.bean name="/jsp/biz/equipcategory/category"
- *
- * @author Conan
- *
- */
- public class CategoryAction extends BaseAction{
- private final static Log log = LogFactory.getLog(CategoryAction.class);
-
- /**
- * http://localhost:8880/DVS/jsp/biz/equipcategory/category.do?action=list
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- * @throws Exception
- */
- public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
- log.info("CategoryAction : list");
- return mapping.findForward("list");
- }
- }
轉向CategoryList.jsp
- <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
- <%@ include file="../../common/taglibs.jsp"%>
-
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html:html lang="true">
- <head>
- <html:base />
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>設備信息查詢(xún)</title>
- <link rel="stylesheet" type="text/css" href="../../../css/main.css">
- </head>
- <body>
- <html:errors />
- <h2 align="center">設備大類(lèi)查詢(xún)</h2>
- <div id="m1" align="center">
- <a:ajax name="dojo.table" service="http://localhost:8880/DVS/jsp/biz/equipcategory/jmaki.do?action=categoryList" />
- <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="table7">
- <tr>
- <td width="273" height="30">數據共2條,第1頁(yè),共1頁(yè)</td>
- <td width="171">
- <div align="right">
- <a href="#"><img src="../../../pic/qj.gif" width="13" height="13" border="0" align="absmiddle"></a>
- <a href="#"><img src="../../../pic/qj1.gif" width="13" height="13" border="0" align="absmiddle"></a>
- <a href="#"><img src="../../../pic/qj1-1.gif" width="13" height="13" border="0" align="absmiddle"></a>
- <a href="#"><img src="../../../pic/qj-1.gif" width="13" height="13" border="0" align="absmiddle"></a>
- </div>
- </td>
- </tr>
- </table>
- </div>
- </body>
- </html:html>
(上面的分頁(yè),還有一些東西,還沒(méi)有做完。只是為了演示demo用的)
<a:ajax name="dojo.table" service="http://localhost:8880/DVS/jsp/biz/equipcategory/jmaki.do?action=categoryList" />
jsp頁(yè)面,通過(guò)鏈接再去啟動(dòng)table.
TableAction,這里是提供JSON串的類(lèi)。
- package com.dvs.biz.web.action.ajax;
-
- import java.io.PrintWriter;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.json.JSONArray;
- import org.json.JSONObject;
-
- import com.dvs.biz.model.equipcategory.CategoryDTO;
- import com.dvs.biz.model.equipcategory.CategorySubcategoryDTO;
- import com.dvs.biz.model.equipgroup.EquipFatherGroupDTO;
- import com.dvs.biz.service.equipcategory.EquipCategoryMamtService;
- import com.dvs.biz.service.equipcategory.EquipSubcategoryMamtService;
- import com.dvs.biz.service.equipgroup.EquipGroupMamtService;
- import com.dvs.common.web.action.BaseAction;
-
- /**
- * @struts.action path="/jsp/biz/equipcategory/jmaki"
- * type="org.springframework.web.struts.DelegatingActionProxy"
- * scope="request" parameter="action"
- *
- *
- * @spring.bean name="/jsp/biz/equipcategory/jmaki"
- * @spring.property name="equipCategoryMamtService" ref="equipCategoryMamtService"
- * @author Conan
- *
- */
- public class DojoTableAction extends BaseAction {
- private final static Log log = LogFactory.getLog(DojoTableAction.class);
-
- private EquipCategoryMamtService equipCategoryMamtService;
-
- /**
- * http://localhost:8880/DVS/jsp/biz/equipcategory/jmaki.do?action=categoryList
- *
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- * @throws Exception
- */
- public ActionForward categoryList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
- log.info("JMakiAction : CategoryList");
- List<CategoryDTO> list = equipCategoryMamtService.queryEquipCategory();
- PrintWriter out = response.getWriter();
- String str = categoryTable(list);
- log.info("Json :" + str);
- out.print(str);
- out.flush();
- out.close();
- return null;
- }
-
- @SuppressWarnings("unchecked")
- private String categoryTable(List<CategoryDTO> list) {
- JSONArray row = new JSONArray();
- JSONArray rows = new JSONArray();
-
- Map map = new HashMap();
- map.put("code", "設備大類(lèi)代碼");
- map.put("categoryname", "設備大類(lèi)名稱(chēng)");
- map.put("description", "設備大類(lèi)描述");
- map.put("standard", "分類(lèi)標準");
- map.put("detail", "詳情");
-
- // input columns
- JSONObject columns = DojoTableCommon.setColumns(map);
-
- for (CategoryDTO dto : list) {
- row.put(dto.getCode());
- row.put(dto.getCategoryname());
- row.put(dto.getDescription());
- row.put(dto.getCode());
- row.put("<a href="#">詳情</a>");
-
- rows.put(row);
- }
-
- // input columns,rows
- JSONObject table = DojoTableCommon.setTable(columns, rows);
- return table.toString();
- }
-
- public void setEquipCategoryMamtService(EquipCategoryMamtService equipCategoryMamtService) {
- this.equipCategoryMamtService = equipCategoryMamtService;
- }
-
- }
CategoryAction轉發(fā)請求給CategoryList.jsp
CategoryList.jsp從DojoTableAction中取回JSON組成table
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請
點(diǎn)擊舉報。