In the second part of this tutorial, we got to the point of being able to load entities from the database and display them on a page.

For this tutorial, we will handle the creation of a new Entry object with a form, handle the submission of the form and redirect after post back to the list of entries that we created in part 2 of this tutorial.

The goal

The user must be able to navigate to a form where they can enter a title and some text for a new blog entry. The user must be able to save this entry by submitting a form and should then be redirected to a list of all existing blog entries. If the user hits refresh on the list of blog entries, the most recent entry should not be duplicated.

Changes to configuration files

Firstly, we’re going to need to map a url for this new action. To accomplish this we need to change the ParameterizedUrlHandlerMapping section of the spring-servlet.xml file.

spring-servlet.xml (ParameterizedUrlHandlerMapping bean and new controller)


      
      
         
            
         
      
      
      
         
            homeController
            createEntryController
         
      
   
 
   
            
   

Controller for the create page and changes to repository

EntryRepository

To be able to save our entry, we need to add a save method to our EntryRepository.

@Transactional(readOnly = false)
public void store(Entry entry) {
   Session session = sessionFactory.getCurrentSession();
   session.save(entry);
   session.flush();
}

EntryCreateController

For our form controller, we can extend an existing Spring Controller class called the SimpleFormController. This class makes it simple to specify a form view, a success view and your command object.

Lets discuss what you will see in the controller class.

  • @Controller – this identifies the class as a Spring controller.
  • @Autowired – this allows Spring to populate our entryRepository.
  • @RequestMapping – in this usage, we are mapping http method requests onto a specific method.
  • @ModelAttribute – in this case is our command (form) object that we ask Spring to bind for us
@Controller
public class EntryControllerCreate extends SimpleFormController {

   @Autowired private EntryRepository entryRepository;

   @RequestMapping(method = RequestMethod.GET)
   public String showForm(   HttpServletRequest request,
                     HttpServletResponse response,
                     ModelMap model) {

      model.put("entry", new Entry());
      return getFormView();
   }

   @RequestMapping(method = RequestMethod.POST)
   public String processSubmit(   @ModelAttribute("entry")Entry entry,
                           HttpServletRequest request,
                           HttpServletResponse response,
                           SessionStatus status,
                           ModelMap model){
      entryRepository.store(entry);
      return "redirect:"+getSuccessView();
   }
}

Template

Then we need an html file to put the form on.

entryform.html

[#import "/spring.ftl" as spring /]      


  Create Entry



   


Entry Title: [@spring.formInput "entry.title"/]
Entry Text: [@spring.formInput "entry.text"/]


Running the application

You should now be able to run the application and create a blog entry with a title and some text.

How do we handle errors?

In this example we aren’t going to handle any errors – we’ll cover that in another tutorial at a later stage.

Why is redirect after post good practise?

While this is best practise it is not a hard and fast rule. The main reason behind it is that if you don’t redirect, the user can refresh the page and the form will be resubmitted. In some cases this can be the desired behaviour (checking availability of tickets) and in some cases it is really undesired (resubmitting payment information).

In each case it’s up to you to decide what will be best for your users.

Coming up in Part 4

We will update our code to be able to handle editing entries.