10. Spring - Spring form /Hibernate validation.


 

Spring form /Hibernate validation.


Convert bootstrap from to spring form.

Add spring form tags library in the jsp file. Prefix can be anything as we prefer (In here it is sf).

<%@taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>

 

Bootsrap from

Spring form

<form>

 

<sf:from>

<inputtype="text"name="name"/>

 

Attribute name.

private String name;

 

<sf:input type="text" path="name"/>

 

Name should be change as path.Path is map with object

 

<select path="categoryId" name="categoryId">

      <option>catOne</option>

</select>

<sf:select path="categoryId"items="${categories}" itemLabel="name" itemValue="id"/>

 

This itemValue="id" is liked with path="categoryId" .

 

items="${categories}" – this is link with back end. In here we use an annotation called @ModelAttribute.

 

@ModelAttribute – return expected data (list of categories) for all request mapping. Controller එක call වෙන හැම වෙලාවකදීම මෙම list of categories ටික available වෙනවා.

 

@ModelAttribute("categories")

public List<Category> modelCategories() {

    returncategoryDAO.list();

}

 

 

Backend එකේදී new product object එකක් හදලා return කරනවා.එක තමා form එකත් එක්ක set කරන්නේ.Modify කිරීමක් නම් කරන්නේ, Backend එකෙන් එවන එම modify කරන්න ඕනේ object එක from එකට set කරනවා.

Backend එකේදී ලෙස object එක set කලානම් එය form එකට modelAttribute="product" මගින් map කරගනු ලබන අතර path property එක හරහා එම object එකේ values access කරනු ලබනවා.

@ModelAttribute("product") ලෙස form එකේ දී ඇති එක(From එකෙන් modify වන product object එක) , backend එකේදී  modelAttribute="product" එකේ ලෙස access කරනු ලබයි .

Object එකේ තියන අපිට display වෙන්න ඕන data view කරලා අනෙත් ඒවා hidden ලෙස තියන්න ඕනේ.නැත්තම් submit කලට පස්සේ, ඒවගේ actual values ගන්න බෑ.ඒ වෙනුවට default values තමා ගන්නේ.ඒ නිසා display වෙන්න ඕනේ නැති ඒවා hidden කරලා තියන්න වෙනවා.

<sf:form class="form-horizontal" modelAttribute="product" action="${contextRoot}/manage/product" method="POST" enctype="multipart/form-data">

  

Server side from valuator.

Using spring valuator this can be done. We need bellow dependencies for that.


<!-- Hibernate Validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>

Note:

සමහර අවස්ථාවල  band internet connection නිසා dependencies properly download වෙන්නේ නෑ .මෙවිට m2 directory එකට ගිහින්  dependencies delete kraal project එකට maven update එකක් කරන්න ඕනේ.


විවිද  validations කරගැනීම සදහා  hibernate validation වලදී entity class එක තුල විවිද annotations බාවිතා කරයි .පහත image එක  බලන්න.




Form එක submit කරාට පස්සේ backend එකේදී spring වලට මෙම object එක validate කරන්න one කියලා කියන්න @Valid බාවිතා කරනවා.මෙහිදී @ModelAttribute("product") ලෙස ගන්නේ form modelAttribute="product" එකේ ලෙස දී ඇති එකයි (From එකෙන් modify වන product object එක) .

    @NotBlank(message = "Please enter the product name!")

ලෙස hibernate validation එක දෙනු ලබයි.

මෙහිදී name field එක empty ලෙස තබා from එක submit කල පසු controller එකේදී @Valid හරහා entity class එකෙන් validation එක බලන අතර validation fail නම් පහත පරිදි එම page එකටම redirect කරනු ලබයි .

if(results.hasErrors()) {
return"page"; // load again to form jsp page
}

එහිදී,

<sf:errors path="name" cssClass="help-block" element="em"/>

හරහා එම error message එක display කරගත හැක.

@Entity
public class Product implements Serializable {
private static final long serialVersionUID = 1L;

@NotBlank(message = "Please enter the product name!")
private String name;
}


<%@taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
<sf:form modelAttribute="product" action="${contextRoot}/manage/product" method="POST">
<sf:input type="text" path="name"/>
<sf:errors path="name" cssClass="help-block" element="em"/>
</sf:form>


@RequestMapping(value = "/product", method=RequestMethod.POST)
public String managePostProduct(@Valid @ModelAttribute("product") Product mProduct,
BindingResult results, Model model) {

if(results.hasErrors()) {
return"page"; // load again to form jsp page
}
return "redirect:/manage/product?success=product";
}

මෙම image එකෙන් form එකට අදාළ data flow එක වෙන ආකාරය පැහැදිලි කර ඇත.





Comments

Popular posts from this blog

09.Data Binding.

Database - Topics

02. Spring – Creating spring project clone it with GIT step by step.