【问题标题】:Iterating List using DAO Java使用 DAO Java 迭代列表
【发布时间】:2013-11-12 01:20:00
【问题描述】:

在这段代码中,我声明了一个包含列表值的变量,即acode。我可以看到这个列表中的值,但我不知道如何使用adao.adao.findAllacctDesc(**acode**) 迭代这些值。我如何遍历这个列表,以便选项显示值?

代码如下:

<%
TblTaxTypeDAO tdao = DAOFactory.getDaoManager(TblTaxType.class);
TblAccountCodesDAO adao = DAOFactory.getDaoManager(TblAccountCodes.class);

List<TblTaxType> acode =  null;

String tcode = request.getParameter("taxt");
String bcode = request.getParameter("bfns");


acode = tdao.findAllAcctCode(bcode, tcode);
Debugger.print(acode);

List<TblAccountCodes> acctdesclist = null;
acctdesclist = adao.findAllacctDesc(acode); <= Having error in this line because acode is a list not a string.

String acctdescoptions = "";

if( acctdesclist!=null) {
if( acctdesclist.size()>0 ) {
for(int i=0; i<acctdesclist.size();i++) {
TblAccountCodes acctcode = (TblAccountCodes) acctdesclist.get(i);
acctdescoptions += "<option value='"+acctcode.getAcctCode()+"'>"+acctcode.getAcctDesc()+"</option>";                                        
acctcode = null;
    }
  }
}

adao = null;
acctdesclist = null;
%>
<%=acctdescoptions%>

【问题讨论】:

  • 不要混合使用 Java 和 HTML。使用jstl
  • 我知道,但我只需要这段代码。仅用于此目的。
  • 我建议你不要那样想。尽早开始养成良好的习惯。
  • 好的,我很感激。
  • 我从未见过声明的代码,它是什么?此外,这不会编译:Debugger.print(acode.);

标签: java jsp loops iterator dao


【解决方案1】:

改变

List<TblAccountCodes> acctdesclist = null;
acctdesclist = adao.findAllacctDesc(acode); <= Having error in this line because acode is   a list not a string.

通过

List<TblAccountCodes> acctdesclist = null;
for(TblTaxType T:acode){
acctdesclist.add(adao.findAllacctDesc(T.getString))
}

这是你想要的吗?

其中 getString 是 TblTaxType 中的函数,您可以在其中返回所需的字符串。

【讨论】:

  • 错误:The method findAllacctDesc(String) in the type TblAccountCodesDAO is not applicable for the arguments (TblTaxType)
  • 错误:The method add(TblAccountCodes) in the type List&lt;TblAccountCodes&gt; is not applicable for the arguments (List&lt;TblAccountCodes&gt;)
  • 如果你想让我帮助你更多,你需要提供整个代码
  • 您需要什么确切的代码?我相信我已经给出了所需的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-02
  • 1970-01-01
  • 2012-09-13
相关资源
最近更新 更多