Jsp + JS + DBLookup +内部条件,解决方案?

时间:2014-09-30 09:57:38

标签: javascript jsp jsp-tags

这里我有2个标签查询,我想在JSP中调用它们在文本列表中有条件,如果基于列表中先前选择的项目,这里是我的代码: 首先是JS函数:

<script language="javascript">


    var basevalue;
    function checkoutlet(id)
{
    basevalue= document.getElementsById(id).value;
    return basevalue;
}




</script>

这是JSP部分:

<tr>
                <td class="txtMF">Base<span class="txtnbmed"><span class="txtredsmall">*</span></span></td>
                <td class="txtnbmed" Id="base">
                            <html:select property="spotterBase">
                            <option value="">-- Select --</option>
                            <dblookup:DBLookupTagVer2 lookUpName="common.spotter.base" />
                            </html:select>
                        </td>
            </tr>
            <tr>
                <td class="txtMF">Outlet Code</td>
                <td class="txtnbmed">
                            <html:select property="spotterOutlet" onchange="checkoutlet('<%=base%>');">

                            <option value="">-- Select --</option>

                            <script language="javascript">
                             if (basevalue.equals("Branch")){ 
                            </script>

                            <dblookup:DBLookupTagVer2 lookUpName="common.spotter.outletb" />

                            <script language="javascript">
                             } else if (basevalue.equals("Dealer")){ 
                            </script> 

                            <dblookup:DBLookupTagVer2 lookUpName="common.spotter.outletd" />
                             <script language="javascript"> } </script> 
                            </html:select>
                        </td>
            </tr>

对不起,代码有点乱!我需要一些帮助才能解决这个问题,感谢前进:)

1 个答案:

答案 0 :(得分:0)

这是谁感兴趣的解决方案:

首先在JS部分:

function checkoutlet()
{
document.forms[0].action = "populate_spotter_instant.do";
document.forms[0].submit();

} 

populate_spotter_instant是一个服务,它有助于应用chages instantle它意味着你只需要在表单中写入,而struts表单将保存新值,In Service file这里是服务,它的逻辑是从当前表单获取值然后保存在对象中,类似于:

private ActionForward doPopulateSpotterInstant(
        ActionMapping mapping,
        SpotterSelectForm spotterForm,
        SessionData sessionData)
        throws Exception
    {
    log.debug("doPopulateSpotterInstant | Begin ");

    SpotterRequestValueObject requestVo = new SpotterRequestValueObject();

    String branchCode2 = getBranchCode(sessionData);
    log.debug("doPopulateSpotterInstant | branchCode:" + branchCode2);

    requestVo.setBranchCode(branchCode2);


    String spotterCode = spotterForm.getSpotterCode();
    log.debug("doPopulateSpotterInstant | spotterCode:" + spotterCode);

    String companyCode = spotterForm.getCompanyCode();
    log.debug("doPopulateSpotterInstant | companyCode:" + companyCode);

    String createdBy = getUserID(sessionData);
    log.debug("doPopulateSpotterInstant | createdBy:" + createdBy);

    Timestamp createTimestamp = new Timestamp(System.currentTimeMillis());
    log.debug("doPopulateSpotterInstant | createdTM:" + createTimestamp);

    requestVo.setSpotterCode(spotterCode);

    Gspotter gSpotter = new Gspotter();
    GspotterPK pk = new GspotterPK();
    pk.setSpCode(spotterCode);
    pk.setComp_code(companyCode);
    gSpotter.setComp_id(pk);
    gSpotter.setName(spotterForm.getSpotterName());
    gSpotter.setSpId(spotterForm.getSpotterId());
    gSpotter.setBr_code(branchCode2);
    gSpotter.setType(spotterForm.getSpotterType());
    gSpotter.setBase(spotterForm.getSpotterBase());
    gSpotter.setOutlet(spotterForm.getSpotterOutlet());
    gSpotter.setDesignation(spotterForm.getDesignation());
    gSpotter.setContact_no(spotterForm.getSpotterContactNo());
    gSpotter.setAddr1(spotterForm.getSpotterAddress1());
    gSpotter.setAddr2(spotterForm.getSpotterAddress2());
    gSpotter.setAddr3(spotterForm.getSpotterAddress3());
    gSpotter.setAddr4(spotterForm.getSpotterAddress4());
    gSpotter.setPostcode(spotterForm.getSpotterPostCode());
    gSpotter.setCreatedBy(createdBy);
    gSpotter.setCreateTimestamp(createTimestamp);

    requestVo.setSpotterObj(gSpotter);


    log.debug("doPopulateSpotterInstant | End ");
    return mapping.getInputForward();
    }

好的,现在在JSP PART中:

<tr>
                <td class="txtMF">Base<span class="txtnbmed"><span class="txtredsmall">*</span></span></td>
                <td class="txtnbmed" id="base">
                            <html:select property="spotterBase" onchange="javascript:checkoutlet();">
                            <option value="">-- Select --</option>
                            <dblookup:DBLookupTagVer2 lookUpName="common.spotter.base" />
                            </html:select>
                        </td>
            </tr>
            <tr>
                <td class="txtMF">Outlet Code</td>
                <td class="txtnbmed">
                <%

                SpotterSelectForm form = (SpotterSelectForm)request.getAttribute("SpotterSelectForm");

                if(form.getSpotterBase() == null){
                 %>         <html:select property="spotterOutlet" disabled="true">

                            <option value="">-- Select --</option>                              
                            <dblookup:DBLookupTagVer2 lookUpName="common.spotter.outletd" >

                            </dblookup:DBLookupTagVer2>

                            </html:select>

                <%
                }else if(form.getSpotterBase() == 'B'){
                %>
                <html:select property="spotterOutlet">

                            <option value="">-- Select --</option>                              
                            <dblookup:DBLookupTagVer2 lookUpName="common.spotter.outletb" >

                            </dblookup:DBLookupTagVer2>

                            </html:select>
                <%
                }else if(form.getSpotterBase() == 'D'){
                 %>

                            <html:select property="spotterOutlet" >

                            <option value="">-- Select --</option>                              
                            <dblookup:DBLookupTagVer2 lookUpName="common.spotter.outletd" >

                            </dblookup:DBLookupTagVer2>

                            </html:select>


                <%
                }else {
                 %>         <html:select property="spotterOutlet" disabled="true">

                            <option value="">-- Select --</option>                              
                            <dblookup:DBLookupTagVer2 lookUpName="common.spotter.outletd" >

                            </dblookup:DBLookupTagVer2>

                            </html:select>

                <%
                }
                 %>

                        </td>
            </tr>

因为你可以看到我使用Scriplets来实例化表单并使用它的属性,因为已经保存了我当前的表单值并在Base Dorp down输入中调用了JS方法,所以当我改变Base时值将重新填充所有表单值,然后我的出口将访问新的Base值,并基于它将讨论参考这些QueryTag的合适查询:

<query cacheDuration="90000" cacheMaxRows="100" key="common.spotter.outletb" toCache="true">
    <stmt>
    <![CDATA[
    SELECT BIBRCD, BIBRCD||'-'||BIBRNM from e31dlib3.GBRANCH where BIBRTY='B' AND BIRS='A'
    ]]>
    </stmt>
</query>
<query cacheDuration="90000" cacheMaxRows="100" key="common.spotter.outletd" toCache="true">
    <stmt>
    <![CDATA[
    SELECT BIBRCD, BIBRCD||'-'||BIBRNM from e31dlib3.GBRANCH where BIBRTY='D' AND BIRS='A'
    ]]>
    </stmt>
</query>

总而言之,一切都已经完成,如果没有足够的细节知道,它对我来说非常有效!