<jsp:forward page="contacts.html"></jsp:forward>
Code language: HTML, XML (xml)
The above code will just redirect the user to contacts.html page. <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring 3 MVC Series - Contact Manager</title>
</head>
<body>
<h2>Contact Manager</h2>
<form:form method="post" action="addContact.html">
<table>
<tr>
<td><form:label path="firstname">First Name</form:label></td>
<td><form:input path="firstname" /></td>
</tr>
<tr>
<td><form:label path="lastname">Last Name</form:label></td>
<td><form:input path="lastname" /></td>
</tr>
<tr>
<td><form:label path="lastname">Email</form:label></td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td><form:label path="lastname">Telephone</form:label></td>
<td><form:input path="telephone" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Add Contact"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
Code language: HTML, XML (xml)
Here in above JSP, we have displayed a form. Note that the form is getting submitted to addContact.html page. Contact.java
which is nothing but the form to display/retrieve data from screen and second the ContactController.java
which is the spring controller class. package net.viralpatel.spring3.form;
public class Contact {
private String firstname;
private String lastname;
private String email;
private String telephone;
//.. getter and setter for all above fields.
}
Code language: Java (java)
The above file is the contact form which holds the data from screen. Note that I haven’t showed the getter and setter methods. You can generate these methods by pressiong Alt + Shift + S, R. File: net.viralpatel.spring3.controller.ContactController package net.viralpatel.spring3.controller;
import net.viralpatel.spring3.form.Contact;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
@Controller
@SessionAttributes
public class ContactController {
@RequestMapping(value = "/addContact", method = RequestMethod.POST)
public String addContact(@ModelAttribute("contact")
Contact contact, BindingResult result) {
System.out.println("First Name:" + contact.getFirstname() +
"Last Name:" + contact.getLastname());
return "redirect:contacts.html";
}
@RequestMapping("/contacts")
public ModelAndView showContacts() {
return new ModelAndView("contact", "command", new Contact());
}
}
Code language: Java (java)
In above controller class, note that we have created two methods with Request Mapping /contacts and /addContact. The method showContacts()
will be called when user request for a url contacts.html. This method will render a model with name “contact”. Note that in the ModelAndView
object we have passed a blank Contact object with name “command”. The spring framework expects an object with name command if you are using <form:form>
in your JSP file. Also note that in method addContact()
we have annotated this method with RequestMapping
and passed an attribute method=”RequestMethod.POST”. Thus the method will be called only when user generates a POST method request to the url /addContact.html. We have annotated the argument Contact with annotation @ModelAttribute
. This will binds the data from request to the object Contact. In this method we just have printed values of Firstname and Lastname and redirected the view to cotnacts.html. Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Thanks for that spring3.0 applications. Can u provide the spring3.0 Group authorization example
thanks, can you provide the same thing with internationalization?
Very nice tutorials, etc.
I also think it'd be nice if you show a "List View" of the contacts. After you submit the new contact form, you are taken back to the list of contacts, with a status message of "New contact added".
No need to use tomcat. Spring providing the one of the tool is like SpringSourceTool. We can download this tool use it. Its very easy.
Can you please explain why do we need to use the annotation @SessionAttributes here?
the @SessionAttributes typically list the names of contact attributes which should be transparently stored in the session or some conversational storage, serving as form-backing beans.
This tutorials have made it very easy for me to learn spring 3.They are really good.....I haven't found better ones on line.Thank you!!!
Thank you.
so useful tutorial.
It's little bit different to develop with different IDE but in deed it's Okay.
Thank you.
Great job !!!
Thanks man...! Awesome help you did
This is one of the great series of Spring MVC 3 tutorials. Thanks for putting it up.