0

I am facing a proplem with the ?faces-redirect=true I want to show the follwoing link http://localhost:8080/FirstJSPApplication/home.xhtml in the url after clicking the submit button in the index.xhtml but when adding ?faces-redirect=true to action="home" I am losing the value of the input field and am just getting Welcome back in the home.xhtml without the value of the username input field.

How can I show home.xhtml in the url after clicking the submit button without losing the username?

MainBean

package com.firstApp;


import javax.faces.bean.ManagedBean;

@ManagedBean
public class MainBean {
    private String username;

    public MainBean() {

    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

index.xhtml

<!DOCTYPE html>
<html xmlns="htpp://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">

<h:head>
    <title>Facelt Title</title>
</h:head>

<h:body>
 <h:form>
   <h:panelGrid columns="3">
     <h:outputLabel value="username"  for="username" />
     <!-- value="#{mainBean.username}" -->
     <h:inputText  id="username"  value="#{mainBean.username}" required="true" requiredMessage="#{msg['msg.username.validation']}" />
     <h:message for="username" />  

     <f:facet name="footer">
     <!-- '?faces-redirect=true"' add home.xhtml to the url-->
      <h:commandButton value="Submit" action="home?faces-redirect=true" />
     </f:facet>
   </h:panelGrid>

 </h:form>

</h:body>
</html>

home.xhtml

<!DOCTYPE html>
<html xmlns="htpp://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">

<h:head>
    <title>Home pages</title>
</h:head>

<h:body>
<!--  #{mainBean.username}  -->

 <h:outputLabel value="Welcome back #{mainBean.username}"  style="font-size: 25px;"/>

 <footer style="margin-top: 300px; border: 1px solid red;">
 <h:outputLabel value="#{msg['page.footer.copyright']}"/>
 </footer>

</h:body>
</html>
TheBook
  • 1,698
  • 1
  • 16
  • 32

1 Answers1

0

Ok the problem is solved if I add the following notation @SessionScoped to the MainBean class

@ManagedBean
@SessionScoped
public class MainBean {

}
TheBook
  • 1,698
  • 1
  • 16
  • 32