To use Spring Form Error Tag, you have to follow following steps.
Your JSP Page should look like this:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>About Us Page</title>
</head>
<body>
<sf:form method="POST" action ="/SpringBasics/redirect.aspx" modelAttribute="basicClass">
FirstName: <sf:input path="firstName" />
<sf:errors path="firstName" />
<br/>
<input type="submit" value="Redirect Page" />
</sf:form>
</body>
</html>
Your JSP Page should look like this:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>About Us Page</title>
</head>
<body>
<sf:form method="POST" action ="/SpringBasics/redirect.aspx" modelAttribute="basicClass">
FirstName: <sf:input path="firstName" />
<sf:errors path="firstName" />
<br/>
<input type="submit" value="Redirect Page" />
</sf:form>
</body>
</html>
Your Controller File Should be like this
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Controllers;
import Beans.basicClass;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
/**
*
* @author Zeeshan
*/
@Controller
@RequestMapping({"/", "/*.htm", "/homepage"})
public class HomeController {
@RequestMapping(value = "/", method = GET)
public String aboutUs(basicClass obj) {
System.out.println("HomeController >>>>>>>>>>>>>>>> aboutUs");
return "aboutus";
}
@RequestMapping(value = "redirect", method = POST)
public String redirect(@Valid basicClass obj, BindingResult result, Model m) {
if (result.hasErrors()) {
return "aboutus";
}
System.out.println("HomeController >>>>>>>>>>>>>>>> redirect");
m.addAttribute("firstName", "zeeshan");
return "index";
}
}
Please note that @Valid tag will validate inputs on forms
Your Bean Class should look like this
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Beans;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.context.annotation.Bean;
/**
*
* @author zeeshan
*/
public class basicClass {
/*
@NotNull
*/
@NotEmpty
@Size(min = 2, max = 5)
private String firstName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
No comments:
Post a Comment