0

How to create a <h:button> from Java? I can create a <h:commandButton> like this:

HtmlCommandButton button = new HtmlCommandButton();

But I can't find the Java class for the <h:button>. For example I need to create this tag from Java:

<h:button outcome="test.xhtml" />

How can I achieve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Uta Alexandru
  • 324
  • 4
  • 11
  • AFAIK you don't create markup from Java, but you can conditionally show / hide buttons, using ``. – Robert Jan 06 '18 at 19:07
  • this wont help me.. It is hard to believe there no way to create it dynamically. I have something like faces messages , like RequestContext.getCurrentInstance().showMessageInDialog(message) .And now i want to change the close dialog button with a redirect button.For now i am using the HtmlCommandButton with a string expresion to achieve that but it will not change the browser link if do not use faces-redirect=true .If i use that then will create a HttpSession which i do not need at that moment. – Uta Alexandru Jan 06 '18 at 19:16
  • It **will** help you. In 99% of the cases, doing things in xhtml instead of Java code works, is easier etc... – Kukeltje Jan 07 '18 at 07:55
  • 1
    You can safely say 100%, @Kukeltje. – BalusC Jan 07 '18 at 08:16
  • 1
    Still was not able to achieve https://stackoverflow.com/questions/29156426/subscribing-to-postvalidationevent-of-dynamicaly-created-child-component in pure xhtml. But it is almost like developing full components and that is a different usecase @Balusc. Still thinking of hiring you for a day for some consultancy in this area – Kukeltje Jan 07 '18 at 08:24
  • @Kukeltje: You know how to reach me. – BalusC Jan 08 '18 at 18:11

1 Answers1

2

Just peek around in javax.faces.component.html package for all of them.

The <h:button> is represented by the HtmlOutcomeTargetButton class.

HtmlOutcomeTargetButton button = new HtmlOutcomeTargetButton();
button.setOutcome("test.xhtml");

Said that, using XHTML to define the component tree will end up in much better maintainable code. See also How to create dynamic JSF form fields and JSTL in JSF2 Facelets... makes sense?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you.It is exactly what i need.I know it is better to create it in xhtml directly but for something like faces messages i prefer to do it in java methods.It is easier to maintain this part from java in my opinion. – Uta Alexandru Jan 07 '18 at 08:51
  • @UtaAlexandru: for FacesMessages you just create the content, not the component! That **is** common practice. – Kukeltje Jan 07 '18 at 09:40
  • yes ... but i don't like how it looks. (especially that close button which i could not customize it).I preferred to create a small component instead :D – Uta Alexandru Jan 07 '18 at 09:45
  • @BalusC: is it on purpose that you create the component via 'new' here and in https://stackoverflow.com/questions/3510614/how-to-create-dynamic-jsf-form-fields instead of `UIComponent implementation = application.createComponent(...);` – Kukeltje Jan 07 '18 at 12:10
  • @Kukeltje: simplicity https://stackoverflow.com/q/16610655. When developing for a component library, of course use `createComponent()` but generally the component library developer should already know that :) – BalusC Jan 08 '18 at 18:06