【问题标题】:How to invoke a link using href or anything from my java method?如何使用 href 或我的 java 方法中的任何内容调用链接?
【发布时间】:2012-10-08 15:29:23
【问题描述】:

我需要在我的 java 方法上调用 href 链接。我的示例代码定义如下。完成我的逻辑后,我需要调用 URL。该 URL 用于打开 Excel 工作簿并将我们的 bean 值导入其中。

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

在上面我的代码中,我显示为粗体以供您识别。

Jsp页面:

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

【问题讨论】:

  • 您的意思是要将用户的请求重定向到 Web 应用程序中名为 MultiTableExportServlet 的 servlet?或者你的意思是你想从你的 Java 代码中调用这个 servlet,然后继续处理用户的请求,例如使用你的 Struts Action 类?
  • @martin,我想从我的 java 方法中重定向 multitableexportservlet
  • web.xml 中 MultiTableExportServlet servlet 的 servlet-mapping 中的 servlet-name 是什么?是multitableexportservlet吗?
  • @martin,servlet 名称是 MultiTableExportServlet
  • servlet-mapping 中的 url-pattern 是什么?

标签: java struts href struts-1


【解决方案1】:

我会采用这种方法:

在 struts-config.xml 中为你的 action 中的 servlet 添加一个转发,例如:

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

(如果与上述不同,您需要将“路径”更改为 servlet 的 url 模式)。

在您的 Action 类中,您可以通过执行以下操作重定向到此转发:

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

您的查询字符串(multitablesId=WeeklyDeliveryInstances-Count 等)看起来有误。 WeeklyDeliveryInstances-Count 是变量吗?我看不到您在哪里定义或使用它。

【讨论】:

  • 在转发到该 servlet 后,列表为空。我不知道它发生了。但我检查了它在转发函数之前具有值的列表....
  • 什么列表?如果这是一个请求属性,那么它会随着重定向而丢失。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-27
  • 2013-10-30
  • 1970-01-01
  • 2020-07-14
  • 1970-01-01
  • 2012-05-05
相关资源
最近更新 更多