1

I don't know if this is a good practice but I would like to call different actions on managed beans from the same commandButton depending on the user context. The reason is that I access a page from different locations in the application, and I want my single command be a sort of "Back" button to the locations I came from. I've implemented a custom mechanism based on the idea of MyFaces Orchestra Flow with redirect in a custom NavigationHandler but I wonder if there is a way to parameterized the action attribute of my commandButton.

Any suggestions welcome

mremond
  • 36
  • 7
  • Are you using JSF 2.x or 1.2? You mixed both tags on the question which is quite confusing. – BalusC Jan 24 '13 at 16:57
  • Currently JSF 2.x ; I thought maybe it exists a standard mechanism in the two versions. – mremond Jan 24 '13 at 17:01
  • You could make two buttons and set the **rendered** property showing the button you need or not. – Diogo Moreira Jan 24 '13 at 17:02
  • Yes, this was my first solution but I was looking for something less intrusive by reusing existing facelets, ManagedBean and navigation rules. – mremond Jan 24 '13 at 17:10

1 Answers1

1

If your target environment supports Servlet 3.0 / EL 2.2 (Tomcat 7, Glassfish 3, etc), or has JBoss EL installed, then you can just pass the deisred argument directly into the method. So you can just have

public void submit(String argument) {
    // ...
}

and

<h:commandButton value="submit" action="#{bean.submit('some')}" />

or, based on an EL scoped variable:

<h:commandButton value="submit" action="#{bean.submit(some)}" />

There are other ways though. Think of <f:param>, <f:setPropertyActionListener>, Application#evaluateExpressionGet(), etc. Many of them are answered in context of <h:dataTable> here: How can I pass selected row to commandLink inside dataTable? (the principle is not that different when outside data table)

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you very much for the response but these requirements are not satisfied for me. However I'm not sure this will work because what I want is really : "#{bean1.method1}" or "#{bean2.method2}" and take advantage of the navigation rules associated with these action. – mremond Jan 24 '13 at 17:07
  • 1
    Just let the method delegate to the desired method and return the desired outcome? E.g. `if ("foo".equals(argument)) { return bean1.method1(); } else { return bean2.method2(); }`. – BalusC Jan 24 '13 at 17:17