在struts2中从JavaScript检索值到Action类

时间:2013-02-25 02:04:05

标签: java-ee jqgrid struts2 struts2-jquery

我正在使用jquery struts2网格插件。我正在使用自己的表单来更新,删除和插入对象。我创建了一个隐藏字段来从javascript中检索值但它不起作用......似乎是什么问题?

当我点击下面的网格代码时,会激活。

var product_id = grid.jqGrid('getCell', sel_id, 'product_Id'); 

我希望将product_id的值传递给struts2中的隐藏标记。

document.getElementById('product_Id').value = product_id;

因此它应该通过此标记将product_id值传递给product_Key字段。

<s:hidden value="" name="product_Key" id="product_Id"/>

然后当我点击网格上的编辑按钮时,它应该作为一个参数传递到这一行。

 <s:url id="editurl" action="ProductUpdate">
            <s:param name="product_Key"><s:property value="product_Key"/></s:param>
 </s:url>

这是编辑点击按钮。

 Edit : { 
            title :'Edit record on the selected record on the row',
            caption :'Edit Record', 
            icon:'ui-icon-pencil',
            onclick: function(){ alert('Edit is clicked'),document.getElementById('product_Id').value = product_id,window.location = '%{editurl}' }
            }

整个守则受到影响。

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>
<!DOCTYPE html>
<html>
    <head>
        <sj:head jqueryui="true" jquerytheme="redmond" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

        <s:url id="remoteurl" action="ProductMain"/>
        <s:url id="editurl" action="ProductUpdate">
            <s:param name="product_Key"><s:property value="product_Key"/></s:param>
        </s:url>



        <s:url id="myUrl" action="ProductInitialise" />
        <s:hidden value="" name="product_Key" id="product_Id"/>   -----> use to retrieve value of product id

        <script type="text/javascript">
            // this will retrieve the record in the row.. retrieve this for delete and edit.
            $.subscribe('rowselect', function(event, data) {
                var grid = event.originalEvent.grid; 
                var sel_id = grid.jqGrid('getGridParam', 'selrow'); 
                var product_id = grid.jqGrid('getCell', sel_id, 'product_Id'); 
                var last_date =  grid.jqGrid('getCell', sel_id, 'last_Date'); 
                alert(product_id + 'date' + last_date);
                document.getElementById('product_Id').value = product_id; ---> paste the value of the product_id.
                //  alert('Selected Row : ' + event.originalEvent.id );
            });





        </script>




        <sjg:grid
            id="gridtable"
            caption="Customer Examples"
            dataType="json"
            href="%{remoteurl}"
            pager="true"
            gridModel="gridModel"
            rowList="10,15,20"
            rowNum="10"
            rownumbers="true"

            navigatorExtraButtons="{
            seperator: { 
            title : 'seperator'  
            }, 
            Insert : { 
            title : 'Insert Record', 
            caption : 'Insert', 
            icon: 'ui-icon-plusthick', 
            onclick: function(){  alert('Grid Button clicked!') ,window.location = '%{myUrl}'} 
            },
            Edit : { 
            title :'Edit record on the selected record on the row',
            caption :'Edit Record', 
            icon:'ui-icon-pencil',
            onclick: function(){ alert('Edit is clicked'),window.location = '%{editurl}' }
            }
            }"
            navigatorAdd="false"
            navigatorEdit="false"
            navigator="true"
            resizable="true"
            onSelectRowTopics="rowselect"
            >


            <sjg:gridColumn name="product_Id" 
                            index="product_Id" 
                            title="Product_Id"  
                            sortable="false"
                            />


            <sjg:gridColumn name="serial_Number" 
                            index="serial_Number" 
                            title="Serial_Number" 
                            editable="true"
                            sortable="true"/>
            <sjg:gridColumn name="date_Assembled" 
                            index="date_Assembled" 
                            title="Date_Assembled"  
                            editable="true"
                            sortable="true"/>
            <sjg:gridColumn name="time_Assembled" 
                            index="time_Assembled" 
                            title="Time_Assembled" 
                            editable="true"
                            sortable="true"/>
            <sjg:gridColumn name="model" 
                            index="model" 
                            title="Model"  
                            editable="true"
                            sortable="true"/>
            <sjg:gridColumn name="batch_Id" 
                            index="batch_Id" 
                            title="Batch_Id" 
                            sortable="true"/>
            <sjg:gridColumn name="process_Code" 
                            index="process_Code" 
                            title="Process_Code"  
                            editable="true"
                            sortable="false"/>
            <sjg:gridColumn name="dc_Power_PCB_Serial" 
                            index="dc_Power_PCB_Serial" 
                            title="Dc_Power_PCB_Serial" 

                            editable="true"
                            sortable="dc_Power_PCB_Serial"/>
            <sjg:gridColumn name="control_Power_PCB_Serial" 
                            index="control_Power_PCB_Serial" 
                            title="Control_Power_PCB_Serial" 
                            editable="true"
                            sortable="control_Power_PCB_Serial"/>
            <sjg:gridColumn name="mains_Power_PCB_Serial" 
                            index="mains_Power_PCB_Serial" 
                            title="Mains_Power_PCB_Serial" 
                            editable="true"
                            sortable="mains_Power_PCB_Serial"/>
            <sjg:gridColumn name="blower_Serial" 
                            index="blower_Serial" 
                            title="Blower_Serial" 
                            editable="true"
                            sortable="blower_Serial"/>
            <sjg:gridColumn name="heaterPlate_Serial" 
                            index="heaterPlate_Serial" 
                            title="HeaterPlate_Serial" 
                            editable="true"
                            sortable="heaterPlate_Serial"/>
            <sjg:gridColumn name="last_Process" 
                            index="last_Process" 
                            title="Last_Process" 
                            editable="true"
                            sortable="last_Process"/>
            <sjg:gridColumn name="last_Date" 
                            index="last_Date"  
                            formatter="date" 
                            formatoptions="{newformat : 'd.m.Y H:i', srcformat : 'Y-m-d H:i:s'}"
                            editable="true"
                            title="Last_Date" sortable="last_Date"/>
            <sjg:gridColumn name="updatedTime" 
                            index="updatedTime" 
                            title="UpdatedTime" 
                            sortable="updatedTime"/>




        </sjg:grid>
    </body>
</html>

这是我的动作类

    package lotmovement.action;

    import com.opensymphony.xwork2.*;
    import java.util.Date;
    import lotmovement.business.crud.ProductCrud;
    import lotmovement.business.crud.RecordExistProduct;
    import lotmovement.business.crud.crudinterface.CrudInterface;
    import lotmovement.business.entity.Product;
    import org.apache.struts2.interceptor.validation.SkipValidation;


    public class ProductAction extends ActionSupport implements CrudInterface {

        private Long product_Id;
        private String serial_Number;
        private String date_Assembled;
        private String time_Assembled;
        private String model;
        private int batch_Id;
        private int process_Code;
        private String dc_Power_PCB_Serial;
        private String control_Power_PCB_Serial;
        private String mains_Power_PCB_Serial;
        private String blower_Serial;
        private String heaterPlate_Serial;
        private String last_Process;
        private String last_Date;

        private Date today;
        private Long product_Key; --> this is the hidden field


        ProductCrud productCrud;

        RecordExistProduct recordExistProduct;

        Product product;

        public Product getProduct() {
            return product;
        }

        public void setProduct(Product product) {
            this.product = product;
        }



        public ProductCrud getProductCrud() {
            return productCrud;
        }

        public void setProductCrud(ProductCrud productCrud) {
            this.productCrud = productCrud;
        }

        public RecordExistProduct getRecordExistProduct() {
            return recordExistProduct;
        }

        public void setRecordExistProduct(RecordExistProduct recordExistProduct) {
            this.recordExistProduct = recordExistProduct;
        }



        @Override
        public void validate() {
          setToday(new Date());

          if(recordExistProduct.ProductExist(getSerial_Number())==true){
                addActionError("Record already exist. Please use another Serial Number");
          }

        }

        @Override

        public String execute() {
          setToday(new Date());

            return SUCCESS;
        }

        @SkipValidation
        public String Initialise_Product(){

              setToday(new Date());
              return SUCCESS;
        }

        @Override
        @SkipValidation
        public void Update() {

        PopulateProduct();     
        }

        @Override
        @SkipValidation
        public void Insert() {

            PopulateProduct();
            productCrud.Insert(product);



        }

        @Override
        @SkipValidation
        public void Delete() {
            throw new UnsupportedOperationException("Not supported yet.");
        }


        public void PopulateProduct()
        {
            product.setProduct_Id(getProduct_Id());
            product.setSerial_Number(getSerial_Number());
            product.setDate_Assembled(getDate_Assembled());
            product.setTime_Assembled(getTime_Assembled());
            product.setModel(getModel());
            product.setBatch_Id(getBatch_Id());
            product.setProcess_Code(getProcess_Code());
            product.setDc_Power_PCB_Serial(getDc_Power_PCB_Serial());
            product.setControl_Power_PCB_Serial(getControl_Power_PCB_Serial());
            product.setMains_Power_PCB_Serial(getMains_Power_PCB_Serial());
            product.setBlower_Serial(getBlower_Serial());
            product.setHeaterPlate_Serial(getHeaterPlate_Serial());
            product.setLast_Process(getLast_Process());
            product.setLast_Date(getLast_Date());
        }











        public Long getProduct_Id() {
            return product_Id;
        }

        public void setProduct_Id(Long product_Id) {
            this.product_Id = product_Id;
        }

        public String getSerial_Number() {
            return serial_Number;
        }

        public void setSerial_Number(String serial_Number) {
            this.serial_Number = serial_Number;
        }

        public String getDate_Assembled() {
            return date_Assembled;
        }

        public void setDate_Assembled(String date_Assembled) {
            this.date_Assembled = date_Assembled;
        }

        public String getTime_Assembled() {
            return time_Assembled;
        }

        public void setTime_Assembled(String time_Assembled) {
            this.time_Assembled = time_Assembled;
        }

        public String getModel() {
            return model;
        }

        public void setModel(String model) {
            this.model = model;
        }

        public int getBatch_Id() {
            return batch_Id;
        }

        public void setBatch_Id(int batch_Id) {
            this.batch_Id = batch_Id;
        }

        public int getProcess_Code() {
            return process_Code;
        }

        public void setProcess_Code(int process_Code) {
            this.process_Code = process_Code;
        }

        public String getDc_Power_PCB_Serial() {
            return dc_Power_PCB_Serial;
        }

        public void setDc_Power_PCB_Serial(String dc_Power_PCB_Serial) {
            this.dc_Power_PCB_Serial = dc_Power_PCB_Serial;
        }

        public String getControl_Power_PCB_Serial() {
            return control_Power_PCB_Serial;
        }

        public void setControl_Power_PCB_Serial(String control_Power_PCB_Serial) {
            this.control_Power_PCB_Serial = control_Power_PCB_Serial;
        }

        public String getMains_Power_PCB_Serial() {
            return mains_Power_PCB_Serial;
        }

        public void setMains_Power_PCB_Serial(String mains_Power_PCB_Serial) {
            this.mains_Power_PCB_Serial = mains_Power_PCB_Serial;
        }

        public String getBlower_Serial() {
            return blower_Serial;
        }

        public void setBlower_Serial(String blower_Serial) {
            this.blower_Serial = blower_Serial;
        }

        public String getHeaterPlate_Serial() {
            return heaterPlate_Serial;
        }

        public void setHeaterPlate_Serial(String heaterPlate_Serial) {
            this.heaterPlate_Serial = heaterPlate_Serial;
        }

        public String getLast_Process() {
            return last_Process;
        }

        public void setLast_Process(String last_Process) {
            this.last_Process = last_Process;
        }

        public String getLast_Date() {
            return last_Date;
        }

        public void setLast_Date(String last_Date) {
            this.last_Date = last_Date;
        }

          public Date getToday() {
            return today;
        }

        public void setToday(Date Today) {
            this.today = Today;
        }

        public Long getProduct_Key() {
            return product_Key;
        }

        public void setProduct_Key(Long product_Key) {
            this.product_Key = product_Key;
        }



    }

1 个答案:

答案 0 :(得分:0)

尝试在<s:url>

中传递参数
<s:url id="remoteurl" action="ProductMain" namespace="/">
   <s:param name="product_Key"><s:property value="product_Key"/></s:param>
</s:url>