Adnan
Adnan

Reputation: 4607

How to redisplay form with empty fields?

I'm inserting data from a jsf page to database using managed beans.
But the problem is when my jsf page is loaded existing beans values are displayed in the form fields, but i want to show form fields empty at the time of loading. Please reply. Thanks in advance.

Following are my code snippets Bean class:

public class Employee implements Serializable{
private int eId;
private String name;
public String addEmployee(){
try{
//dbconnection
String query = "insert into emp values(?,?)";
PreparedStatement pstmt = conn.prepareStatement(query); 
pstmt = conn.prepareStatement(query); // create a statement
pstmt.setInt(1,this.eId); 
pstmt.setString(2,this.eName);
return "success-page";
}catch(Exception e){
    return "failure-page";
}
}

JSF page:-

<html>
<body>
<h:form>
Id   
<h:inputText value="#{employee.eId}">
    <f:convertNumber/>      
</h:inputText>      

Name                                 
<h:inputText value="#{employee.eName}">
        <f:validateRegex pattern="[a-zA-Z ]*"/>
</h:inputText>    
</h:form>
</body>
</html>

Upvotes: 1

Views: 684

Answers (1)

BalusC
BalusC

Reputation: 1108692

Put the bean in the request or view scope (and thus not in the session scope). This way the bean will be garbaged and recreated when a new view is requested.

Further you also need to instruct the webbrowser to not cache the page. Some webbrowsers (such as Firefox) will namely also redisplay the old input values when the page is requested from the browser cache. Create a filter which is annotated as @WebFilter(servletNames={"facesServlet"}) (where facesServlet is the <servlet-name> of the FacesServlet as definied in web.xml) and contains basically the following in doFilter() method:

HttpServletResponse hsr = (HttpServletResponse) response;
hsr.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
hsr.setHeader("Pragma", "no-cache"); // HTTP 1.0.
hsr.setDateHeader("Expires", 0); // Proxies.
chain.doFilter(request, response);

Upvotes: 2

Related Questions