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>

Refer

    Spring 4 maven dependency

    Spring 4 Java based configuration



Screenshot



Download

            Spring 4 form example war

            Spring 4 form example maven zip




Related Post

Comments

Lesaspida
         Purchase Dog Cephalexin Stendra Avana cialis overnight shipping from usa Kamagra User Reviews Staxyn Price Kamagra Vs Silagra
NeooZor
         ?де?? в? може?е заказа?? копи? л?бого ?ай?а под кл??, недо?ого и ка?е??венно, п?и ??ом не ??а?? ?вое в?ем? на ?азли?н?е п?ог?амм? и ??илан?-?е?ви??. ?лони?овани? подлежа? ?ай?? как на кон????к?о?а?, ?ак и на движка?: - Tilda (Тил?да) - Wix (?ик?) - Joomla (?ж?мла) - Wordpress (?о?дп?е??) - Bitrix (?и??ик?) и ?.д. ?еле?он 8-996-725-20-75 звони?е пи?и?е viber watsapp ?опи?????? не ?ол?ко одно???ани?н?е ?ай?? на подобии Landing Page, но и много???ани?н?е. Создае??? полна? копи? ?ай?а и на???аива???? ?о?м? дл? о?п?авки за?вок и ?ооб?ений. ??оме ?ого, подкл??ае??? админка (админ панел?), позвол???а? ?едак?и?ова?? код ?ай?а, измен??? ?ек??, заг??жа?? изоб?ажени? и док?мен??. ?де?? в? пол??и?е ве?? комплек? ??л?г по копи?овани?, ?аз?або?ке и п?одвижени? ?ай?а в Яндек?е и Google. Хо?и?е ?зна?? ?кол?ко ??ои? ?дела?? копи? ?ай?а? напи?и?е нам 8-996-725-20-75 звони?е пи?и?е viber watsapp

©candidjava.com