将选定的复选框值传递给Struts2中的数据库

时间:2014-12-19 13:52:14

标签: java jsp struts2 jstl

我正在使用Struts2,这是我在JSP中的表单

<s:form target="_top" cssClass="table-controls form-wrapper" name="incentives" id="frmIncentives" action="saveIncentives.action" method="post">
        <div id="actionErrors" style="color: red;">
                        <s:actionerror />
                        <s:fielderror />
        </div>
            <table id ="showIncentives" cellspacing="0" cellpadding="0" border="1" width="500" >
                <tr style="font-weight: bold">
                    <td><s:checkbox name="Select All" id="select_all"
                            theme="simple" /></td>
                    <td>
                    <td></td>
                    <td></td>
                    <td><s:text name="Marketing Incentive" /></td>
                    <td></td>
                    <td><s:text name="Advertising Incentive" /></td>
                    <td></td>
                    <td><s:text name="Channel Placement Incentive" /></td>
                    <td></td>
                    </tr>
            <s:iterator value="incentiveList" status="stat">
                    <tr>
                        <td><s:checkbox name="checkboxes[%{#stat.index}]" theme="simple" /></td>
                        <td></td>
                        <td><s:property value="pricingTierId" /> <s:hidden value="%{incentiveList.pricingTierId}" /></td>
                        <td><s:property value="marketingAmount" /> <s:hidden value="%{incentiveList.marketingAmount}" /></td>
                        <td></td>
                        <td><s:property value="marketingIncentive" /></td>
                        <td></td>
                        <td><s:property value="advertizingIncentive" /></td>
                        <td></td>
                        <td><s:property value="channelPlacementIncentive" /></td>
                        <td></td>
                    </tr>
            </s:iterator>
            </table>
            <c:if test="${fn:length(incentiveList) == 0}">
                <s:submit id="submitButton" name="submit" value="Submit Incentives" onclick='return closeWindow()' disabled="true"/>
            </c:if>
            <c:if test="${fn:length(incentiveList) gt 0}">
                <s:submit id="submitButton" name="submit" value="Submit Incentives" onclick='return closeWindow()' disabled="false"/>
            </c:if>
    </s:form>

现在我的Action类中的方法如下

private String marketingIncentive;
    private String advertizingIncentive;
    private String channelPlacementIncentive;
    private BigDecimal marketingAmount;
    private Integer pricingTierId;


    //getters and setters
    public String getMarketingIncentive() {
        return marketingIncentive;
    }
    public void setMarketingIncentive(String marketingIncentive) {
        this.marketingIncentive = marketingIncentive;
    }
    public String getAdvertizingIncentive() {
        return advertizingIncentive;
    }
    public void setAdvertizingIncentive(String advertizingIncentive) {
        this.advertizingIncentive = advertizingIncentive;
    }
    public String getChannelPlacementIncentive() {
        return channelPlacementIncentive;
    }
    public void setChannelPlacementIncentive(String channelPlacementIncentive) {
        this.channelPlacementIncentive = channelPlacementIncentive;
    }
    public BigDecimal getMarketingAmount() {
        return marketingAmount;
    }
    public void setMarketingAmount(BigDecimal marketingAmount) {
        this.marketingAmount = marketingAmount;
    }


    // Method to save the selected checkbox values 

    public String saveIncentives(){
        WorkFlowDAO workFlowDAO = new WorkFlowDAO();

        try {
            if(submit != null){

                IncentiveDetailsDO incentiveDetailsDO = new IncentiveDetailsDO();
                incentiveDetailsDO.setPricingTierId(pricingTierId);
                incentiveDetailsDO.setMarketingAmount(marketingAmount);
                incentiveDetailsDO.setMarketingIncentive(marketingIncentive);
                incentiveDetailsDO.setAdvertizingIncentive(advertizingIncentive);
                incentiveDetailsDO.setChannelPlacementIncentive(channelPlacementIncentive);
                workFlowDAO.applyIncentives(incentiveDetailsDO);
                closeWindow = true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }

  1. pricingTierId,marketingIncentive,advertizingIncentive, channelPlacementIncentive和channelPlacementIncentive
  2. 尽管在Action类中有getter和setter,

    仍会传递给函数null。

    我已按照以下建议进行了更改

    <s:iterator value="incentiveList" status="stat">
                    <tr>
                        <td><s:checkbox name="checkboxes[%{#stat.index}]" theme="simple" /></td>
                        <td></td>
                        <td><s:property value="pricingTierId"  /> <s:hidden name="pricingTierId" value="%{incentiveList.pricingTierId}" /></td>
                        <td><s:property value="marketingAmount" /> <s:hidden name="marketingAmount" value="%{incentiveList.marketingAmount}" /></td>
                        <td></td>
                        <td><s:property value="marketingIncentive" />
                        <s:hidden name="marketingIncentive" value="%{incentiveList.pricingTierId}" />
                        </td>
                        <td></td>
                        <td><s:property value="advertizingIncentive"/> 
                        <s:hidden name="advertizingIncentive" value="%{incentiveList.pricingTierId}"/> 
                        </td>
                        <td><s:property value="channelPlacementIncentive" /> 
                        <s:hidden name="channelPlacementIncentive" value="%{incentiveList.pricingTierId}" />
                        </td>
                        <td></td>
                    </tr>
            </s:iterator>   
    

    仍将所有值都传递为null。 请建议

1 个答案:

答案 0 :(得分:1)

您的表单将如下所示。

<s:form target="_top" cssClass="table-controls form-wrapper" name="incentives" id="frmIncentives" action="saveIncentives.action" method="post">
        <div id="actionErrors" style="color: red;">
                        <s:actionerror />
                        <s:fielderror />
        </div>
            <table id ="showIncentives" cellspacing="0" cellpadding="0" border="1" width="500" >
                <tr style="font-weight: bold">
                    <td><s:checkbox name="Select All" id="select_all"
                            theme="simple" /></td>
                    <td>
                    <td></td>
                    <td></td>
                    <td><s:text name="Marketing Incentive" /></td>
                    <td></td>
                    <td><s:text name="Advertising Incentive" /></td>
                    <td></td>
                    <td><s:text name="Channel Placement Incentive" /></td>
                    <td></td>
                    </tr>
            <s:iterator value="incentiveList" status="stat">
                    <tr>
                        <td><s:checkbox name="checkboxes[%{#stat.index}]" theme="simple" /></td>
                        <td></td>
                        <td><s:property value="pricingTierId" /> <s:hidden name="pricingTierId" value="%{incentiveList.pricingTierId}" /></td>
                        <td><s:property value="marketingAmount" /> <s:hidden name="marketingAmount" value="%{incentiveList.marketingAmount}" /></td>
                        <td></td>
                        <td><s:property value="marketingIncentive" /><s:hidden name="marketingIncentive" value="%{incentiveList.marketingIncentive}" /></td>
                        <td></td>
                        <td><s:property value="advertizingIncentive" /><s:hidden name="advertizingIncentive" value="%{incentiveList.advertizingIncentive}" /></td>
                        <td></td>
                        <td><s:property value="channelPlacementIncentive" /><s:hidden name="channelPlacementIncentive" value="%{incentiveList.channelPlacementIncentive}" /></td>
                        <td></td>
                    </tr>
            </s:iterator>
            </table>
            <c:if test="${fn:length(incentiveList) == 0}">
                <s:submit id="submitButton" name="submit" value="Submit Incentives" onclick='return closeWindow()' disabled="true"/>
            </c:if>
            <c:if test="${fn:length(incentiveList) gt 0}">
                <s:submit id="submitButton" name="submit" value="Submit Incentives" onclick='return closeWindow()' disabled="false"/>
            </c:if>
    </s:form>