java 使用 DisplayTag 库框架通过 Struts2 进行分页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/777199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pagination through Struts2 using DisplayTag Library Framework
提问by Nirmal
I want to apply pagination for some class of my application, in which i am using spring, struts2 & hibernate. Here i am calling action class from welcome.jsp file. It has following code :
我想为我的应用程序的某个类应用分页,其中我使用的是 spring、struts2 和 hibernate。这里我从welcome.jsp 文件调用动作类。它有以下代码:
<s:form action="marketing/allCountry.action">
<s:submit value="Click"></s:submit>
</s:form>
Now my allCountry.actionclass of java has following code :
现在我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;
}
}
It fetches the data properly, that i confirmed by printing countryList object. But now i am redirecting from SUCCESSto country.jsp. The code of country.jspis :
它正确获取数据,我通过打印 countryList 对象确认了这一点。但现在我从 重定向SUCCESS到country.jsp。的代码country.jsp是:
<display:table list="countryList" requestURI="CountryAllAction" pagesize="3">
<display:column property="id" title="ID" />
<display:column property="name" />
</display:table>
Now at the time executing my application i am getting run time error like :
现在在执行我的应用程序时,我收到运行时错误,例如:
javax.servlet.ServletException: javax.servlet.ServletException: Exception: [.LookupUtil] Error looking up property "id" in object type "java.lang.String". Cause: Unknown property 'id'
javax.servlet.ServletException:javax.servlet.ServletException:异常:[.LookupUtil] 在对象类型“java.lang.String”中查找属性“id”时出错。原因:未知的属性“id”
What is the solution to this type of error?
此类错误的解决方案是什么?
回答by Ruggs
You need to have a getter for your countryList in your action.
您需要在您的操作中为您的 countryList 设置一个吸气剂。
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;
}
回答by andrey
I'm not familiar with Struts2 but it seems that DisplayTag can't find your countryList in any scope.
我不熟悉 Struts2,但似乎 DisplayTag 在任何范围内都找不到您的 countryList。
I don't see you putting countryList in from or request. If I'm not right may be you need to specify form name like
<display:table list="${yourStrutsFormName.countryList}" ...
我没有看到您将 countryList 放入 from 或 request。如果我不正确,您可能需要指定表单名称,例如
<display:table list="${yourStrutsFormName.countryList}" ...
回答by A_M
You need make "countryList" available to DisplayTag, e.g. by doing something like:
您需要使 DisplayTag 可以使用“countryList”,例如通过执行以下操作:
request.setAttribute("countryList", countryList);
in your action (unless there's a cleverer Struts2 way of doing this). I think you can get hold of the request in your action by making it implement ServletRequestAware
在你的行动中(除非有更聪明的 Struts2 方式来做到这一点)。我认为您可以通过实现ServletRequestAware来掌握您的操作中的请求
You also need to make sure that your Country class has id and name properties.
您还需要确保您的 Country 类具有 id 和 name 属性。
回答by A_M
Use the following code.Instead of list attribute use name attribute.
使用以下代码。使用名称属性代替列表属性。
<display:table name="countryList" requestURI="CountryAllAction" pagesize="3">
<display:column property="id" title="ID" />
<display:column property="name" />
</display:table>
回答by A_M
u dont need to do any thing just use list variable in display tag name i.e
你不需要做任何事情,只需在显示标签名称中使用列表变量即可
<display:table name="yourListnameinaction">
</display>

