Search code examples
javaseam

Redirection with method in Seam


Let's say I have a button:

<h:commandButton value="Assign task" action="#{taskAssigner.assignTo(user)}"/>

which assigns a new task to a user. I'd like to be able to redirect immediately to the task page, which would be equivalent to clicking on a link like

<s:link value="#{task.description}" action="#{workOnTask.start}">
    <f:param name="taskId" value="#{task.id}" />
</s:link>

assuming that in WEB-INF/pages.xml I have something like

<page view-id="/task-list.xhtml">
    <navigation from-action="#{workOnTask.start}">
        <redirect view-id="/task.xhtml"/>
    </navigation>
</page>

I can't see how I can do that in Java in TaskAssigner.assignTo(). I would need access to the Seam component, right? I need to redirect to /task.xhtml and set param taskId as needed and also run workOnTask.start() which does some initialization on the component.


Solution

  • First, it is not recommended to use the s:link-tag together with the action-attribute (see JBoss Forum).

    For the navigation issue i would replace (or combine) the from-action tag with the if-outcome one and return the value behind if-outcome at the assignTo- and workOnTask.start-methods.

    <page view-id="/task-list.xhtml">
      <navigation>
        <rule if-outcome="navToTask">
          <redirect view-id="/task.xhtml"/>
        </rule>
      </navigation>
    </page>
    

    And at the method:

    public String assignTo(User u)
    {
        [...]
        // in case of success
        return "navToTask";
    }