Struts 2 jquery网格组件,带有jquery网格列类型列表,用于获取对象列表

时间:2012-09-13 14:44:15

标签: struts2 struts2-jquery

我遇到的问题是我想要一个多选数据网格列,它将有一个List对象(productsEntitled)。

当用户在记录上点击编辑时,通过提供buildSelect自定义功能来填充我的编辑对话框,我可以正确显示产品。

当我的网格上有多选<sjg:gridColumn name="productsEntitledListModel的列时,保存功能不起作用且不保存。我没有在浏览器控制台和java控制台上看到任何错误。

任何帮助都将受到赞赏,因为我无法找出问题所在,我对整个展示案例罐进行了反编译,并没有任何有助于解决此问题的内容。

我的模型看起来像这样:

@Entity
@Table ( name = "USERS")
public class User {

    private Long id;
    private String name;
    private String username;
    private String password;
    private String sourceIp;
    private Device device;
    private List<Product> productsEntitled;

这是我在jsp页面上的网格:

<s:url id="remoteurl" action="loadUsersJson"/>
    <s:url id="editurl" action="editGridUserEntry"/>
    <s:url id="selectproductsurl" action="loadProductsJson"/>


    <sjg:grid gridModel="users"  
              id="gridUsers"
              dataType="json" 
              width="1150"
              href="%{remoteurl}"
              draggable="true"
              pager="true" 
              resizable="true"
              navigatorAddOptions="{height:525, width:425, readAfterSubmit:true, draggable:true, resizable:true}"
              navigatorEditOptions="{height:525, width:425, reloadAfterSubmit:true, draggable:true, resizable:true}"
              navigatorDeleteOptions="{height:200, width:200, reloadAfterSubmit:true, draggable:true, resizable:true}"
              editurl="%{editurl}"
              navigator="true"
              navigatorEdit="true"
              navigatorAdd="true"
              navigatorView="true"
              navigatorDelete="true"
              rowList="10,15,20"
              rowNum="15"
              multiselect="false"
              onSelectRowTopics="rowselect">

        <sjg:gridColumn name="id" editable="true" index="id" hidden="true" key="true" title="ID"/>
        <sjg:gridColumn name="name" index="name" editable="true" edittype="text" title="NAME"/>
        <sjg:gridColumn name="sourceIp" index="sourceIp" editable="true" edittype="text" title="SOURCE IP"/>
        <sjg:gridColumn name="username" index="username" editable="true" edittype="text" title="USERNAME"/>
        <sjg:gridColumn name="password" index="password" editable="true" edittype="password" title="PASSWORD"/>
        <sjg:gridColumn name="role" index="role" editable="true" edittype="select" editoptions="{value:'Admin:Admin;User:User;'}" title="ROLE"/>

        <sjg:gridColumn name="deviceId" jsonmap="device.id" key="true" hidden="true" editable="text" title="DEVICE ID"/> 
        <sjg:gridColumn name="deviceIp" jsonmap="device.ip" editable="true" edittype="text" title="DEVICE IP"/>

        <sjg:gridColumn name="productsEntitledListModel"  
                        width="300"
                        editable="true" 
                        edittype="select"
                        editoptions="{dataUrl: '%{selectproductsurl}', multiple:true, buildSelect:customBuildSelect}"
                        title="PRODUCTS"/>

    </sjg:grid>

这些是我的行动类:

显示网格:

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;


public class AdminAction extends ActionSupport implements ServletRequestAware {

    private static final long serialVersionUID = -1090720652366248768L;
    private static final Log logger = LogFactory.getLog(AdminAction.class);

    private HttpServletRequest request;
    private AuthenticationTicket ticket;
    private AdminService adminService;
    private List<User> users;
    private List<Product> products;
    //private List<String>productsAllList;
    private String userId;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }

    public AuthenticationTicket getTicket() {
        return ticket;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public void setTicket(AuthenticationTicket ticket) {
        this.ticket = ticket;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {

        this.request = request;

    }

    public String redirectUsersTab() {
        return "users";
    }

    public String redirectProductsTab() {
        return "products";
    }

    private void initAdminService () {

        logger.debug("initAdminService()...");

        if (adminService == null) {
            adminService = (AdminService)ServiceFinder.getContext(request).getBean("adminService");
        }

    }

    public String loadUsersJson() {

        initAdminService();

        this.users = adminService.getUsersAll();

        return "success";
    }

    public String loadProductsJson() {

        initAdminService();

        this.products = adminService.getProductsAll();

        return "success";
    }

    //TODO: clean up
    public String getAllProductsList() {

        logger.debug("testParam, userId: " + this.userId);

        initAdminService();


        List<Product> temp = adminService.getProductsAll();

        if (userId != null) {

            User userTemp = new User();
            userTemp.setId(new Long(userId));

            List<Product> prodEntitled = adminService.getProductsByUser(userTemp);

            logger.debug("Products entitled: " + prodEntitled);
        }

        //TODO: merge prod and prod entitled to Model to populate the grid

        products = temp;
        /*
        if (temp != null && temp.size() > 0) {

            this.productsAllList = new ArrayList<String>();

            for (Product p : temp) {
                this.productsAllList.add(p.getName());
            }
        }
        */

        //logger.debug("this.productsAllList: " + this.productsAllList);


        return "success";
    }

}

编辑网格的类:

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;


public class EditUsersGridAction extends ActionSupport implements ServletRequestAware {

    private static final Log logger = LogFactory.getLog(EditUsersGridAction.class);
    private static final long serialVersionUID = -5485382508029951644L;

    private HttpServletRequest request;

    private AdminService adminService;
    private String oper = "";
    private Long id;
    private String name;
    private String sourceIp;
    private String password;
    private String username;
    private Long deviceId;
    private String deviceIp;
    private String devicePortDescription;
    private String devicePortLayer;
    private String deviceType;
    private List<Product>productsEntitled;
    private List<GridColumnListModel> productsEntitledListModel;
    private List<Product>productsAvailable;


    public List<GridColumnListModel> getProductsEntitledListModel() {

        try {

            if (productsEntitled != null && productsEntitled.size() > 0) {

                productsEntitledListModel = new ArrayList<EditUsersGridAction.GridColumnListModel>();

                for (Product p : productsEntitled) {

                    GridColumnListModel tmp = new GridColumnListModel();

                    tmp.setName(p.getName());
                    tmp.setValue(p);

                    productsEntitledListModel.add(tmp);
                }

                return this.productsEntitledListModel;
            }else {
                return null;
            }

        } catch (Exception ex) {
            logger.error("Exception in getProductsEntitledString", ex);
            return null;
        }
    }

    public void setProductsEntitledListModel(List<GridColumnListModel> productsEntitledListModel) {
        this.productsEntitledListModel = productsEntitledListModel;
    }

    public Long getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(Long deviceId) {
        this.deviceId = deviceId;
    }

    public String getDeviceIp() {
        return deviceIp;
    }

    public void setDeviceIp(String deviceIp) {
        this.deviceIp = deviceIp;
    }

    public String getDevicePortDescription() {
        return devicePortDescription;
    }

    public void setDevicePortDescription(String devicePortDescription) {
        this.devicePortDescription = devicePortDescription;
    }

    public String getDevicePortLayer() {
        return devicePortLayer;
    }

    public void setDevicePortLayer(String devicePortLayer) {
        this.devicePortLayer = devicePortLayer;
    }

    public String getDeviceType() {
        return deviceType;
    }

    public void setDeviceType(String deviceType) {
        this.deviceType = deviceType;
    }

    public String getOper() {
        return oper;
    }

    public void setOper(String oper) {
        this.oper = oper;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSourceIp() {
        return sourceIp;
    }

    public void setSourceIp(String sourceIp) {
        this.sourceIp = sourceIp;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public List<Product> getProductsEntitled() {
        return productsEntitled;
    }

    public void setProductsEntitled(List<Product> productsEntitled) {
        this.productsEntitled = productsEntitled;
    }

    public List<Product> getProductsAvailable() {
        return productsAvailable;
    }

    public void setProductsAvailable(List<Product> productsAvailable) {
        this.productsAvailable = productsAvailable;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;

    }

    private void initAdminService () {

        logger.debug("initAdminService()...");

        if (adminService == null) {
            adminService = (AdminService)ServiceFinder.getContext(request).getBean("adminService");
        }

    }

    public String execute() throws Exception {

        logger.debug("#### IN EditGridUsersAction ###");

        initAdminService();

        productsAvailable = adminService.getProductsAll();

        User user = new User();

        user.setName(name);
        user.setPassword(password);
        user.setUsername(username);
        user.setSourceIp(sourceIp);

        Device device = new Device();

        device.setId(deviceId);
        device.setIp(deviceIp);
        device.setPortDescription(devicePortDescription);
        device.setType(deviceType);

        user.setDevice(device);

        if (id != null) {
            user.setId(id);
        }

        if (oper.equalsIgnoreCase("add")) {

            logger.debug("products selected");
            //user.setProductsEntitled(adminService.getProductsAll());
            adminService.addUser(user);

        } else if ( oper.equalsIgnoreCase("edit")) {

            //TODO: convert model to products and add to user
            logger.debug("now in edit");

            List<Product> tempProd = new ArrayList<Product>();

            for (GridColumnListModel gridColModel : productsEntitledListModel) { 
                tempProd.add(gridColModel.getValue());
            }

            user.setProductsEntitled(tempProd);
            adminService.updateUser(user);

            //return "input";

        }else if (oper.equalsIgnoreCase("del")) {
            logger.debug("in delete");
            adminService.deleteUser(user);
        }

        return "success";
    }

    public class GridColumnListModel {

        private String name;
        private Product value;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Product getValue() {
            return value;
        }

        public void setValue(Product value) {
            this.value = value;
        }


    }

}

0 个答案:

没有答案