开发者

JSF app, actions don't work if a different action has been invoked previously

开发者 https://www.devze.com 2023-03-15 14:33 出处:网络
I\'m learning Java EE 6 and JSF 2.0 on JBoss6 and have built a very simple 1 page \"Todo\" app which works but with a very strange bug.Tested in Safari 5.0.5 and Firefox 5.

I'm learning Java EE 6 and JSF 2.0 on JBoss6 and have built a very simple 1 page "Todo" app which works but with a very strange bug. Tested in Safari 5.0.5 and Firefox 5.

There are two actions you can do (add a todo and check/uncheck todos). It all works, but not the first time an action is done after a different action has been done.

And example usage might look like this:

  1. try to add a todo = success
  2. try to add a todo = success
  3. try to check a todo = fail
  4. try to check a todo = success
  5. try to add a todo = fail
  6. try to add a todo = success
  7. try to check a todo = fail

The app has the following basic files (plus other bits and pieces):

  • entities/Todo.java <- JPA entity
  • managers/TodoManager.java <- EJB for handing the Todo entities, @Stateless
  • controllers/TodoController.java <- managed bean for the开发者_如何学运维 page, @SessionScoped
  • todos.xhtml <- the JSF page

No faces-config.xml

The form to add a todo looks like this:

<h:panelGroup id="projects">
   <h:message for="newtitle" />
   <h:form id="newtodo">
    <h:panelGrid columns="5">
            <h:outputText value="New Todo: "/>
            <h:inputText id="newtitle" value="#{todoController.todo.title}" />
            <h:outputText value="Due: "/>
            <h:inputText id="newDueDate" value="#{todoController.todo.dueDate}">
                <f:convertDateTime pattern="dd/mm/yyyy"/>
            </h:inputText>
            <h:commandButton action="#{todoController.addTodo}" value="add">
                <f:ajax execute="@form" render=":projects"/>
            </h:commandButton>

the form to check/uncheck the todo "done" status looks like this:

<h:form>
    <h:dataTable id="todolist" var="t" value="#{todoController.todolist}">
        <h:column>
            <h:selectBooleanCheckbox id="rowCheckbox" value="#{t.done}" >
                <f:ajax event="click" listener="#{todoController.updateDone(t)}" render=":projects"/>
            </h:selectBooleanCheckbox>
        </h:column>

TodoController looks like this:

@ManagedBean(name="todoController")
@SessionScoped
public class TodoController 
{
    @EJB
    private TodoManager todoManager;
    private Todo todo = new Todo();
    private ArrayList<Todo> todolist = new ArrayList<Todo>();

    public String addTodo()
    {
        todo.setDone(false);
        todo.setUser(this.getLoggedInUser());
        todoManager.addTodo(todo);
        todo = null;
        return "todos.xhtml";
    }

    public String updateDone(Todo t)
    {
        t.setDone(!t.getDone());
        todoManager.updateTodo(t);
        return "todos.xhtml";
    }

I did add logging messages to addTodo() and updateDone(Todo t) to verify when they get called. When the actions are "not working" they in fact don't seem to get called at all. :-(


This will happen when you have 2 forms of which second form is re-rendered by sumbitting the first form. This way the JSF view state of the second form will be completely lost (you can determine it yourself by the absence of a hidden input field with the name javax.faces.ViewState in the Ajax response). Submitting the second form after being re-rendered by a submit of the first form would then visually have no effect. Without the view state JSF won't process the submit of the form. Only a new form (with the proper view state!) will come back in place and hence the second submit works.

You need to finetune the render attribute that way that only the contents of the second form is re-rendered and not the whole form itself.

E.g.

<h:form id="form1">
    <h:panelGroup id="content">
        ...
        <h:commandButton value="Submit">
            <f:ajax execute="@form" render="@form :form2:content" />
        </h:commandButton>
    </h:panelGroup>
</h:form>
<h:form id="form2">
    <h:panelGroup id="content">
        ...
        <h:commandButton value="Submit">
            <f:ajax execute="@form" render="@form :form1:content" />
        </h:commandButton>
    </h:panelGroup>
</h:form>

In your particular case, give the <h:panelGrid> of the first form and the <h:dataTable> of the second form a fixed id and re-render just that from the other form on.

0

精彩评论

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