Passing a parameter using h: commandButton

I have a4j: commandButton, which should redirect me to the corresponding "Edit" page based on the identifier that I wanted to pass as a parameter, something like this:

<h:commandButton action="/details.jsf?faces-redirect=true" value="details"> <f:attribute name="id" value="#{bean.id}" /> </h:commandButton> 

The problem is that it does not work. I also tried replacing the f: attribute with "f: param name =" id "value =" # {bean.id} "", but it also failed. The only thing I got is outputLink:

 <h:outputLink value="/details.jsf"> link <f:param name="id" value="#{bean.id}" /> </h:outputLink> 

But I'm not very happy with the link, so is there a way to make commandButton work?

Oh, and I also have a bean that should get this "id" after the redirect:

 @PostConstruct public void init(){ id= resolve("id"); } 
+4
source share
2 answers

Check out this linking article in JSF, BalusC

f:param only works with h:commandLink and h:outputLink .

You can use hidden input:

 <h:form> <h:commandButton action="/details.jsf?faces-redirect=true" value="details"/> <input type="hidden" name="id" value="#{bean.id}" /> </h:form> 

And then in your faces-config, I think this is the request area. If you use JSF2 annotations, just translate that into the appropriate annotations.

 <managed-bean> <managed-bean-name>bean</managed-bean-name> <managed-bean-class>mypackage.Bean</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <managed-property> <property-name>id</property-name> <value>#{param.id}</value> </managed-property> </managed-bean> 

You need to explicitly have getters and setters for this field in the backup bean.

or try to draw a button link through CSS.

+6
source

If my JSF is extremely rusty, the action attribute on the command button or command line is used to indicate the result line defined in the faces-config-nav file, or should point to a method on the bean that will return the result (or redirect / whatever).

In your case, if you want to redirect to another page ... you should define this in your configuration file as a navigation link ( redirect if necessary). Then in your action button you should have something like

 <h:commandButton action="showDetails" value="details"> 

...

 <navigation-case> <from-outcome>showDetails</from-outcome> <to-view-id>/details.jsf?faces-redirect=true</to-view-id> </navigation-case> 

As an aside, the <f:atribute> will work, but it will only set the attribute on the component. Therefore, if you hold the command line button in a bean, you can get the attribute value by name. To pass a query parameter, use a hidden field technique like pakore

+1
source

Source: https://habr.com/ru/post/1316154/


All Articles