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.
Syntax to use Spring tag library
1 | <%@taglib uri="http://www.springframework.org/tags" prefix="spring"> |
htmlEscape and escapeBody tags
Example for and tags
1.Modify the web.xml to configure the Dispatcher Servlet.
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?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.htm</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.
dispatcher-servlet.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <?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="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The Index Controller
-->
<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 htmlEscape and escapeBody tags. In this example both tags are set with different cominations of values(“true”,”false”).
index.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<!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">
<h3>htmlEscape is false </h3>
<h3>escapeBody is false </h3>
<spring:htmlEscape defaultHtmlEscape="false">
<spring:escapeBody htmlEscape="false">
<h5> less than < </h5>
<h5> greater than > </h5>
</spring:escapeBody>
</spring:htmlEscape>
<h3>htmlEscape is false </h3>
<h3>escapeBody is true </h3>
<spring:htmlEscape defaultHtmlEscape="false">
<spring:escapeBody htmlEscape="true">
<h5> less than < </h5>
<h5> greater than > </h5>
</spring:escapeBody>
</spring:htmlEscape>
<h3>htmlEscape is true </h3>
<h3>escapeBody is false </h3>
<spring:htmlEscape defaultHtmlEscape="true">
<spring:escapeBody htmlEscape="false">
<h5> less than < </h5>
<h5> greater than > </h5>
</spring:escapeBody>
</spring:htmlEscape>
<h3>htmlEscape is true </h3>
<h3>escapeBody is true </h3>
<spring:htmlEscape defaultHtmlEscape="true">
<spring:escapeBody htmlEscape="true">
<h5> less than < </h5>
<h5> greater than > </h5>
</spring:escapeBody>
</spring:htmlEscape>
</body>
</html> |
4.Building and running the application
Output
Access page:
1 | http://localhost:8084/HtmlEscape/index.htm |

Spring Framework Articles
- Spring Framework Articles
- Buy Spring Framework Books from Java Books Store
- Introduction to Spring MVC Web Framework – Web Tier by Raja
- Integrating Spring Framework with Hibernate ORM Framework by Christy
- Introduction to Spring Web Framework
- Introduction to Spring’s Aspect Oriented Programming(AOP)
- Introduction to Spring Web Services






November 30, 2010
Spring Framework