• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Spring HTML TRANSFORM Tag (<spring:transform>)

December 6, 2010 //  by Krishna Srinivasan//  Leave a Comment

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:

  • Spring Framework Articles
  • Introduction to Spring MVC Web Framework – Web Tier
  • Integrating Spring Framework with Hibernate ORM Framework
  • Introduction to Spring Web Framework
  • Introduction to Spring’s Aspect Oriented Programming(AOP)
  • Introduction to Spring Web Services

Syntax to use Spring tag library

&lt;%@taglib  uri=&quot;http://www.springframework.org/tags&quot; prefix=&quot;spring&quot;&gt;

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;web-app version=&quot;2.5&quot; xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;&gt;
    &lt;context-param&gt;
        &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
        &lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt;
    &lt;/context-param&gt;
    &lt;listener&gt;
        &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
    &lt;/listener&gt;
    &lt;servlet&gt;
        &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
        &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
        &lt;load-on-startup&gt;2&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
        &lt;url-pattern&gt;*.htm&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
    &lt;session-config&gt;
        &lt;session-timeout&gt;
            30
        &lt;/session-timeout&gt;
    &lt;/session-config&gt;
    &lt;welcome-file-list&gt;
        &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
        &lt;/welcome-file-list&gt;
    &lt;/web-app&gt;

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
       xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
       xmlns:p=&quot;http://www.springframework.org/schema/p&quot;
       xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot;
       xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot;
       xsi:schemaLocation=&quot;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&quot;&gt;

    &lt;bean class=&quot;org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping&quot;/&gt;

 &lt;bean id=&quot;urlMapping&quot; class=&quot;org.springframework.web.servlet.handler.SimpleUrlHandlerMapping&quot;&gt;
      &lt;property name=&quot;urlMap&quot;&gt;
&lt;map&gt;
&lt;entry  key=&quot;/index.html&quot;&gt;
&lt;ref bean=&quot;CountryController&quot;/&gt;
&lt;/entry&gt;
&lt;/map&gt;
&lt;/property&gt;

 &lt;/bean&gt;
    &lt;bean id=&quot;CountryController&quot; class=&quot;countryController&quot;&gt;

&lt;property name=&quot;commandName&quot; value=&quot;Country&quot;&gt;&lt;/property&gt;

&lt;property name=&quot;commandClass&quot; value=&quot;country&quot;&gt;&lt;/property&gt;

&lt;property name=&quot;formView&quot;&gt;&lt;value&gt;index&lt;/value&gt;&lt;/property&gt;

&lt;property name=&quot;successView&quot;&gt;&lt;value&gt;success&lt;/value&gt;&lt;/property&gt;

    &lt;/bean&gt;

    &lt;bean id=&quot;viewResolver&quot;
          class=&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;
          p:prefix=&quot;/WEB-INF/jsp/&quot;
          p:suffix=&quot;.jsp&quot; /&gt;

    &lt;bean name=&quot;indexController&quot;
          class=&quot;org.springframework.web.servlet.mvc.ParameterizableViewController&quot;
          p:viewName=&quot;index&quot; /&gt;

&lt;/beans&gt;

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


&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@taglib uri=&quot;http://www.springframework.org/tags&quot; prefix=&quot;spring&quot; %&gt;
&lt;%@taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot; %&gt;
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
   &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;

&lt;html&gt;
    &lt;head&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
        &lt;title&gt;Welcome to Spring Web MVC project&lt;/title&gt;
    &lt;/head&gt;

    &lt;body bgcolor=&quot;#DDDDDD&quot;&gt;
        &lt;h1&gt;Spring:transform tag example.&lt;/h1&gt;
      Select any country and click the submit button.The selected country in the list will
      be displayed along with the word &quot;selected&quot; thus shows the transform tag provide support for transforming properties.
       &lt;form action=&quot;&quot; method=&quot;post&quot;&gt;
           &lt;spring:nestedPath path=&quot;Country&quot;&gt;
        &lt;spring:bind path=&quot;country_name&quot; &gt;
      &lt;b&gt;Country&lt;/b&gt; &lt;select name=&quot;&lt;c:out value='${status.expression}'/&gt;&quot;&gt;

            &lt;c:forEach items=&quot;${countryName}&quot; var=&quot;type&quot;&gt;

               &lt;spring:transform value=&quot;${type}&quot; var=&quot;typeString&quot;/&gt;
                &lt;option value=&quot;&lt;c:out value='${typeString}'/&gt;&quot;/&gt;

                    &lt;c:if test=&quot;${status.value == typeString}&quot;&gt; selected&lt;/c:if&gt;

                    &lt;c:out value=&quot;${typeString}&quot;/&gt;
                &lt;/option&gt;
                &lt;h1&gt;${name}&lt;/h1&gt;
            &lt;/c:forEach&gt;
        &lt;/select&gt;
          &lt;h1&gt;${name}&lt;/h1&gt;
        &lt;/spring:bind&gt;
        &lt;/spring:nestedPath&gt;
        &lt;input type=&quot;submit&quot;&gt;
        &lt;/form&gt;

    &lt;/body&gt;
&lt;/html&gt;

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(&quot;empty-statement&quot;)
    protected Map referenceData(HttpServletRequest request) throws Exception {
        country c1=new country();
        Map mp=new HashMap();

        ArrayList&lt;string&gt; li=new ArrayList&lt;string&gt;();
        li.add(&quot;India&quot;);
        li.add(&quot;USA&quot;);
        li.add(&quot;UK&quot;);
        c1.setCountry_name(li);
        mp.put(&quot;countryName&quot;,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&lt;string&gt; country_name=new ArrayList&lt;string&gt;();

    public country() {

    }

    public ArrayList&lt;string&gt; getCountry_name() {
        return country_name;
    }

    public void setCountry_name(ArrayList&lt;string&gt; 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:

  • Spring Framework Articles
  • Introduction to Spring MVC Web Framework – Web Tier
  • Integrating Spring Framework with Hibernate ORM Framework
  • Introduction to Spring Web Framework
  • Introduction to Spring’s Aspect Oriented Programming(AOP)
  • Introduction to Spring Web Services

Category: Spring FrameworkTag: Spring Tags

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Creating Database aware applications in Ruby on Rails
Next Post: What is Ext GWT 2.0? »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact