开发者

empty ui:repeat, is the component created?

开发者 https://www.devze.com 2023-04-07 22:48 出处:网络
I am trying to debug an issue with the following code: <h:panelGroup id=\"items\"> <ui:repeat value=\"#{itemController.items}\" var=\"item\">

I am trying to debug an issue with the following code:

<h:panelGroup id="items">
<ui:repeat value="#{itemController.items}" var="item">
    <h:form>
        <h:inputText id="title" value="#{item.fields['Title']}"/>
        <a4j:commandButton action="#{dao.storeItem(item)}" value="Save" render="@form"/>
   开发者_JAVA技巧 </h:form>
</ui:repeat>
</h:panelGroup>

The above works if a collection is displayed in the view directly. However, if the ui:repeat starts empty, and items are added through an AJAX request, and the ui:repeat rerendered, the forms break. Specifically the model is not updated, nor actions triggered. I want to understand why.

Right now my guess is that if the ui:repeat starts empty, the form component is not created at all. Can anyone verify this, or provide the correct explanation?

ADDITIONAL INFO

Here are relevant parts of the controller, I have also tried ViewScoped, and long-running conversations:

@Named
@ConversationScoped
public class ItemController implements Serializable
{   
    private static final long serialVersionUID = 1L;

    @Inject
    private HibernateDAO dao;    

    public List<Item> getItems()
    {
        return dao.getItems();
    }

    public void uploadListener(final FileUploadEvent event)
    {
        final UploadedFile item = event.getUploadedFile();

        final FacesContext context = FacesContext.getCurrentInstance();
        final Application application = context.getApplication();
        final String messageBundleName = application.getMessageBundle();
        final Locale locale = context.getViewRoot().getLocale();
        final ResourceBundle resourceBundle = ResourceBundle.getBundle(messageBundleName, locale);

        final String msg = resourceBundle.getString("upload.failed");
        final String detailMsgPattern = resourceBundle.getString("upload.failed_detail");

        try
        {
            CSVImporter.doImport(item.getInputStream(), dao, item.getName());
        }
        catch (ParseException e)
        {
            final Object[] params = {item.getName(), e.getMessage()};
            final String detailMsg = MessageFormat.format(detailMsgPattern, params);
            final FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, detailMsg);

            context.addMessage("uploadForm:uploader", facesMsg);
        }
        catch (TokenMgrError e)
        {
            final Object[] params = {item.getName(), e.getMessage()};
            final String detailMsg = MessageFormat.format(detailMsgPattern, params);
            final FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, detailMsg);

            context.addMessage("uploadForm:uploader", facesMsg);
        }
    }
}

The dao simple fetches the items from a database. Here is the relevant fileupload code:

<h:form id="uploadForm" enctype="multipart/form-data">          
    <h:message id="message" showDetail="true" for="uploader" errorClass="error" warnClass="warning" infoClass="info" fatalClass="fatal"/>
    <rich:fileUpload id="uploader"
        fileUploadListener="#{itemController.uploadListener}"
        maxFilesQuantity="1"
        acceptedTypes="csv"
        render="items message" />            
</h:form>


Okay posting it here because it will be longer than comments .

It works for me which is probably not what you wanted to hear :( but I had to teak few minor things . Firstly in controller add

public void storeItems(Item item)
{
  dao.storeItems();
}

then change this

 <a4j:commandButton action="#{dao.storeItem(item)}" value="Save" render="@form"/>

to

<a4j:commandButton action="#{itemController.storeItem(item)}" value="Save" render="@form"/>

That however is probably not the real issue and I think that is around here

CSVImporter.doImport(item.getInputStream(), dao, item.getName());

basically I am expecting that the method above would have uploaded data from where dao.getItems(); can fetch it. So put a breakpoint at public List<Item> getItems() and once file has been upload and render="items message" renders the items panel group again it should will hit this method and at that time see if dao.storeItems() is bringing any data back which it should. Reply back then and we will take it from there.


Update below to avoid running dao fetch twice.

You can not avoid two calls to your get thats part of JSF lifeCycle and is normal.
How ever you can avoid hitting the database twice as you should too but refactoring your code along the lines of

 private List<Item> items;

    public List<Item> getItems()
    {
        return items;
    }

    @PostConstruct
    public void init()
    {
        this.items = dao.getItems();
    }


    public void uploadListener(FileUploadEvent  event) throws Exception{
        ......
        CSVImporter.doImport(item.getInputStream(), dao, item.getName());
        this.items = dao.getItems();

        .....
    }
0

精彩评论

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

关注公众号