开发者

Backing bean String[] access in JSF

开发者 https://www.devze.com 2022-12-18 12:05 出处:网络
First, apologies if you\'ve been involved with my recent questions.As this isn\'t a discussion forum, and comments are limited, my last hope is to ask a specific question with actual code in the hope

First, apologies if you've been involved with my recent questions. As this isn't a discussion forum, and comments are limited, my last hope is to ask a specific question with actual code in the hope that somehow I can reach the bottom of my problem.

OK. I've a backing bean called PrismBacking with this pertinent code:

     public class PrismBacking {
 private ArrayList dataList;

    public ArrayList<String> getFeedDataList() {
  XMLHandler xmlh = new XMLHandler();
  dataList = new ArrayList();
  Document doc = xmlh.getDoc(map); // catches removed

  // get all the feedNames from the doc, and the feedIds
  String[] FeedIds = xmlh.getXMLList("//feed/feed_id/text()");

  for (String feedId : FeedIds) {
   TaskListData tld = new TaskListData();
   tld.setFeedId(feedId);

   String feedName = xmlh.getValue("//feed[feed_id='" + feedId +"']" +"/feedname/text()");
   tld.setFeedName(feedName);

   String[] FTQs = xmlh.getList("//feed[feed_id='" + feedId +"']" +"/ftq/ftq_id/text()");
   for (String ftqId : FTQs) {
    logger.info("FTQ: " + ftqId);
   }
   tld.setFTQs(FTQs);

   dataList.add(tld);
  }

  setFeedDataListSize(dataList.size());
  return dataList;
 }

In class TaskListData,

    public class TaskListData {
 private String feedId;
 private String feedName;
 private String[] FTQar;

 public String g开发者_运维技巧etFeedId() {  
  return feedId;  
 }  

 public void setFeedId(String f) {  
  feedId = f;  
 } 

 public String getFeedName() {  
  return feedName;  
 }  

 public void setFeedName(String fn) {  
  feedName = fn;  
 } 

 public String[] getFTQs() {  
  return FTQar;  
 }  

 public void setFTQs(String[] ftqs) {  
  FTQar = ftqs;
 }  
}

so I've got my getters and setters setup, and my XPath all good. In my index.jsp jsf file:

    <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
    <!-- RichFaces tag library declaration -->
    <%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
    <%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
    <!-- JSTL XML lib declaration -->
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <!-- error redirect -->

    <f:view>
     <rich:page pageTitle="mypage" markupType="xhtml">
      <h:dataTable id="dt1" value="#{PrismBacking.feedDataList}" var="item"
       border="10" cellpadding="5" cellspacing="3" first="0"
       rows="#{PrismBacking.feedDataListSize}" width="50%" dir="LTR"
       frame="hsides" rules="all"
       summary="This is a JSF code to create dataTable.">

   <f:facet name="header">
    <h:outputText value="This is 'dataTable' demo" />
   </f:facet>

   <h:column>
    <f:facet name="header">
     <h:outputText value="id" />
    </f:facet>
    <h:outputText value="|#{item.feedId}|"></h:outputText>
   </h:column>

   <h:column>
    <f:facet name="header">
     <h:outputText value="name" />
    </f:facet>
    <h:outputText value="|#{item.feedName}|"></h:outputText>
   </h:column>

   <h:column>
    <f:facet name="header">
     <h:outputText value="ftqs" />
    </f:facet>
    <c:forEach items="#{item.FTQs}" var="jef">
     <h:outputText value="|#{jef}|" />
     <br />
    </c:forEach>

   </h:column>

  </h:dataTable>
  <h:outputText value="" />
 </rich:page>
</f:view>

OK, this compiles fine, and runs ok without error. I see from the logger.info line in PrismBacking:

|STDOUT| 2010-01-28 00:02:48,960 | INFO  | [http-8989-1]: feedId: 1 | feedSwitch: on | feedName: FEED1
|STDOUT| 2010-01-28 00:02:48,991 | INFO  | [http-8989-1]: FTQ: 1
|STDOUT| 2010-01-28 00:02:48,991 | INFO  | [http-8989-1]: feedId: 2 | feedSwitch: on | feedName: FEED2
|STDOUT| 2010-01-28 00:02:48,991 | INFO  | [http-8989-1]: FTQ: 1
|STDOUT| 2010-01-28 00:02:48,991 | INFO  | [http-8989-1]: feedId: 3 | feedSwitch: on | feedName: FEED3
|STDOUT| 2010-01-28 00:02:48,991 | INFO  | [http-8989-1]: FTQ: 1
|STDOUT| 2010-01-28 00:02:49,007 | INFO  | [http-8989-1]: feedId: 4 | feedSwitch: on | feedName: FEED4
|STDOUT| 2010-01-28 00:02:49,007 | INFO  | [http-8989-1]: feedId: 5 | feedSwitch: off | feedName: FEED5
|STDOUT| 2010-01-28 00:02:49,023 | INFO  | [http-8989-1]: feedId: 6 | feedSwitch: on | feedName: FEED6
|STDOUT| 2010-01-28 00:02:49,038 | INFO  | [http-8989-1]: feedId: 7 | feedSwitch: on | feedName: FEED7

so I know I have FEEDs 1-3 which should have the FTQ number 1 in, in my dataTable. On rendering the page, I see this

This is 'dataTable' demo

id name ftqs
|1| |FEED1| ||
|2| |FEED2| ||
|3| |FEED3| ||
|4| |FEED4| ||
|5| |FEED5| ||
|6| |FEED6| ||
|7| |FEED7| || 

I don't like grovelling. I can't bear long posts either, but I'm working in an environment where I've nobody who knows about these technologies and stack overflow is the only and best place I've found to ask these questions. As I'm at my wits end, I hope you don't mind the extra post length.

My question then is what from the above code needs to change to get the TaskListData String[] member FTQar accessible? At this stage, I'll be honest and say I'm hoping someone could post a modified snippet of my code showing me where I've gone wrong. If it were up to me, you'd get extra points than normal for the right answer.

Many thanks indeed Mark


First of all, JSTL and JSF doesn't work seamlessly together in sync as you would expect from the order in the coding. It's more so that JSTL runs first before JSF kicks in. Virtually, JSTL processes the entire page from top to bottom first and then hands the generated output (thus, without any JSTL tags, but with its generated output) over to JSF which in turn processes the entire page from top to bottom again.

As the h:dataTable hasn't generated any rows yet at the moment the JSTL runs, you aren't going to see anything from the c:forEach inside a h:column.

Rather use the JSF-supplied iterating components, such as Tomahawk's t:dataTable, or RichFaces' a4j:repeat, or Facelets' ui:repeat, or maybe even a nested h:dataTable.


I would guess the problem stems from using <c:forEach>. JSTL tags don't always play well with JSF tags as they are not evaluated at the same time. Try <a4j:repeat>


I remember having a similar problem due to using all caps in the name of the getter/field or like your case FTQs, have you tried changing item.FTQs to item.fTQs or even renaming your getter/setter to Ftqs instead and using item.ftqs?

0

精彩评论

暂无评论...
验证码 换一张
取 消