26 Jan
Posted by Rachel as Code, Java, Spring MVC
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 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.
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.
<bean class="carbonfive.spring.web.pathparameter.ParameterizedUrlHandlerMapping"> <property name="alwaysUseFullPath" value="false" /> <property name="interceptors"> <list> <ref bean="hibernateInterceptorSessionInView" /> </list> </property> <property name="defaultHandler" ref="homeController"/> <property name="mappings"> <props> <prop key="/index">homeController</prop> <prop key="/entry/create">createEntryController</prop> </props> </property> </bean> <bean id="createEntryController" class="com.tutorial.webapp.EntryCreateController"> <property name="formView" value="entryform" /> </bean>
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(); }
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 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(); } }
Then we need an html file to put the form on.
[#import "/spring.ftl" as spring /]
<html>
<head>
<title>Create Entry</title>
</head>
<body>
<form method="post" id="entry" name="entryForm">
<p>
<button class="button" type="submit" name="submit" id="update">Update</button>
<br/>Entry Title: [@spring.formInput "entry.title"/]
<br/>Entry Text: [@spring.formInput "entry.text"/]
</p>
<br/>
</form>
</body>
</html>You should now be able to run the application and create a blog entry with a title and some text.
In this example we aren’t going to handle any errors – we’ll cover that in another tutorial at a later stage.
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.
We will update our code to be able to handle editing entries.
RSS feed for comments on this post · TrackBack URI
Leave a reply