Spring MVC Tutorial

Spring 4 MVC Form Example Using Eclipse and Maven

Spring Registration form:

<%@page import="org.springframework.validation.ObjectError"%>
<%@ page import="com.candidjava.spring.bean.User"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
.err {
  color: red;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>

<body>

  <h2>Simple spring form handling</h2>
  <form action="register" method="POST" modelAttribute="user">
    Name:<input type="text" name="name" /><br> Gender : Male<input
      type="radio" name="gender" value="male" /> Female<input type="radio"
      name="gender" value="female" /><br> Email : <input type="text"
      name="email" /><br> Languages : English<input type="checkbox"
      name="language" value="english" /> French<input type="checkbox"
      name="language" value="french" /> Tamil
    <input type="checkbox" name="language" value="tamil"  />
    <br> Country : <select name="country">
      <option value="">select country</option>
      <option value="india">INDIA</option>
      <option value="usa">USA</option>
      <option value="china">CHINA</option>
    </select> <br> <input type="submit" value="submit" />
  </form>
</body>
</html>

Spring Controller using @getMapping and @postMapping

package com.candidjava.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import com.candidjava.spring.bean.User;

@Controller
public class UserController {
  
  User users = new User();
  @GetMapping("index")
  public ModelAndView register(User user) {
    return new ModelAndView("register");
  }

  @PostMapping("/register")
  public ModelAndView create(User user) {
    ModelAndView model = new ModelAndView("view");
    // user bean will be automatically binded to view . refer @ModelAttribute
      users.setName(user.getName());
      users.setEmail(user.getEmail());
      users.setGender(user.getGender());
      users.setLanguage(user.getLanguage());
      users.setCountry(user.getCountry());
      users.setPassword(user.getPassword());
    
    return model;
  }

  @GetMapping("/register")
  public ModelAndView viewData(User user) {

    ModelAndView model = new ModelAndView("register");
    return model;
  }

}

Spring View

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>User Info</h2>

<strong>User Name </strong>: ${user.name} <br>
<strong>Gender </strong> : ${user.gender}<br>
<strong>Email </strong> : ${user.email}<br>
<strong>Language </strong> : <c:forEach var="val" items="${user.language}">
 <c:out value="${val}"></c:out>
 </c:forEach><br>
<strong>Country </strong>: ${user.country} <br> 

  
</body>
</html>

Screenshot 1:

Screenshot 2:

Download

Spring 4 form example war

Spring 4 form example maven zip