Extインスタンスユーザ管理


                Ext  :      、 、 、 。     ,           。         。

ユーザー列ページjsp:
  
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ include file="/include/taglibs.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><tiles:getAsString name="project_name" /></title>
</head>
<body>
<script type="text/javascript">
Ext.onReady(function(){
    //       
    Ext.QuickTips.init();
    // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'side';
    
    var sm=new Ext.grid.CheckboxSelectionModel();

	var cm=new Ext.grid.ColumnModel([
	    new Ext.grid.RowNumberer(),
		sm,
		{header:'   ',width:120,align:'center',dataIndex:'userName'},
		{header:'  ',width:120,align:'center',dataIndex:'realName'},
		{header:'  ',width:100,align:'center',dataIndex:'roleName'},
		{header:'    ',width:150,align:'center',renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s'),dataIndex:'createTime'}
	]);

	cm.defaultSortable=true;

	var ds=new Ext.data.Store({
	    //proxy:new Ext.data.MemoryProxy(data),
	    proxy:new Ext.data.ScriptTagProxy({url:'UserListAction.do'}),
		reader:new Ext.data.JsonReader(
		    {totalProperty:'totalProperty',
			 root:'user'
			},
		[{name:'id',mapping:'userId'},
		 {name:'userName'},
		 {name:'realName'},
		 {name:'roleName'},
		 {name:'createTime',type: 'date', dateFormat: 'Y-m-d H:i:s'}
		])
	});

	var searchbar=new Ext.Toolbar({
	    id:'searchbar',
		items:['   ',
		    new Ext.form.TextField({
			    id:'userName',
				width:100
			}),' ',
			new Ext.Toolbar.Button({
			    id:'searchbutton',
				text:'  ',
				handler:doSearch
			})
		]
	});

	function doSearch(){
	    ds.load(
	        {params:{
	            start:0,
	            limit:10,
	            userName:Ext.get('userName').dom.value
	    }});
	}

	var paging=new Ext.PagingToolbar({
	    pageSize:10,
		store:ds,
		displayInfo:true,
		displayMsg:'   {0}  {1}   ,  {2} ',
		items:
		["-"," ",
		    {text:'  ',
		     tooltip:'          ',
		     handler:function(){
			     window.location="UserListNaviAction.do?action=UserAdd";
			 }
			},
		"-"," ",
		    {text:'  ',
		    tooltip:'          ',
		     handler:function(){
		         var _record=grid.getSelectionModel().getSelected();
		         if(sm.getCount()==1){
		             window.location='UserListNaviAction.do?action='+'UserEdit'+'&userId='
				     +_record.get('id');
				 }else
				     Ext.Msg.alert('  ', '       !');
			 }
			},
		 "-"," ",{text:'  ',
		      tooltip:'           ',
		      handler:function(){
		          if(sm.hasSelection()){
		              Ext.MessageBox.confirm('  ','            ?',
		                  function(button){
		                      if(button=='yes'){
		                          var list=sm.getSelections();
		                          var jsonData="";
		                          for(var i=0;i<list.length;i++){
		                              var id=list[i].data["id"];
		                              if(i==0){
		                                  jsonData=jsonData+id;
		                              }else{
		                                  jsonData=jsonData+","+id;
		                              }
		                          }
		                          window.location='UserListNaviAction.do?action='+'UserDelete'+'&delIds='
				                  +jsonData;
		                      }
		                  }
		              );
		          }else{
		              Ext.Msg.alert('  ', "         !");
		          }
		      }
		    }
		],
		emptyMsg:"      !"
	});

	var grid=new Ext.grid.EditorGridPanel({
	    renderTo:'content',
		cm:cm,
		ds:ds,
		sm:sm,
		tbar:searchbar,
		bbar:paging,
		height:400,
		bodyStyle:'width:100%',
		stripeRows: true,
		title:'    '
	});
	
    ds.load({params:{start:0, limit:10}});

});
</script>
<div id="content"  style="width:1100px;padding:10px;"></div>
</body>

struts ActionクラスUserListAction.java:
  
package actions.common.user;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONObject;
import org.json.JSONArray;

import domain.common.TbUser;
import service.common.UserService;
/**
 *     Action.
 * @author jinyy
 */
public class UserListAction extends Action {

    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger.getLogger(UserListAction.class);

    final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.UK);

    /**
     * UserService.
     */
    private UserService userService;

    /**
     * @param mapping struts mapping
     * @param request request
     * @param form action form
     * @param response response
     * @return action forward
     * @throws Exception any exception
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String userName = request.getParameter("userName");
        String start = request.getParameter("start");
        String limit = request.getParameter("limit");
        int index=0;
        int pageSize=10;
        if (start != null) {
            index = Integer.parseInt(start);
            pageSize = Integer.parseInt(limit);
        }
        logger.info("list user - User Name: " + userName);
        String delIds = request.getParameter("delIds");
        if (delIds != null && !"".equals(delIds)) {
            String[] ids = delIds.split(",");
            for (String id : ids) {
                userService.delete(Integer.parseInt(id));
            }
        }

        //   service  
        List<TbUser> userList = userService.queryLists(userName);
        Integer count = userList.size();
        boolean scriptTag = false;
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("GBK");
        String cb = request.getParameter("callback");
        if (cb != null) {
            scriptTag = true;
            response.setContentType("text/javascript");
        } else {
            response.setContentType("application/x-json");
        }
        PrintWriter out = response.getWriter();
        if (scriptTag) {
            out.write(cb + "(");
        }
        JSONObject object = new JSONObject();
        JSONArray js = new JSONArray();
        try {
            Object[] users = userList.toArray();
            for (int i = index; (i < pageSize + index) && (count - 1 >= i); i++) {
                JSONObject objTemp = new JSONObject();
                objTemp.put("userName", ((TbUser) users[i]).getUserName());
                objTemp.put("userId", ((TbUser) users[i]).getUserId());
                objTemp.put("realName", ((TbUser) users[i]).getRealName());
                objTemp.put("roleName", ((TbUser) users[i]).getRoleName());
                if (((TbUser) users[i]).getCreateTime() != null) {
                    objTemp.put("createTime", sdf.format(((TbUser) users[i]).getCreateTime()));
                }
                js.put(objTemp);
            }
            object.put("user", js);
            object.put("totalProperty", count);
            out.print(object.toString());
            if (scriptTag) {
                out.write(");");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param pUserService the userService to set
     */
    public void setUserService(UserService pUserService) {
        userService = pUserService;
    }

}