Tag Library

  • There are 2 tag libraries in Spring
    • General Tag Library
      • Helps us in creating view and integrating it in our Spring Framework
      • Using Common Spring MVC Tags to build url's and other components
    • Form Components Tag Library
      • Form Component tags support data binding
      • Relationship between form taglib and data binding.
  • Url Tag
    • Helps us create different hyperlinks that allow us to navigate through our application.
    • Binds Context root to the url.
    • To check your "Context Root" in "STS" go to project properties and go to "Web project settings".
    • To add this tag to your jsp page add following taglib directive.
      •  <%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>  
        
    • The url tag is as follows.
      •  <spring:url value="/resource/save" htmlEscape="true"/>  
        
    • We can add other parameters as needed to this tag such as htmlEscape="true" to escape dynamic content etc.
    • We can also change the context using context property.
      • Default Context is "ContextRoot" property.
  • Form Tag
    • The Spring Form Tag was created to build  form elements in jsp pages that could automatically handle bound data.
    • To add a jsp form tag we must include Spring Form Tag library on our jsp page as follows.
      •  <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>  
    • The form tag is added in place of HTML form tag.
      • We add properties such as "method" which represent type of request i.e. GET or POST.
      • The action property specifies the page where we need to post data.
        • In action property we can't add "spring url tag" directly as we are not allowed to nest these tags.
        • In such a case we create a reference variable for the spring url tag as follows
          •  <spring:url value="/project/add" var="formUrl" htmlEscape="true"/>  
            
        • We then use this reference in our action property of Form Tag.
      • To bind values to a form we use "modelAttribute" property.
        • The value of this property is passed as model attribute from the controller.
        • We have added a "Project" type in controller method "addProject" as follows
          •  @RequestMapping(value="/add",method=RequestMethod.GET)  
             public String addProject(HttpSession session,Model model)  
             {  
                    session.setAttribute("token","12345");  
                    System.out.println("Invoking addProject");  
                    model.addAttribute("project",new Project());  
                    return "project_add";  
             }  
            
        • We now pass the same attribute as a value to the property "modelAttribute" as follows.
      • So our Form Tag looks as follows
        •  <form:form method="POST" action="${formUrl}" modelAttribute="project">  
          
  • Input Tag
    • Used to display text box
    • This tag is available in form tag library.
    • Helps to provide data binding in different fields in our model attribute.
    • The syntax is as follows
      •  <form:input path="name" cssClass="form-control" id="resource-name" />  
        
    • Path is the name of the attribute in the corresponding model class
    • cssClass attribute specifies the cssClass for the field.
    • id attribute specifies id for the element.
    • Next for data binding in our save method we modify the code as follows
      •       @RequestMapping("/save")  
              public String save(@ModelAttribute Resource resource,Model model)  
              {  
                   System.out.println("Invoking save() method");  
                   System.out.println(resource);  
                   model.addAttribute("resource",resource);  
                   return "resource_add";  
              }  
        
  • Select Tag
    • Present in Spring Form Tag Library
    • Used to add drop down list on jsp pages.
    • Pre selects the item when bound to a model.
    • We use the following syntax
      •  <form:select path="type" items="${itemOptions}" cssClass="form-control"></form:select>  
        
    • Path attribute contains the name of corresponding mapped model field
    • items attribute contains the list of items
      • These items should be supplied via model as List type
      • For example here we supply as
        •       @RequestMapping("/add")  
                public String add(Model model)  
                {  
                     List<String> options=new LinkedList<>(Arrays.asList(new String[]{"Material","Other","Staff","Technical Equipment"}));  
                     model.addAttribute("itemOptions",options);  
                     model.addAttribute("resource",new Resource());  
                     return "resource_add";  
                }  
          
    • cssClass attribute contains the css class
  • Check Box and Radio Buttons
    • These tags are a part of form tag library
    • Form Tag library must be included in jsp to use these
      •  <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>  
        
    • These are used to display radio buttons and check boxes on jsp pages.
    • We will create these fields and data bind them to properties "unitofMeasure" and "indicators" in our Resource entity.
    • We have setup two values of List types for these fields in our resource controller as radios and checks.
    •       @RequestMapping("/add")        public String add(Model model)  
            {  
                 List<String> options=new LinkedList<>(Arrays.asList(new String[]{"Material","Other","Staff","Technical Equipment"}));  
                 List<String> radios=new LinkedList<>(Arrays.asList(new String[]{"Hours","Piece","Tons"}));  
                 List<String> checks=new LinkedList<>(Arrays.asList(new String[]{"Lead Time","Special Rate","Requires Approval"}));  
                 model.addAttribute("itemOptions",options);  
                 model.addAttribute("radioOptions",radios);  
                 model.addAttribute("checkOptions",checks);  
                 model.addAttribute("resource",new Resource());  
                 return "resource_add";  
            }  
      
    • As seen above we have added these to model attributes to be passed to view.
    • The syntax for a form radio button is as follows
      •  <form:radiobuttons path="unitOfMeasure" items="${radioOptions}"/>    
    • The syntax for a form checkbox is as follows
      •  <form:checkboxes id="indicators" path="indicators" items="${checkOptions}"/>   
      • "Path" property indicates property to bind to in Resource entity
      • "items" property indicate model attribute from the controllers.
  • Text Area
    • Allows us to enter larger text than a regular text box.
    • Is a part of form tag library
    • We will map this to notes field in our Resource class.
    • Next we add textarea to our jsp as follows
      •  <form:textarea id="notes" path="notes" cssClass="form-control" rows="3"/>  
        
      • "Path" property indicates property to bind to in Resource entity
Resource.java
 package com.springimplant.mvc.data.entities;  
 import java.math.BigDecimal;  
 import java.util.Arrays;  
 public class Resource {  
      private Long resourceId;  
      private String name;  
      private String type;  
      private BigDecimal cost;  
      private String unitOfMeasure;  
      private String indicators[];  
      private String notes;  
      public String getNotes() {  
           return notes;  
      }  
      public void setNotes(String notes) {  
           this.notes = notes;  
      }  
      public Long getResourceId() {  
           return resourceId;  
      }  
      public void setResourceId(Long resourceId) {  
           this.resourceId = resourceId;  
      }  
      public String getName() {  
           return name;  
      }  
      public void setName(String name) {  
           this.name = name;  
      }  
      public String getType() {  
           return type;  
      }  
      public void setType(String type) {  
           this.type = type;  
      }  
      public BigDecimal getCost() {  
           return cost;  
      }  
      public void setCost(BigDecimal cost) {  
           this.cost = cost;  
      }  
      public String getUnitOfMeasure() {  
           return unitOfMeasure;  
      }  
      public void setUnitOfMeasure(String unitOfMeasure) {  
           this.unitOfMeasure = unitOfMeasure;  
      }  
      @Override  
      public String toString() {  
           return "Resource [resourceId=" + resourceId + ", name=" + name + ", type=" + type + ", cost=" + cost  
                     + ", unitOfMeasure=" + unitOfMeasure + ", indicators=" + Arrays.toString(indicators) + ", notes="  
                     + notes + "]";  
      }  
      public String[] getIndicators() {  
           return indicators;  
      }  
      public void setIndicators(String indicators[]) {  
           this.indicators = indicators;  
      }  
 }  
dd

No comments:

Post a Comment

Spring Boot

What is circular/cyclic dependency in spring boot? When two services are interdependent on each other, that is to start one service, we requ...