Spring Tag Library
Spring MVC provides a JSP tag library (Spring Form) for making it easier to bind form elements to Model data. Spring Framework also provides you with some tags for evaluating errors, setting themes and outputting internationalized messages.
also read:
Syntax to use Spring tag library
<%@taglib uri="http://www.springframework.org/tags" prefix="spring">
transform tag
-This tag provides support for transforming properties which is not contained by Command usign propertyEditors associated with Command. This tag can only be used inside the spring:bind tag.
Example for tags
1.Modify the web.xml to configure the Dispatcher Servlet.
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
2.Create an dispatcher-servlet.xml file which contains all the configuration beans to handle the user requests.It handles the user request and dispatches to respective controllers.This file contains the countryController bean which is used to control the request.
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/index.html"> <ref bean="CountryController"/> </entry> </map> </property> </bean> <bean id="CountryController" class="countryController"> <property name="commandName" value="Country"></property> <property name="commandClass" value="country"></property> <property name="formView"><value>index</value></property> <property name="successView"><value>success</value></property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> <bean name="indexController" class="org.springframework.web.servlet.mvc.ParameterizableViewController" p:viewName="index" /> </beans>
3.Create a Jsp file index.jsp which contains the spring tranform tag. In this example the user has to select a country from the list and click submit button.The selected country name in the list will be displayed along with “selected ” word which shows the transform tag tranforming the properties of the given object.
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib uri="http://www.springframework.org/tags" prefix="spring" %> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Welcome to Spring Web MVC project</title> </head> <body bgcolor="#DDDDDD"> <h1>Spring:transform tag example.</h1> Select any country and click the submit button.The selected country in the list will be displayed along with the word "selected" thus shows the transform tag provide support for transforming properties. <form action="" method="post"> <spring:nestedPath path="Country"> <spring:bind path="country_name" > <b>Country</b> <select name="<c:out value='${status.expression}'/>"> <c:forEach items="${countryName}" var="type"> <spring:transform value="${type}" var="typeString"/> <option value="<c:out value='${typeString}'/>"/> <c:if test="${status.value == typeString}"> selected</c:if> <c:out value="${typeString}"/> </option> <h1>${name}</h1> </c:forEach> </select> <h1>${name}</h1> </spring:bind> </spring:nestedPath> <input type="submit"> </form> </body> </html>
4.Create countryController.java which extends a SimpleFormController class provides configurable form and success views.In this example we have referenceData method which returns a Map of values from the bean.These Map values are loaded into the list in the index.jsp page.
countryController.java
import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.SimpleFormController; public class countryController extends SimpleFormController { public countryController() { } @Override @SuppressWarnings("empty-statement") protected Map referenceData(HttpServletRequest request) throws Exception { country c1=new country(); Map mp=new HashMap(); ArrayList<string> li=new ArrayList<string>(); li.add("India"); li.add("USA"); li.add("UK"); c1.setCountry_name(li); mp.put("countryName",c1.getCountry_name()); return mp; } }
5.Create country.java bean to hold the country names as an ArrayList.This bean is declared in the dispatcher-servlet.xml file which makes the spring to control the bean with their respective getter and setter methods.
country.java
import java.util.ArrayList; public class country { private ArrayList<string> country_name=new ArrayList<string>(); public country() { } public ArrayList<string> getCountry_name() { return country_name; } public void setCountry_name(ArrayList<string> country_name) { this.country_name = country_name; } }
4.Building and running the application
Output
Access page:http://localhost:8084/transform/index.html
After user selects a country and clicks submit button.
also read: