Jmaki 集成到 struts 一起使用

来源:百度文库 编辑:神马文学网 时间:2024/05/21 00:57:12
Jmaki 集成到 struts 一起使用Author: bsspirit
Source: http://gocom.primeton.com/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的内容。比如:。这个过程其他和读二进制流的概念差不多。取回来的可能是一个JSon串,也可能是XML文件。JSON串的方式就叫做Rest,XML方式可以直接调度Web Services。
其实只要明白,list不是通过UserAction传到UserList.jsp,而是UserList.jsp再访问另外的Action去取的就行了。
下面贴一些代码,是项目里的,还杂着Xdoclet,spring等等你的其他的框架的东西,有点懒得挑出来了,凑合着看。
流程的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"%>                       设备信息查询                     

设备大类查询

      
                                                                                                                              
数据共2条,第1页,共1页                               
                                                                                                                                                                                     
                        
      

(上面的分页,还有一些东西,还没有做完。只是为了演示demo用的)

jsp页面,通过链接再去启动table.
TableAction,这里是提供JSON串的类。
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 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 list) { JSONArray row = new JSONArray(); JSONArray rows = new JSONArray();   Map map = new HashMap(); map.put("code", "设备大类代码"); map.put("categoryname", "设备大类名称"); map.put("description", "设备大类描述"); map.put("standard", "分类标准"); 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("详情");   rows.put(row); }   // input columns,rows JSONObject table = DojoTableCommon.setTable(columns, rows); return table.toString(); }   public void setEquipCategoryMamtService(EquipCategoryMamtService equipCategoryMamtService) { this.equipCategoryMamtService = equipCategoryMamtService; }   }
CategoryAction转发请求给CategoryList.jsp
CategoryList.jsp从DojoTableAction中取回JSON组成table