【问题标题】:Pagination through Struts2 using DisplayTag Library Framework使用 DisplayTag 库框架通过 Struts2 进行分页
【发布时间】:2010-10-21 02:28:43
【问题描述】:

我想为我的应用程序的某些类应用分页,其中我使用的是 spring、struts2 和 hibernate。在这里,我从welcome.jsp 文件中调用动作类。它有以下代码:

<s:form action="marketing/allCountry.action">  
         <s:submit value="Click"></s:submit>  
         </s:form>  

现在我的allCountry.action java 类有以下代码:

public String executeAction() throws Exception {
        try {
            countryList = new ArrayList<Country>();
            countryList = this.countrySecurityProcessor.findByAll(0, null, null, null, null, false, false, null, null, null, null, 0);
            System.out.println("countryList = "+countryList);
            return ActionSupport.SUCCESS;

        } catch (Exception ex) {
            return ActionSupport.ERROR;
        }
}

它正确地获取数据,我通过打印 countryList 对象确认。但现在我从SUCCESS 重定向到country.jspcountry.jsp的代码是:

<display:table list="countryList" requestURI="CountryAllAction" pagesize="3">
    <display:column property="id" title="ID" />
    <display:column property="name" />
</display:table>

现在在执行我的应用程序时,我遇到了运行时错误,例如:

javax.servlet.ServletException: javax.servlet.ServletException: 异常:[.LookupUtil] 在对象类型中查找属性“id”时出错 “java.lang.String”。原因:未知属性 'id'

此类错误的解决方法是什么?

【问题讨论】:

    标签: java struts2


    【解决方案1】:

    你不需要做任何事情,只需在显示标签名称中使用列表变量,即

    <display:table name="yourListnameinaction">
    
    </display>
    

    【讨论】:

      【解决方案2】:

      使用以下代码。使用名称属性代替列表属性。

      <display:table name="countryList" requestURI="CountryAllAction" pagesize="3">
       <display:column property="id" title="ID" />
       <display:column property="name" />
      </display:table>
      

      【讨论】:

        【解决方案3】:

        您需要为 DisplayTag 提供“countryList”,例如通过执行以下操作:

        request.setAttribute("countryList", countryList);

        在你的行动中(除非有更聪明的 Struts2 方法来做到这一点)。我认为您可以通过实现ServletRequestAware 来在您的操作中掌握请求

        您还需要确保您的 Country 类具有 id 和 name 属性。

        【讨论】:

          【解决方案4】:

          你需要在你的行动中为你的 countryList 设置一个 getter。

          List<Country> countryList = new ArrayList<Country>();
          
          public String executeAction() throws Exception {
            try {
               countryList = this.countrySecurityProcessor.findByAll(0, null, null, null, null, false, false, null, null, null, null, 0);
               System.out.println("countryList = "+countryList);
               return ActionSupport.SUCCESS;
          
            } catch (Exception ex) {
               return ActionSupport.ERROR;
            }
          
          }
          
          public List<Country> getCountryList() {
            return countryList;
          }
          

          【讨论】:

            【解决方案5】:

            我对 Struts2 不熟悉,但 DisplayTag 似乎在任何范围内都找不到您的 countryList。

            我没有看到您将 countryList 放入 from 或 request 中。如果我不正确,您可能需要指定表单名称,例如 &lt;display:table list="${yourStrutsFormName.countryList}" ...

            【讨论】:

              猜你喜欢
              • 2023-03-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-03-15
              • 1970-01-01
              相关资源
              最近更新 更多