0

I am new to jsf and doing the following: For example, i ask user to fill a form, and then submit it. After submitting, i want to show whether submission is successful. To do this i create another html page and after submission, if it is successful i redirect page to this new successful html page. So, i have a lot of web pages in my project. My question is, is that a bad thing? Are there any other solutions that i can try instead of creating many "successful" pages? Here is how i do it:

<h:form>
        <h:panelGrid columns="2">
            <h:outputText value="Select one:"></h:outputText>
            <rich:select  defaultLabel="Select..."  value="#{account.accountCurrency}" >
                <f:selectItem itemValue="a" itemLabel="a" />
                <f:selectItem itemValue="b" itemLabel="b" />
                <f:selectItem itemValue="c" itemLabel="c" />
            </rich:select>

            <h:commandButton value="Submit" action="#{selection.approve}" ></h:commandButton>
        </h:panelGrid>           
    </h:form>

And in my managed bean i do following:

 public void approve() {
   // check whether it is successful, then redirect to successful page
}

Thanks

yrazlik
  • 10,411
  • 33
  • 99
  • 165

2 Answers2

1

Use messages component to display a message back.

<h:commandButton value="Submit" actionListener="#{somebean.someMethod}">
    <f:ajax  render="msg"/>
</h:commandButton>
<h:messages id="msg"/>

Java code:

String msg = "Successfully submitted!";
FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, null);
FacesContext.getCurrentInstance().addMessage(null, fm);
fareed
  • 3,034
  • 6
  • 37
  • 65
  • The OP is using `h:commandButton` with non-ajax behaviour. In this case, the next page should have the message available to render it, but it'll be lost since the requests are different. He'll probably need to use [flash scope](http://stackoverflow.com/a/12485381/1199132) for that. – Aritz Jul 15 '13 at 08:04
  • @XtremeBiker The OP has asked for an alternative to having multiple pages. This is an alternative for having multiple pages by display the message back in the same page (thus, ajax). So in this case, there "won't be a next page". – fareed Jul 15 '13 at 09:30
  • Basically, we're reading the question in different ways. I understand that he talks about different files (basically because he talks about creating pages in his project) and as far as I can see you talk about multiple urls. With my interpretation you can easily solve the problem having only one success page (with its url or navigation result) and passing the success message using the flash scope. – Aritz Jul 15 '13 at 09:39
0

You can use composite components and templates to help reduce the amount of code required.

John Ament
  • 11,595
  • 1
  • 36
  • 45