Chapter 4. Working with Spring Tag Libraries

You learned that one of the biggest advantages of using Spring MVC is its ability to separate View technologies from the rest of the MVC framework. Spring MVC supports various View technologies such as JSP/JSTL, Thymeleaf, Tiles, FreeMarker, Velocity, and more. In previous chapters, you saw some basic examples of how to use InternalResourceViewResolver to implement JSP/JSTL views. Spring MVC provides first-class support for JSP/JSTL views with the help of Spring tag libraries. In this chapter, you are going to learn more about the various tags that are available as part of Spring tag libraries.

After finishing this chapter, you will have a good idea about the following topics:

  • JavaServer Pages Standard Tag Library (JSTL)
  • Serving and processing web forms
  • Form-binding and whitelisting
  • Spring tag libraries

The JavaServer Pages Standard Tag Library

JavaServer Pages (JSP) is a technology that lets you embed Java code inside HTML pages. This code can be inserted by means of <% %> blocks or by means of JSTL tags. To insert Java code into JSP, JSTL tags are generally preferred, since tags adapt better to their own tag representation of HTML, making JSP pages look more readable.

Tip

JSP even lets you define your own tags; you must write the code that actually implements the logic of your own tags in Java.

JSTL is just a standard tag library provided by Oracle. We can add a reference to the JSTL tag library in our JSP pages as follows:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 

Similarly, Spring MVC also provides its own tag library to develop Spring JSP views easily and effectively. These tags provide a lot of useful common functionality such as form binding, evaluating errors, and outputting messages, and more when we work with Spring MVC.

In order to use these, Spring MVC has provided tags in our JSP pages. We must add a reference to that tag library in our JSP pages as follows:

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

These taglib directives declare that our JSP page uses a set of custom tags related to Spring and identify the location of the library. They also provide a means to identify custom tags in our JSP page. In the taglib directive, the uri attribute value resolves to a location that the servlet container understands and the prefix attribute announces which bits of markup are custom actions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.23.12