Results 1 to 4 of 4

Thread: How to clear form fields after submit

  1. #1
    Join Date
    Apr 2009
    Posts
    3

    Default How to clear form fields after submit

    Hi Everyone,

    I have a simple form with some fields on a jsp page. After submitting the page I am showing the same page. But the page is not clearing the field values. I want them to clear after submitt the page.

    Thanks for any help.

  2. #2
    Join Date
    Mar 2009
    Location
    New Jersey
    Posts
    44

    Default

    After submit, if you are trying to get the same page, you need to redirect the the page to get the new form.

    The new from gets populated from the object created in formBackingObject method.

    Could you post your controller code?

  3. #3
    Join Date
    Apr 2009
    Posts
    3

    Default

    Hi sixseasons,

    Thanks for your reply. This is my controller code -

    Code:
    public class AddressController extends SimpleFormController {
    
        private StateDao stateDao;
        private AddressManager addressManager;
    
        private static final String ADDR_ID = "id";
    
        public AddressController(StateDao stateDao,
                AddressManager addressManager) {
            this.stateDao = stateDao;
            this.addressManager = addressManager;
        }
    
        protected Object formBackingObject(HttpServletRequest request) throws Exception {
    
              CustomerAddress command = null;
              command = new CustomerAddressImpl();
              command.clearValues();  // I found this way from the forum. 
    
              return command;
        }
    
        protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command,
            BindException errors) throws Exception {
    
            CustomerAddress custAddr = (CustomerAddress) command;
            custAddr.setUserId(1);
            Address addr = addressManager.getAddress(1);
            addressManager.addAddress(custAddr, addr);
    
            Map model = errors.getModel();
    
            model.put("addresses", getAllAddresses());
            model.put("states", getStatesRefData());
    
            CustomerAddress newAddr = new CustomerAddressImpl();
            model.put(this.getCommandName(), newAddr);
            return new ModelAndView(this.getSuccessView(), model);
        }
    
        private List getStatesRefData() {
            return stateDao.readStatesByCountry("US");
        }
        
        private List getAllAddresses() {
        	return addressManager.getAllAddresses(1);
        }
    }
    In a forum I found a way for doing the same, but it's not working. They have adviced to make a clearValues() method in command object and call it in formBackingObject () method.

    Thanks

  4. #4
    Join Date
    Mar 2009
    Location
    New Jersey
    Posts
    44

    Default

    My suggestions:
    1. use referenceData method for addresses and states.
    2. use redirectview to get the new form in onSubmit method.
    3. In formbackingObject method, you are already creating new object, that's enough. No need to 'clearValues'.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •