Search code examples
javastrutshrefstruts-1

How to invoke a link using href or anything from my java method?


I need to invoke the href link on my java method. My sample code is defined below. After finishing my logic i need to invoke the URL. The URL is used to open an Excel workbook and import our bean values to there.

public void populateExcelReportValues(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        String METHOD_NAME = "populateExcelReportValues";
        log.entering(CLASS_NAME, METHOD_NAME);

        //Rating Element Id 
        String ratingElementIdForExcel = request.getParameter("ratingElementIdFromJSP");
        //Instance Type Name
        String instanceTypeForExcel = request.getParameter("instanceTypeFromJSP");
        int ratingElementIdForExcelInt = 0;

        String instanceTypeValueForExcel = "";
        if(ratingElementIdForExcel != null && ratingElementIdForExcel.trim().length()>0)
        {
            ratingElementIdForExcelInt = Integer.parseInt(ratingElementIdForExcel);
            System.out.println("ratingElementIdForExcelInt: "+ratingElementIdForExcelInt);

        }
        if(instanceTypeForExcel != null && instanceTypeForExcel.trim().length()>0)
        {
            instanceTypeValueForExcel = instanceTypeForExcel.trim();
            System.out.println("instanceTypeValueForExcel: "+instanceTypeValueForExcel);
        }
        switch (ratingElementIdForExcelInt) 
        {
            case 1: //ASN Accuracy - Rating Element ID - 1 
                    weeklyDeliveryInstancesRatingElementQO = getASNAccuracyRatingElement(instanceTypeValueForExcel);
                    //return weeklyDeliveryInstancesRatingElementQO;
                    break;
            default:
                    weeklyDeliveryInstancesRatingElementQO = new ArrayList<WeeklyDeliveryInstancesRatingElementQO>();
                    break;
        }
        //How to invoke this url to here
        **(MultiTableExportServlet?multitablesId=WeeklyDeliveryInstances-Count&amp;name=WeeklyDeliveryInstances-Count&amp;type=excel)**
        log.exiting(CLASS_NAME, METHOD_NAME);
    }

In above my code i showed to bold for your identification.

Jsp page:

<display:table name="${weeklyDlvyInstancesDashboardReportForm.asnAccuracyListQO}" uid="asnAccuracyListUID" sort="list" defaultsort="1" 
                                        requestURI="/weeklyDlvyInstancesDashboardReportPre.do?method=httpGet" excludedParams="method"
                                        decorator="com.ford.mpl.superg.decorator.WeeklyDeliveryInstancesTypeTableDecorator" keepStatus="true">
                                        <%@include file="/jsp/include/displaytag.jsp"%>
                                        <c:set value="${asnAccuracyListUID.firstWeekOfCountLabel}" var="asnAccuracyFirstWeekOfCount"/>
                                        <c:set value="${asnAccuracyListUID.secondWeekOfCountLabel}" var="asnAccuracySecondWeekOfCount"/>
                                        <c:set value="${asnAccuracyListUID.thirdWeekOfCountLabel}" var="asnAccuracyThirdWeekOfCount"/>
                                        <c:set value="${asnAccuracyListUID.fourthWeekOfCountLabel}" var="asnAccuracyFourthWeekOfCount"/>
                                        <c:set value="${asnAccuracyListUID.fifthWeekOfCountLabel}" var="asnAccuracyFifthWeekOfCount"/>
                                        <c:set value="${asnAccuracyListUID.sixthWeekOfCountLabel}" var="asnAccuracySixthWeekOfCount"/>

                                        <c:choose>
                                        <c:when test="${asnAccuracyListUID.instanceTypeDescription != null && asnAccuracyListUID.instanceTypeDescription != 'Sum'}">
                                            <display:column property="instanceTypeDescription" title="Instance Type" sortable="false"/>
                                        </c:when>
                                        <c:otherwise>
                                            <display:column property="instanceTypeDescription" title="Instance Type" sortable="false" style="font-weight:bold;text-align:center"/>
                                        </c:otherwise>
                                        </c:choose>


                                        <display:column property="firstWeekOfCount" title="${asnAccuracyFirstWeekOfCount}" sortable="false"/>
                                        <display:column property="secondWeekOfCount" title="${asnAccuracySecondWeekOfCount}" sortable="false"  />
                                        <display:column property="thirdWeekOfCount" title="${asnAccuracyThirdWeekOfCount}" sortable="false"  />
                                        <display:column property="fourthWeekOfCount" title="${asnAccuracyFourthWeekOfCount}" sortable="false" />
                                        <display:column property="fifthWeekOfCount" title="${asnAccuracyFifthWeekOfCount}" sortable="false" />
                                        <display:column property="sixthWeekOfCount" title="${asnAccuracySixthWeekOfCount}" sortable="false"/>
                                    </display:table>
                                </fieldset>

                                <export:multitables id="WeeklyDeliveryInstances-Count">
                                        <export:table title="tablename" source="${weeklyDlvyInstancesDashboardReportForm.weeklyDeliveryInstancesRatingElementQO}">
                                            <export:column property="shipCode" title="Ship Code" />
                                            <export:column property="plantOrRecLoc" title="Plant/Rec Loc" />
                                            <export:column property="instanceType" title="Instance Type" />
                                            <export:column property="fordOrg" title="Ford Org" />
                                            <export:column property="OrgType" title="Org Type" />
                                            <export:column property="region" title="Region" />
                                            <export:column property="shipDate" title="Ship Date" />
                                            <export:column property="errorTransmitted" title="Error Transmitted" />
                                            <export:column property="externalAlertNo" title="External Alert Number" />
                                            <export:column property="asnNumber" title="ASN Number"/>
                                            <export:column property="packingSlipNumber" title="Packing Slip Number"/>
                                        </export:table>
                                    </export:multitables>

Solution

  • I would take this approach:

    In struts-config.xml add a forward to the servlet in your action, for example:

    <forward name="Success" path="/servlet/MultiTableExportServlet"/>
    

    (You'll need to change 'path' to the url-pattern of your servlet if it is different from above).

    In your Action class you can redirect to this forward by doing this:

        ActionForward originalForward = a_mapping.findForward ("Success");  
        String path = originalForward.getPath() 
            + "?multitablesId=WeeklyDeliveryInstances-Count&name=WeeklyDeliveryInstances-Count&type=excel";     
    
        ActionForward forward = new ActionForward(path);
        forward.setRedirect(true);
        return (forward);
    

    Your query string (multitablesId=WeeklyDeliveryInstances-Count etc) looks wrong. Is WeeklyDeliveryInstances-Count a variable? I can't see where you define or use it.