struts how do clear the html fields after inserting data.

Posted on 2006-06-02
Last Modified: 2011-09-20
I have a simple struts applications. when I submit the form it inserts into the table and go back to the input screen. I want the html fields to be cleared. They still contain the values that i entered.

I tried using reset() method in the form but no luck.

Thanks in advance.
Question by:muthiahmerchant
    LVL 29

    Expert Comment

    The reset method should work
    LVL 1

    Author Comment

    In my formbean this is what i have

    public void reset(ActionMapping mapping, HttpServletRequest request){
            firstName = "";
            lastName = "";

    in my .jsp page
     <td><html:text property="firstName" styleClass="field"/></td>
     <td><html:text property="lastName" styleClass="field"/></td>

    is there anything else that needs to be done, cause reset is not working for me.

    LVL 1

    Author Comment

    I put a bunch of log messages and was able to figure this much.

    the reset method is called in the begining before the page is loaded, but after I submit the form and mapping.getInputForward() is called it does not call the reset method, the form bean still has the values that it got from the form and hence it does not get cleared.

    my customerformaction has the following

    Customer customer = new Customer();
    cFormBean.setCustomerId(customer.insertCustomer(customerDTO)) ;
    return mapping.getInputForward();

    LVL 3

    Accepted Solution

    If you want the HTML fields to be cleared when the page is rendered again, you must set the fields in your form to null or "" in your Action once you are done using the field values.

    Do this:

    Customer customer = new Customer();
    customer.insertCustomer(customerDTO) ;
    cFormBean.setCustomerId(<null or "");
    return mapping.getInputForward();
    LVL 10

    Expert Comment

    Hi muthiahmerchant,

    If you don't have to keep the values of the input fields in case of validation errors,
    you can simply set the scope of your formbean to request in the action tag in struts-config.xml.

    Or else, follow runa_paathak's solution.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    The viewer will learn how to implement Singleton Design Pattern in Java.
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now