June 23, 2014

Working with JSTL inside Java Spring application

How to use Java Standard Tag Library in Java Spring Application


Description : JSTL provides functionality to Java Sever Page, You can replace JSTL forJSP tags. Date format, internationalization are main facilities of JSTL. You need to download and add JSTL-version.jar file to run the code. This post will explains you how to jstl in your java web application.

Inside html (your jsp page)
first you need to add the following taglib (to include jstl function to your jsp page)

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


To display jstl value use the follwing code

<div id="jstlExampleId" class="JSTL example"><a href="events.html"> JSTL </a><a href="trivenimap.html"> Java server pages    </a> <a href="trivenimap.html"> ${unit.unitName}    </a></div>


Installing jstl to your project :

copy and paste jstl-version.jar (say jstl-1.2.jar) to Webapp/WEB-INF/lib folder in your application. Only thing you have to ensure that you have no duplicates/older version of jstl jar file in your classpath

Software Requirements :

servlet container 2.5 or above, Tomcat 6.0 or above

Java bean class (Model class) : TriveniUnit.java

public class TriveniUnit {

private String unitName = "Java Spring Jstl example";
    public String getUnitName() {
        return unitName;
    }
    public void setUnitName(String unitName) {
        this.unitName = unitName;
    }

}


Create a controller class and add the following code. returning triveniUnit object to JSP page as Modelandview object. Here the model name is unit so you need to call all jstl value in the name of unit
see ${unit.unitName}
 in jsp page. This is called Expression Language  ${}

@Controller
public class ContactController {

    @RequestMapping(value = "/trivenimap", method = RequestMethod.GET)
    public ModelAndView triveniMaps() {
        System.out.println(" Java JSP Spring Java Standard Tag Library Example ");
        TriveniUnit triveniUnit = new TriveniUnit();
       return new ModelAndView("triveniMap" , "unit", triveniUnit);
     }

}


project structure


Please provide your feedback

http://javabelazy.blogspot.in/

Facebook comments