简体   繁体   中英

Jsp + JS + DBLookup + inside condition , solution?

Here I have 2 tags queries, I want to call them in JSP inside a Text List with condition if based on previous selected item from list also, here is my code : First the JS function :

<script language="javascript">


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




</script>

and here is the 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">
                            <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>

Sorry the code is a bit messy ! I need some help to fix that, thanks by advance :)

here is the solution for who is interested :

First in JS part :

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

} 

populate_spotter_instant is a service which helps to apply chages instantle it means just you write in the form and the struts form will save the new values, In Service file here is the service, its logic is to take the values from the current form and save then in the object, something similar to this :

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();
    }

OK, NOW IN 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>

So as u can see I used Scriplets to instantiate the form and use its attributes, as already saved my current form's values and called the JS method in the Base Dorp down input, so when I will change the Base value all the forms values will be repopulated and then my outlet will have access to the new Base value and based on that it will loat the suitable query refering to these QueryTags :

<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>

That's all, all is done and it works perfectly for me, if not enough details lemme know !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM