DWZ富客户端框架(dwzjs)结合struts2的增改删查

DWZ是实用的国产JQuery UI框架,个人感觉比较好用。

他和服务器端主要通过Ajax方式交互,数据格式为json。

服务器响应数据代码示例:

{

     "statusCode":"200",

    "message":"操作成功",

    "navTabId":"actvtyList",

    "rel":"",

    "callbackType":"closeCurrent",

    "forwardUrl":""

}

statusCode是返回码, 200表示成功, 300表示服务器错误,301表示请求超时。

message为返回信息,dwz中会显示在一个在浏览器顶部中间位置的对话框中。

rel 没研究,想研究的可以参考dwz的官方文档。

navTabId是tab id,表示刷新指定的tab。

callbackType是回调方式,closeCurrent表示关闭当前的tab或者dialog

forwardUrl 为转向url,表示当前tab或者dialog跳转到指定的url。


首先为DWZ定义java数据模型

package com.mytest.domain.dwz;

public class DwzAjaxResult {
	private String statusCode;
	private String message;
	private String navTabId;
	private String callbackType;
	private String forwardUrl;
	
	public DwzAjaxResult(String statusCode, String message, String navTabId,
			String callbackType, String forwardUrl) {
		this.statusCode = statusCode;
		this.message = message;
		this.navTabId = navTabId;
		this.callbackType = callbackType;
		this.forwardUrl = forwardUrl;
	}
	//getters & setters
}
public class DwzPager implements Serializable {
	private static final long serialVersionUID = 1L;
	private String status;
	private String keywords;
	private Integer pageNum = 1;
	private Integer numPerPage = 20;
	private Integer pageTotal = 0;
	private Integer total = 0;
	
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public String getKeywords() {
		return keywords;
	}
	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}
	public Integer getPageNum() {
		if(pageNum == null || pageNum < 1) pageNum = 1;
		if(pageNum > getPageTotal()) pageNum = getPageTotal();
		return pageNum;
	}
	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}
	public Integer getNumPerPage() {
		return numPerPage;
	}
	public void setNumPerPage(Integer numPerPage) {
		if(numPerPage!=null)
			this.numPerPage = numPerPage;
	}
	public Integer getPageTotal() {
		if(total == null) return 1;
		pageTotal = (total + numPerPage - 1) / numPerPage;
		return pageTotal;
	}
	public void setPageTotal(Integer pageTotal) {
		this.pageTotal = pageTotal;
	}
	public Integer getTotal() {
		return total;
	}
	public void setTotal(Integer total) {
		this.total = total;
	}
}

然后是服务器struts代码,其中printDwzJsonResult()方法是将对象转换成dwz数据格式并写入servletoutputstream。

public class ActivityAction extends BaseActionSupport {
	private ActivityService activityService;
	private List activityList;
	private Activity activity;
	private String pk;//主键值,以#连接(用于组合主键)
	
	private DwzPager pager = new DwzPager();
        //需要定义下面4个变量是dwz的pagerForm提交的分页参数
        private String status = "";
	private String keywords = "";
	private Integer pageNum = 1;
	private Integer numPerPage = 20;
	public ActivityAction(){
		activityService = new ActivityServiceImpl();
	}

	public void activityAdd() throws Exception{
		DwzAjaxResult resultBean;
		if(activityService.addActivity(activity)){
			setSessionPageNum(pageNum);//将当前页码保存到session,避免操作完后刷新列表时自动跳转到第1页的尴尬
			resultBean = new DwzAjaxResult("200","保存成功","","forward",request.getContextPath()+"/admin/actvty/add.jsp");
		}else{
			resultBean = new DwzAjaxResult("300","保存失败","","","");
		}
		printDwzJsonResult(resultBean);
	}

	public void activityUpdate() throws Exception{
		DwzAjaxResult resultBean;
		if(activityService.updateActivity(activity)){
			setSessionPageNum(pageNum);//将当前页码保存到session,避免操作完后刷新列表时自动跳转到第1页的尴尬
			resultBean = new DwzAjaxResult("200","修改成功","actvtyList","closeCurrent","");
		}else{
			resultBean = new DwzAjaxResult("300","修改失败","","","");
		}
		printDwzJsonResult(resultBean);
	}

	public String activityEdit() throws Exception{
		String[] prikeys = pk.split("#");//拆分主键值,这里简单地取第一个主键,需要完善逻辑
		activity = activityService.findActivity(Integer.parseInt(prikeys[0]));
		return SUCCESS;
	}

	public void activityDelete() throws Exception{
		DwzAjaxResult resultBean;
		String[] prikeys = pk.split("#");
		activity = activityService.findActivity(Integer.parseInt(prikeys[0]));
		if(activity!=null && activityService.deleteActivity(activity)){
			setSessionPageNum(pageNum);
			resultBean = new DwzAjaxResult("200","删除成功","actvtyList","","");
		}else{
			resultBean = new DwzAjaxResult("300","删除失败","","","");
		}
		printDwzJsonResult(resultBean);
	}

	public void activitySearch() throws Exception{
		pager.setPageNum(1);
		setSessionPageNum(1);
		DwzAjaxResult resultBean = new DwzAjaxResult("200","结果已过滤","actvtyList","","");//不操作业务逻辑,直接刷新actvtyList列表
		printDwzJsonResult(resultBean);
	}

	public String activityList() throws Exception{
		int total = 0;
		if(activity != null) total = activityService.totalFuzzyActivitys(activity);
		else total = activityService.totalActivitys();
		pageNum = getSessionPageNum()!=null?getSessionPageNum():pageNum;
		pager.setTotal(total);
		pager.setStatus(status);
		pager.setKeywords(keywords);
		pager.setPageNum(pageNum);
		pager.setNumPerPage(numPerPage==null?pager.getNumPerPage():numPerPage);
		if(activity != null) activityList = activityService.getFuzzyActivitys(activity, pager.getPageNum(), pager.getNumPerPage());
		else activityList = activityService.getActivitys(pager.getPageNum(), pager.getNumPerPage());
		removeSessionPageNum(); //重置session中的pageNum
		return SUCCESS;
	}

	public List getActivityList(){
		return activityList;
	}

	public void setPk(String pk){
		this.pk = pk;
	}

	public DwzPager getPager() {
		return pager;
	}

	public void setActivity(Activity activity){
		this.activity = activity;
	}

	public Activity getActivity(){
		return activity;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public Integer getPageNum() {
		return this.pageNum;
	}

	public void setNumPerPage(Integer numPerPage) {
		this.numPerPage = numPerPage;
	}

}
struts2配置,大部分action都是ajax请求,所以都没配置处理结果。




	
		
		
			/admin/actvty/edit.jsp
		
		
		
		
			/admin/actvty/list.jsp
		
		
	
list.jsp (DWZ中扩展了a标签,分别设置target='navTab"和"dialog"可以使得链接网页分别在tab、dialog中打开,另外target="ajaxTodo"可以请求服务器url。详细解释可以参看dwz文档,这里不赘述)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<% String base = request.getContextPath();%>

">
  name image type intro st_time ed_time address stars join_way album_id entrants
,${pager.total}
add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

Activity

name:
image:
type:
intro:
st_time:
ed_time:
address:
stars:
join_way:
album_id:
entrants:
edit.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

Activity

name:
"/>
image:
"/>
type:
"/>
intro:
"/>
st_time:
"/>
ed_time:
"/>
address:
"/>
stars:
"/>
join_way:
"/>
album_id:
entrants:
"/>
  • "/> "/>
search.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

其中用到一些国际化的配置,index.jsp中加载dwz.frag.xml全局配置

$(function(){
	var config = "";
	DWZ.init(config, {
		loginUrl:"../login.jsp",	// 跳到登录页面
		statusCode:{ok:200, error:300, timeout:301}, //【可选】
		pageInfo:{pageNum:"pageNum", numPerPage:"numPerPage", orderField:"orderField", orderDirection:"orderDirection"}, //【可选】
		debug:false,	// 调试模式 【true|false】
		callback:function(){
			initEnv();
			$("#themeList").theme({themeBase:"themes"}); // themeBase 相对于index页面的主题base路径
		}
	});
});


几张截图

DWZ富客户端框架(dwzjs)结合struts2的增改删查_第1张图片

DWZ富客户端框架(dwzjs)结合struts2的增改删查_第2张图片

DWZ富客户端框架(dwzjs)结合struts2的增改删查_第3张图片

DWZ富客户端框架(dwzjs)结合struts2的增改删查_第4张图片


你可能感兴趣的:(Java编程)