<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JavaBeat &#187; AbhilashEtikala</title>
	<atom:link href="http://www.javabeat.net/author/abhilashetikala/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javabeat.net</link>
	<description>Java Technology News</description>
	<lastBuildDate>Mon, 13 May 2013 20:10:23 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Spring MVC &#8211; Setter Injection Example</title>
		<link>http://www.javabeat.net/2011/01/spring-mvc-setter-injection-example/</link>
		<comments>http://www.javabeat.net/2011/01/spring-mvc-setter-injection-example/#comments</comments>
		<pubDate>Fri, 21 Jan 2011 23:04:44 +0000</pubDate>
		<dc:creator>AbhilashEtikala</dc:creator>
				<category><![CDATA[Spring Framework]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/examples/?p=482</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>Setter Injection Spring IOC container will inject the dependencies using the setter bean class.All the dependencies are declared as the instance variables of the bean class.Here we show a simple Book details example of Setter-Injection. Spring Framework Articles Spring Articles Spring Books Introduction to Spring&#8217;s Aspect Oriented Programming(AOP) Life Cycle Management of a Spring Bean [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><a id="dd_start"></a><h2>Setter Injection </h2>
<p><b>Spring IOC</b> container will inject the dependencies using the setter bean class.All the dependencies are declared as the instance variables of the bean class.Here we show a simple Book details example of Setter-Injection.</p>
<h2>Spring Framework Articles</h2>
<ul>
<li><a href="http://www.javabeat.net/articles/spring/1/">Spring Articles</a></li>
<li><a href="http://www.javabeat.net/books/spring/1/">Spring Books</a></li>
<li><a href="http://www.javabeat.net/articles/51-introduction-to-springs-aspect-oriented-programminga-1.html">Introduction to Spring&#8217;s Aspect Oriented Programming(AOP)</a></li>
<li><a href="http://www.javabeat.net/articles/49-life-cycle-management-of-a-spring-bean-1.html">Life Cycle Management of a Spring Bean</a></li>
<li><a href="http://www.javabeat.net/articles/71-introduction-to-spring-web-framework-1.html">Introduction to Spring Web MVC Framework</a></li>
<li><a href="http://www.javabeat.net/tutorials/spring/1/">Spring Framework Beginner Tutorials</a></li>
</ul>
<h2>Spring MVC Setter Injection example</h2>
<p>1.Modify the <b>web.xml</b> to configure the <b>Dispatcher Servlet</b>.</p>
<p><b>web.xml</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&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;*.html&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.jsp&lt;/welcome-file&gt;
        &lt;/welcome-file-list&gt;
    &lt;/web-app&gt;</pre></td></tr></table></div>

<p>2.Create an <b>dispatcher-servlet.xml</b> file which contains all the configuration beans to handle the user requests.It handles the user request and dispatches  to respective controllers. In this file we declare the bean called <b>Book</b> and we set the properties of the bean using <i>property</i> with corresponding <i>name </i> and <i>value</i> attributes.</p>
<p>
Below code shows how to pass the dependancy arguments to the bean using <i>property attribute</i>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="language" style="font-family:monospace;"> &lt;bean id=&quot;book&quot; class=&quot;Book&quot;&gt;
        &lt;property name=&quot;bookName&quot; value=&quot;Spring in Action&quot;/&gt;
        &lt;property name=&quot;price&quot; value=&quot;31&quot;/&gt;
        &lt;property name=&quot;author&quot; value=&quot;Craig Walls, Ryan Breidenbach&quot;/&gt;
    &lt;/bean&gt;</pre></td></tr></table></div>

</p>
<p><b>dispatcher-servlet.xml</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&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;
&nbsp;
    &lt;bean class=&quot;org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping&quot;/&gt;
&nbsp;
&nbsp;
    &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.htm&quot;&gt;
&lt;ref bean=&quot;book&quot;/&gt;
&lt;/entry&gt;
&lt;/map&gt;
&lt;/property&gt;
    &lt;/bean&gt;
&nbsp;
    &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;
&nbsp;
&nbsp;
&nbsp;
    &lt;bean id=&quot;book&quot; class=&quot;Book&quot;&gt;
        &lt;property name=&quot;bookName&quot; value=&quot;Spring in Action&quot;/&gt;
        &lt;property name=&quot;price&quot; value=&quot;31&quot;/&gt;
        &lt;property name=&quot;author&quot; value=&quot;Craig Walls, Ryan Breidenbach&quot;/&gt;
    &lt;/bean&gt;
&nbsp;
&lt;/beans&gt;</pre></td></tr></table></div>

<p>
3.Create a Jsp file <b>index.jsp</b> to display the output.</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p><b>index.jsp</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@taglib  uri=&quot;http://www.springframework.org/tags/form&quot; prefix=&quot;form&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;
&nbsp;
&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;
&nbsp;
 &lt;body bgcolor=&quot;#DDDDDD&quot;&gt;
       &lt;h2&gt;Spring MVC Setter Injection Example&lt;/h2&gt;
       &lt;table align=&quot;center&quot; style=&quot;font-weight:bold;&quot;&gt;
           &lt;tr&gt;
               &lt;td&gt;Book Name&lt;/td&gt;
               &lt;td&gt;${BookName}&lt;/td&gt;
           &lt;/tr&gt;
            &lt;tr&gt;
               &lt;td&gt;Price&lt;/td&gt;
               &lt;td&gt;${price}&lt;/td&gt;
           &lt;/tr&gt;
            &lt;tr&gt;
               &lt;td&gt;Author&lt;/td&gt;
               &lt;td&gt;${author}&lt;/td&gt;
           &lt;/tr&gt;
       &lt;/table&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

</p>
<p>4.Create a Java class file <b>Book.java</b> which extends <b>AbstractController </b> and contains three fields (bookName,price and author) which will be injected from the Spring IOC using the setter methods of the bean. Here this class acts like a controller.</p>
<p><b>Book.java</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">public class Book extends AbstractController {
&nbsp;
    private String bookName;
    private int price;
    private String author;
&nbsp;
    public Book() {
    }
&nbsp;
&nbsp;
    protected ModelAndView handleRequestInternal(
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
&nbsp;
        ModelAndView mv=new ModelAndView();
        mv.addObject(&quot;BookName&quot;,getBookName());
        mv.addObject(&quot;price&quot;, getPrice());
        mv.addObject(&quot;author&quot;, getAuthor());
        return mv;
        //throw new UnsupportedOperationException(&quot;Not yet implemented&quot;);
    }
&nbsp;
    public String getAuthor() {
        return author;
    }
&nbsp;
    public void setAuthor(String author) {
        this.author = author;
    }
&nbsp;
    public String getBookName() {
        return bookName;
    }
&nbsp;
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
&nbsp;
    public int getPrice() {
        return price;
    }
&nbsp;
    public void setPrice(int price) {
        this.price = price;
    }
&nbsp;
}</pre></td></tr></table></div>

<p>5.Building and running the application</p>
<h4><b>Output</b></h4>
<p><b>Access page:
<pre><code>http://localhost:8080/SetterInjection/index.htm</code></pre>
<p></b></p>
<p><img src="images/2011/01/spring-setter-injection/spring-setter-injection-1.jpg" width="585" height="224"></p>
<ul>
<li><a href="http://www.javabeat.net/articles/71-introduction-to-spring-web-framework-1.html">Introduction to Spring Web MVC Framework</a></li>
</ul>
<div class='dd_outer'><div class='dd_inner'><div id='dd_ajax_float'><div class='dd_button_v'><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Fwww.javabeat.net%2Fauthor%2Fabhilashetikala%2Ffeed%2F" send="false" show_faces="false"  layout="box_count" width="50"  ></fb:like></div><div style='clear:left'></div><div class='dd_button_v'><script type='text/javascript' src='https://apis.google.com/js/plusone.js'></script><g:plusone size='tall' href='http://www.javabeat.net/author/abhilashetikala/feed/'></g:plusone></div><div style='clear:left'></div><div class='dd_button_v'><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.javabeat.net/author/abhilashetikala/feed/" data-count="vertical" data-text="" data-via="javabeat" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div style='clear:left'></div><div class='dd_button_extra_v'><script type="text/javascript">jQuery(document).load(function(){ stLight.options({publisher:'bab47279-62c9-46af-addc-79fd1fe8fee0'}); });</script><div class="st_email_custom"><span id='dd_email_text'>email</span></div></div><div style='clear:left'></div><div class='dd_button_extra_v'><div id='dd_print_button'><span id='dd_print_text'><a href='javascript:window:print()'>print</a></span></div></div><div style='clear:left'></div></div></div></div><script type="text/javascript">var dd_offset_from_content = 44; var dd_top_offset_from_content = 0;</script><script type="text/javascript" src="http://www.javabeat.net/wp-content/plugins/digg-digg//js/diggdigg-floating-bar.js?ver=5.3.0"></script><div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2011/01/spring-mvc-setter-injection-example/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Spring MVC &#8211; Constructor Injection Example</title>
		<link>http://www.javabeat.net/2011/01/spring-mvc-constructor-injection-example/</link>
		<comments>http://www.javabeat.net/2011/01/spring-mvc-constructor-injection-example/#comments</comments>
		<pubDate>Fri, 21 Jan 2011 23:04:11 +0000</pubDate>
		<dc:creator>AbhilashEtikala</dc:creator>
				<category><![CDATA[Spring Framework]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/examples/?p=480</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>Constructor Injection Spring IOC will inject the dependencies using the constructor.All the dependencies are declared in the constructor. Here we show a simple student details example of Constructor-Injection. We have already published another article on the Spring Constructor Injection. This example provides more details. Spring Framework Articles Spring Articles Spring Books Introduction to Spring&#8217;s Aspect [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><p><!--Krishna: Write what is IOC and its advantages in the application. This will be good for the readers to understand. --></p>
<h2>Constructor Injection</h2>
<p><b>Spring IOC</b> will inject the dependencies using the constructor.All the dependencies are declared in the constructor.<br />
Here we show a simple student details example of Constructor-Injection. We have already published another article on the<br />
<a href="http://www.javabeat.net/tips/93-constructor-injection-in-spring-ioc.html">Spring Constructor Injection</a>. This example<br />
provides more details.
</p>
<h2>Spring Framework Articles</h2>
<ul>
<li><a href="http://www.javabeat.net/articles/spring/1/">Spring Articles</a></li>
<li><a href="http://www.javabeat.net/books/spring/1/">Spring Books</a></li>
<li><a href="http://www.javabeat.net/articles/51-introduction-to-springs-aspect-oriented-programminga-1.html">Introduction to Spring&#8217;s Aspect Oriented Programming(AOP)</a></li>
<li><a href="http://www.javabeat.net/articles/49-life-cycle-management-of-a-spring-bean-1.html">Life Cycle Management of a Spring Bean</a></li>
<li><a href="http://www.javabeat.net/articles/71-introduction-to-spring-web-framework-1.html">Introduction to Spring Web MVC Framework</a></li>
<li><a href="http://www.javabeat.net/tutorials/spring/1/">Spring Framework Beginner Tutorials</a></li>
</ul>
<h2>Spring MVC Constructor Injection example</h2>
<p>1.Modify the <b>web.xml</b> to configure the <b>Dispatcher Servlet</b>.</p>
<p><b>web.xml</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;code&gt;
&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;*.html&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.jsp&lt;/welcome-file&gt;
        &lt;/welcome-file-list&gt;
    &lt;/web-app&gt;
&lt;/code&gt;</pre></td></tr></table></div>

<p><!--Krishna:You an send me another tip on how to write dispatcher servlet. Just simple example in this tip. You can take the<br />
same example from this tip and explain about the dispatcher servlet. After that tips we will publish this tips --></p>
<p>2.Create an <b>dispatcher-servlet.xml</b> file which contains all the configuration beans to handle the user requests.It handles the user request and dispatches  to respective controllers. In this file we declare the bean called <b>Student</b> and we pass the respective constructor-arguments using <i>constructor-arg</i> attribute.</p>
<p>
Below code shows how to pass the dependancy arguments to constructor using <i>constructor-arg attribute</i>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;code&gt;
&lt;bean id=&quot;student&quot; class=&quot;Student&quot;&gt;
             &lt;constructor-arg index=&quot;0&quot; type=&quot;java.lang.String&quot; value=&quot;Ganesh&quot;/&gt;
             &lt;constructor-arg index=&quot;1&quot; type=&quot;int&quot; value=&quot;20&quot;/&gt;
             &lt;constructor-arg index=&quot;2&quot; type=&quot;java.lang.String&quot; value=&quot;Computer Science&quot;/&gt;
&nbsp;
          &lt;/bean&gt;
&nbsp;
&lt;/code&gt;</pre></td></tr></table></div>

</p>
<p><b>dispatcher-servlet.xml</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;code&gt;
&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;
&nbsp;
    &lt;bean class=&quot;org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping&quot;/&gt;
&nbsp;
&nbsp;
    &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;student&quot;/&gt;
&lt;/entry&gt;
&lt;/map&gt;
&lt;/property&gt;
&nbsp;
 &lt;/bean&gt;
&nbsp;
&nbsp;
 &lt;bean id=&quot;student&quot; class=&quot;Student&quot;&gt;
             &lt;constructor-arg index=&quot;0&quot; type=&quot;java.lang.String&quot; value=&quot;Ganesh&quot;/&gt;
             &lt;constructor-arg index=&quot;1&quot; type=&quot;int&quot; value=&quot;20&quot;/&gt;
             &lt;constructor-arg index=&quot;2&quot; type=&quot;java.lang.String&quot; value=&quot;Computer Science&quot;/&gt;
&nbsp;
          &lt;/bean&gt;
&nbsp;
&nbsp;
     &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;/beans&gt;
&nbsp;
&nbsp;
&lt;/code&gt;</pre></td></tr></table></div>

<p>
3.Create a Jsp file <b>index.jsp</b> to display the output.</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p><b>index.jsp</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;code&gt;
&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@taglib  uri=&quot;http://www.springframework.org/tags/form&quot; prefix=&quot;form&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;
&nbsp;
&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;
&nbsp;
   &lt;body bgcolor=&quot;#DDDDDD&quot;&gt;
&lt;h2&gt;Spring MVC Constructor Injection Example&lt;/h2&gt;
        &lt;table align=&quot;center&quot; style=&quot;font-weight:bold;&quot;&gt;
            &lt;tr&gt;
                &lt;td&gt;Student Name &lt;/td&gt;
                &lt;td&gt;${name}&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;Age &lt;/td&gt;
                &lt;td&gt;${age}&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;Branch &lt;/td&gt;
                &lt;td&gt;${branch}&lt;/td&gt;
            &lt;/tr&gt;
&nbsp;
        &lt;/table&gt;
&nbsp;
    &lt;/body&gt;
&lt;/html&gt;
&nbsp;
&nbsp;
&lt;/code&gt;</pre></td></tr></table></div>

</p>
<p>4.Create a Java class file <b>Student.java</b> which extends <b>AbstractController </b> and contains three fields (name,age and branch) which will be injected from the Spring IOC using the constructor. Here this class acts like a controller.</p>
<p><b>Student.java</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
58
59
60
61
62
63
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;code&gt;
public class Student extends AbstractController
&nbsp;
{
&nbsp;
    private String name;
    private int age;
    private String branch;
&nbsp;
    public Student() {
&nbsp;
    }
&nbsp;
&nbsp;
    public Student(String name, int age, String branch)  {
        this.name = name;
        this.age = age;
        this.branch = branch;
    }
&nbsp;
    public int getAge() {
        return age;
    }
&nbsp;
    public void setAge(int age) {
        this.age = age;
    }
&nbsp;
    public String getBranch() {
        return branch;
    }
&nbsp;
    public void setBranch(String branch) {
        this.branch = branch;
    }
&nbsp;
    public String getName() {
        return name;
    }
&nbsp;
    public void setName(String name) {
        this.name = name;
    }
&nbsp;
&nbsp;
&nbsp;
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
&nbsp;
         ModelAndView mv=new ModelAndView();
&nbsp;
        mv.addObject(&quot;name&quot;,getName());
        mv.addObject(&quot;age&quot;, getAge());
        mv.addObject(&quot;branch&quot;, getBranch());
        return mv;
&nbsp;
    }
&nbsp;
   }
&nbsp;
&nbsp;
&nbsp;
&lt;/code&gt;</pre></td></tr></table></div>

<p>5.Building and running the application</p>
<h4><b>Output</b></h4>
<p><b>Access page:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;code&gt;http://localhost:8080/ConstructorInjection/index.htm&lt;/code&gt;</pre></td></tr></table></div>

<p></b></p>
<p><img src="images/2011/01/spring-constructor-injection/spring-constructor-injection-1.jpg" width="585" height="224"></p>
<ul>
<li><a href="http://www.javabeat.net/articles/71-introduction-to-spring-web-framework-1.html">Introduction to Spring Web MVC Framework</a</li>
</ul>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2011/01/spring-mvc-constructor-injection-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring MVC &#8211; DispatcherServlet Example</title>
		<link>http://www.javabeat.net/2011/01/spring-mvc-dispatcherservlet-example/</link>
		<comments>http://www.javabeat.net/2011/01/spring-mvc-dispatcherservlet-example/#comments</comments>
		<pubDate>Fri, 21 Jan 2011 23:03:40 +0000</pubDate>
		<dc:creator>AbhilashEtikala</dc:creator>
				<category><![CDATA[Spring Framework]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/examples/?p=478</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>DispatcherServlet Configuration DispatcherServlet : In Spring&#8217;s web MVC framework the mechanism of dispatching the request to the appropriate controllers is achieved by configuring the DispatcherServlet class. DispatcherServlet is the class which manages the entire request handling process.Like a normal servlet DispatcherServlet also needs to be configured in the web deployement Descriptor(web.xml).By default DispatcherServlet will look [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><h2>DispatcherServlet Configuration</h2>
<p><b>DispatcherServlet :</b> In Spring&#8217;s web MVC framework the mechanism of dispatching the request to the appropriate controllers is achieved by configuring the <i>DispatcherServlet</i> class. <i>DispatcherServlet</i> is the class which manages the entire request handling process.Like a normal servlet <i>DispatcherServlet</i> also needs to be configured in the web deployement Descriptor(web.xml).By default DispatcherServlet will look for a name <i>dispatcher-servlet.xml</i> to load the Spring MVC configuration. Spring&#8217;s DispatcherServlet is completly integrated with the Spring ApplicationContext and enables to use all the other features of the Spring.This example will explain about DispatcherServlet and its configuration.</p>
<h2>Spring Framework Articles</h2>
<ul>
<li><a href="http://www.javabeat.net/articles/spring/1/">Spring Articles</a></li>
<li><a href="http://www.javabeat.net/books/spring/1/">Spring Books</a></li>
<li><a href="http://www.javabeat.net/articles/51-introduction-to-springs-aspect-oriented-programminga-1.html">Introduction to Spring&#8217;s Aspect Oriented Programming(AOP)</a></li>
<li><a href="http://www.javabeat.net/articles/49-life-cycle-management-of-a-spring-bean-1.html">Life Cycle Management of a Spring Bean</a></li>
<li><a href="http://www.javabeat.net/articles/71-introduction-to-spring-web-framework-1.html">Introduction to Spring Web MVC Framework</a></li>
<li><a href="http://www.javabeat.net/tutorials/spring/1/">Spring Framework Beginner Tutorials</a></li>
</ul>
<h2>Spring MVC DispatcherServlet example</h2>
<p>1.Modify the <b>web.xml</b> to configure the <b>DispatcherServlet</b>.</p>
<p><b>web.xml</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;code&gt;
&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;!--Here we specify about the DispatcherServlet class in the Web Deployment Descriptor--&gt;
	&lt;servlet&gt;
        &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
&nbsp;
        &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;*.html&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.jsp&lt;/welcome-file&gt;
        &lt;/welcome-file-list&gt;
    &lt;/web-app&gt;
&lt;/code&gt;</pre></td></tr></table></div>

<p>2.Create an <b>dispatcher-servlet.xml</b> file which contains all the configuration beans to handle the user requests.It handles the user request and dispatches  to respective controllers.
</p>
<p>The basic usage of the dispatcher-servlet.xml </p>
<ul>
<li>Spring&#8217;s MVC Inversion of Control is configured in dispatcher-servlet.xml file.</li>
<li>Any <b>dependency Injection</b> for the beans is also configured in the dispatcher-servlet.xml like <i>ConstructorInjection,SetterInjection,InterfaceInjection</i>.</li>
<li>Spring MVC provides a feature to initialize and inject the dependencies from the dispatcher-servlet.xml</li>
<li>ViewResolvers are also configured in dispatcher-servlet.xml</li>
<li>Predefined tags supported by Spring Framework makes easier for the user to initialize the beans.</li>
</ul>
<p><b>dispatcher-servlet.xml</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;code&gt;
&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;
&nbsp;
    &lt;bean class=&quot;org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping&quot;/&gt;
&nbsp;
&nbsp;
    &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;student&quot;/&gt;
&lt;/entry&gt;
&lt;/map&gt;
&lt;/property&gt;
&nbsp;
 &lt;/bean&gt;
&nbsp;
&nbsp;
 &lt;bean id=&quot;student&quot; class=&quot;Student&quot;&gt;
             &lt;constructor-arg index=&quot;0&quot; type=&quot;java.lang.String&quot; value=&quot;Ganesh&quot;/&gt;
             &lt;constructor-arg index=&quot;1&quot; type=&quot;int&quot; value=&quot;20&quot;/&gt;
             &lt;constructor-arg index=&quot;2&quot; type=&quot;java.lang.String&quot; value=&quot;Computer Science&quot;/&gt;
&nbsp;
          &lt;/bean&gt;
&nbsp;
&nbsp;
     &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;/beans&gt;
&nbsp;
&nbsp;
&lt;/code&gt;</pre></td></tr></table></div>

<p>
3.Create a Jsp file <b>index.jsp</b> to display the output.</p>
<p><b>index.jsp</b></p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;code&gt;
&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@taglib  uri=&quot;http://www.springframework.org/tags/form&quot; prefix=&quot;form&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;
&nbsp;
&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;
&nbsp;
   &lt;body bgcolor=&quot;#DDDDDD&quot;&gt;
&lt;h2&gt;Spring MVC DispatcherServlet Example&lt;/h2&gt;
        &lt;table align=&quot;center&quot; style=&quot;font-weight:bold;&quot;&gt;
            &lt;tr&gt;
                &lt;td&gt;Student Name &lt;/td&gt;
                &lt;td&gt;${name}&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;Age &lt;/td&gt;
                &lt;td&gt;${age}&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;Branch &lt;/td&gt;
                &lt;td&gt;${branch}&lt;/td&gt;
            &lt;/tr&gt;
&nbsp;
        &lt;/table&gt;
&nbsp;
    &lt;/body&gt;
&lt;/html&gt;
&nbsp;
&nbsp;
&lt;/code&gt;</pre></td></tr></table></div>

</p>
<p>4.Create a Java class file <b>Student.java</b> which extends <b>AbstractController </b> and contains three fields (name,age and branch) which will be injected from the Spring IOC using the constructor. Here this class acts like a controller.</p>
<p><b>Student.java</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
58
59
60
61
62
63
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;code&gt;
public class Student extends AbstractController
&nbsp;
{
&nbsp;
    private String name;
    private int age;
    private String branch;
&nbsp;
    public Student() {
&nbsp;
    }
&nbsp;
&nbsp;
    public Student(String name, int age, String branch)  {
        this.name = name;
        this.age = age;
        this.branch = branch;
    }
&nbsp;
    public int getAge() {
        return age;
    }
&nbsp;
    public void setAge(int age) {
        this.age = age;
    }
&nbsp;
    public String getBranch() {
        return branch;
    }
&nbsp;
    public void setBranch(String branch) {
        this.branch = branch;
    }
&nbsp;
    public String getName() {
        return name;
    }
&nbsp;
    public void setName(String name) {
        this.name = name;
    }
&nbsp;
&nbsp;
&nbsp;
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
&nbsp;
         ModelAndView mv=new ModelAndView();
&nbsp;
        mv.addObject(&quot;name&quot;,getName());
        mv.addObject(&quot;age&quot;, getAge());
        mv.addObject(&quot;branch&quot;, getBranch());
        return mv;
&nbsp;
    }
&nbsp;
   }
&nbsp;
&nbsp;
&nbsp;
&lt;/code&gt;</pre></td></tr></table></div>

<p>5.Building and running the application</p>
<h4><b>Output</b></h4>
<p><b>Access page:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;code&gt;http://localhost:8080/DispatcherServlet/index.htm&lt;/code&gt;</pre></td></tr></table></div>

<p></b></p>
<p><img src="images/2011/01/spring-dispatcher-servlet/spring-dispatcherservlet-1.jpg" width="585" height="224"></p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2011/01/spring-mvc-dispatcherservlet-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring HTML TRANSFORM Tag (&lt;spring:transform&gt;)</title>
		<link>http://www.javabeat.net/2010/12/spring-html-transform-tag-springtransform/</link>
		<comments>http://www.javabeat.net/2010/12/spring-html-transform-tag-springtransform/#comments</comments>
		<pubDate>Sun, 05 Dec 2010 23:03:06 +0000</pubDate>
		<dc:creator>AbhilashEtikala</dc:creator>
				<category><![CDATA[Spring Framework]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/examples/?p=476</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>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. Spring Framework Articles Buy Spring Framework Books from Java Books Store Introduction to Spring MVC [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><h2>Spring  Tag Library</h2>
<p><b>Spring MVC</b> 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.</p>
<p align="justify">
<ul>
<li><a href="http://www.javabeat.net/articles/spring/1/">Spring Framework Articles</a></li>
<li>Buy <a href="http://astore.amazon.com/javabeat-20?_encoding=UTF8&amp;node=11" target="_blank">Spring Framework Books</a> from <a href="http://astore.amazon.com/javabeat-20" target="_blank">Java Books Store</a></li>
<li><a href="http://www.javabeat.net/articles/18-introduction-to-spring-mvc-web-framework-web-tier-1.html">Introduction to Spring MVC Web Framework &#8211; Web Tier</a> by <a href="http://www.javabeat.net/authors/Raja/">Raja</a></li>
<li><a href="http://www.javabeat.net/articles/42-integrating-spring-framework-with-hibernate-orm-framewo-1.html">Integrating Spring Framework with Hibernate ORM Framework</a> by <a href="http://www.javabeat.net/authors/Christy/">Christy</a></li>
<li><a href="http://www.javabeat.net/articles/71-introduction-to-spring-web-framework-1.html">Introduction to Spring Web Framework</a></li>
<li><a href="http://www.javabeat.net/articles/51-76%20introduction-to-springs-aspect-oriented-programminga-1.html">Introduction to Spring&#8217;s Aspect Oriented Programming(AOP)</a></li>
<li><a href="http://www.javabeat.net/articles/175-introduction-to-spring-web-services-1.html">Introduction to Spring Web Services</a></li>
</ul>
<h2>Syntax to use Spring tag library</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">	&lt;code&gt;&lt;%@taglib  uri=&quot;http://www.springframework.org/tags&quot; prefix=&quot;spring&quot;&gt;&lt;/code&gt;</pre></td></tr></table></div>

<h2>transform tag</h2>
<p><b><spring:transform></b>-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 <i>spring:bind</i> tag.</p>
<h2>Example for <spring:transform> tags</h2>
<p>1.Modify the <b>web.xml</b> to configure the <b>Dispatcher Servlet</b>.</p>
<p><b>web.xml</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;code&gt;
&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;
&lt;/code&gt;</pre></td></tr></table></div>

<p>2.Create an <b>dispatcher-servlet.xml</b> 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 <i>countryController</i> bean which is used to control the request.</p>
<p><b>dispatcher-servlet.xml</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;code&gt;
&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;
&nbsp;
    &lt;bean class=&quot;org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping&quot;/&gt;
&nbsp;
&nbsp;
 &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;
&nbsp;
 &lt;/bean&gt;
    &lt;bean id=&quot;CountryController&quot; class=&quot;countryController&quot;&gt;
&nbsp;
&nbsp;
&lt;property name=&quot;commandName&quot; value=&quot;Country&quot;&gt;&lt;/property&gt;
&nbsp;
&lt;property name=&quot;commandClass&quot; value=&quot;country&quot;&gt;&lt;/property&gt;
&nbsp;
&nbsp;
&lt;property name=&quot;formView&quot;&gt;&lt;value&gt;index&lt;/value&gt;&lt;/property&gt;
&nbsp;
&lt;property name=&quot;successView&quot;&gt;&lt;value&gt;success&lt;/value&gt;&lt;/property&gt;
&nbsp;
    &lt;/bean&gt;
&nbsp;
    &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;
&nbsp;
&nbsp;
    &lt;bean name=&quot;indexController&quot;
          class=&quot;org.springframework.web.servlet.mvc.ParameterizableViewController&quot;
          p:viewName=&quot;index&quot; /&gt;
&nbsp;
&lt;/beans&gt;
&nbsp;
&nbsp;
&nbsp;
&lt;/code&gt;</pre></td></tr></table></div>

<p>
3.Create a Jsp file <b>index.jsp</b> which contains the spring <b>tranform</b> 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 &#8220;selected &#8221; word which shows the transform tag tranforming the properties of the given object.</p>
<p><b>index.jsp</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;code&gt;
&nbsp;
&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;
&nbsp;
&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;
&nbsp;
    &lt;body bgcolor=&quot;#DDDDDD&quot;&gt;
        &lt;h1&gt;Spring:transform tag example.&lt;/h1&gt;
      &lt;p&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;/p&gt;
       &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;
&nbsp;
            &lt;c:forEach items=&quot;${countryName}&quot; var=&quot;type&quot;&gt;
&nbsp;
               &lt;spring:transform value=&quot;${type}&quot; var=&quot;typeString&quot;/&gt;
                &lt;option value=&quot;&lt;c:out value='${typeString}'/&gt;&quot;/&gt;
&nbsp;
                    &lt;c:if test=&quot;${status.value == typeString}&quot;&gt; selected&lt;/c:if&gt;
&nbsp;
                    &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;
&nbsp;
    &lt;/body&gt;
&lt;/html&gt;
&nbsp;
&nbsp;
&nbsp;
&lt;/code&gt;</pre></td></tr></table></div>

</p>
<p>4.Create <b>countryController.java</b> which extends a SimpleFormController class provides configurable form and success views.In this example we have <b>referenceData</b> method which returns a Map of values from the bean.These Map values are loaded into the list in the index.jsp page.</p>
<p><b>countryController.java</b></p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;code&gt;
&nbsp;
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;
&nbsp;
&nbsp;
public class countryController extends SimpleFormController {
&nbsp;
    public countryController() {
&nbsp;
    }
&nbsp;
    @Override
    @SuppressWarnings(&quot;empty-statement&quot;)
    protected Map referenceData(HttpServletRequest request) throws Exception {
        country c1=new country();
        Map mp=new HashMap();
&nbsp;
        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());
&nbsp;
        return mp;
    }
&nbsp;
}
&nbsp;
&lt;/code&gt;</pre></td></tr></table></div>

</p>
<p>5.Create <b>country.java</b> 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.</p>
<p><b>country.java</b></p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;code&gt;
&nbsp;
import java.util.ArrayList;
&nbsp;
&nbsp;
public class country {
&nbsp;
    private ArrayList&lt;string&gt; country_name=new ArrayList&lt;string&gt;();
&nbsp;
    public country() {
&nbsp;
    }
&nbsp;
&nbsp;
&nbsp;
    public ArrayList&lt;string&gt; getCountry_name() {
        return country_name;
    }
&nbsp;
    public void setCountry_name(ArrayList&lt;string&gt; country_name) {
        this.country_name = country_name;
    }
&nbsp;
 }
&nbsp;
&lt;/code&gt;</pre></td></tr></table></div>

</p>
<p>4.Building and running the application</p>
<h4><b>Output</b></h4>
<p><b>Access page:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;code&gt;http://localhost:8084/transform/index.html&lt;/code&gt;</pre></td></tr></table></div>

<p></b></p>
<p><img src="images/2010/12/spring-transform/spring-transform-1.jpg" width="700" height="300"></p>
<p><b>After user selects a country and clicks submit button.</b></p>
<p><img src="images/2010/12/spring-transform/spring-transform-2.jpg" width="700" height="300"></p>
<p align="justify">
<ul>
<li><a href="http://www.javabeat.net/articles/spring/1/">Spring Framework Articles</a></li>
<li>Buy <a href="http://astore.amazon.com/javabeat-20?_encoding=UTF8&amp;node=11" target="_blank">Spring Framework Books</a> from <a href="http://astore.amazon.com/javabeat-20" target="_blank">Java Books Store</a></li>
<li><a href="http://www.javabeat.net/articles/18-introduction-to-spring-mvc-web-framework-web-tier-1.html">Introduction to Spring MVC Web Framework &#8211; Web Tier</a> by <a href="http://www.javabeat.net/authors/Raja/">Raja</a></li>
<li><a href="http://www.javabeat.net/articles/42-integrating-spring-framework-with-hibernate-orm-framewo-1.html">Integrating Spring Framework with Hibernate ORM Framework</a> by <a href="http://www.javabeat.net/authors/Christy/">Christy</a></li>
<li><a href="http://www.javabeat.net/articles/71-introduction-to-spring-web-framework-1.html">Introduction to Spring Web Framework</a></li>
<li><a href="http://www.javabeat.net/articles/51-76%20introduction-to-springs-aspect-oriented-programminga-1.html">Introduction to Spring&#8217;s Aspect Oriented Programming(AOP)</a></li>
<li><a href="http://www.javabeat.net/articles/175-introduction-to-spring-web-services-1.html">Introduction to Spring Web Services</a></li>
</ul>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2010/12/spring-html-transform-tag-springtransform/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring HTML ESCAPE and ESCAPE BODY Tags (&lt;spring:htmlEscape&gt; and &lt;spring:escapeBody&gt;)</title>
		<link>http://www.javabeat.net/2010/11/spring-html-escape-and-escape-body-tags-springhtmlescape-and-springescapebody/</link>
		<comments>http://www.javabeat.net/2010/11/spring-html-escape-and-escape-body-tags-springhtmlescape-and-springescapebody/#comments</comments>
		<pubDate>Mon, 29 Nov 2010 23:02:34 +0000</pubDate>
		<dc:creator>AbhilashEtikala</dc:creator>
				<category><![CDATA[Spring Framework]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/examples/?p=474</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>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. Read Integrating Struts With Spring Syntax to use Spring tag library 1 &#60;%@taglib uri=&#34;http://www.springframework.org/tags&#34; prefix=&#34;spring&#34;&#62; [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><h2>Spring  Tag Library</h2>
<p><b>Spring MVC</b> 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.</p>
<ul>
<li>Read <a href="http://www.javabeat.net/articles/70-integrating-struts-with-spring-1.html">Integrating Struts With Spring</a></li>
</ul>
<h2>Syntax to use Spring tag library</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">	&lt;%@taglib  uri=&quot;http://www.springframework.org/tags&quot; prefix=&quot;spring&quot;&gt;</pre></td></tr></table></div>

<h2>htmlEscape and escapeBody tags </h2>
<p><b><spring:htmlEscape></b>-This tag sets the default HTML escape value for the current page.The default is &#8220;false&#8221;.We can also set a &#8220;defaultHtmlEscape&#8221; web.xml context-param.A page-level setting overrides a context-param</p>
<p><b><spring:escapeBody></b>-This tag is used to escape its enclosed body content, applying HTML escaping and/or JavaScript escaping. It is used to explicitly specify whether to apply HTMlL escaping or not. If not set, a page-level default (e.g. from the HtmlEscapeTag) or an application-wide default (the &#8220;defaultHtmlEscape&#8221; context-param in web.xml) is used.</p>
<h2>Example for <spring:htmlEscape> and <spring:escapeBody> tags</h2>
<p>1.Modify the <b>web.xml</b> to configure the <b>Dispatcher Servlet</b>.</p>
<p><b>web.xml</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&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.htm&lt;/welcome-file&gt;
        &lt;/welcome-file-list&gt;
    &lt;/web-app&gt;</pre></td></tr></table></div>

<p>2.Create an <b>dispatcher-servlet.xml</b> file which contains all the configuration beans to handle the user requests.It handles the user request and dispatches  to respective controllers.</p>
<p><b>dispatcher-servlet.xml</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&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;
&nbsp;
    &lt;bean class=&quot;org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping&quot;/&gt;
&nbsp;
&nbsp;
  &lt;bean id=&quot;urlMapping&quot; class=&quot;org.springframework.web.servlet.handler.SimpleUrlHandlerMapping&quot;&gt;
         &lt;property name=&quot;mappings&quot;&gt;
            &lt;props&gt;
                &lt;prop key=&quot;index.htm&quot;&gt;indexController&lt;/prop&gt;
            &lt;/props&gt;
        &lt;/property&gt;
    &lt;/bean&gt;
&nbsp;
    &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;
&nbsp;
    &lt;!--
   The Index Controller
    --&gt;
&nbsp;
	 &lt;bean name=&quot;indexController&quot;
          class=&quot;org.springframework.web.servlet.mvc.ParameterizableViewController&quot;
          p:viewName=&quot;index&quot; /&gt;
&nbsp;
&nbsp;
&lt;/beans&gt;</pre></td></tr></table></div>

<p>
3.Create a Jsp file <b>index.jsp</b> which contains the spring <b>htmlEscape</b> and <b>escapeBody</b> tags. In this example both tags are set with different cominations of values(&#8220;true&#8221;,&#8221;false&#8221;).</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p><b>index.jsp</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&nbsp;
&nbsp;
&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;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
   &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&nbsp;
&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;
&nbsp;
    &lt;body bgcolor=&quot;#DDDDDD&quot;&gt;
&nbsp;
          &lt;h3&gt;htmlEscape is false &lt;/h3&gt;
         &lt;h3&gt;escapeBody is false &lt;/h3&gt;
          &lt;spring:htmlEscape defaultHtmlEscape=&quot;false&quot;&gt;
               &lt;spring:escapeBody htmlEscape=&quot;false&quot;&gt;
&nbsp;
            &lt;h5&gt;  less than &lt; &lt;/h5&gt;
            &lt;h5&gt;  greater than &gt; &lt;/h5&gt;
              &lt;/spring:escapeBody&gt;
          &lt;/spring:htmlEscape&gt;
&nbsp;
 &lt;h3&gt;htmlEscape is false &lt;/h3&gt;
         &lt;h3&gt;escapeBody is true &lt;/h3&gt;
          &lt;spring:htmlEscape defaultHtmlEscape=&quot;false&quot;&gt;
               &lt;spring:escapeBody htmlEscape=&quot;true&quot;&gt;
&nbsp;
            &lt;h5&gt;  less than &lt; &lt;/h5&gt;
            &lt;h5&gt;  greater than &gt; &lt;/h5&gt;
              &lt;/spring:escapeBody&gt;
          &lt;/spring:htmlEscape&gt;
&nbsp;
 &lt;h3&gt;htmlEscape is true &lt;/h3&gt;
         &lt;h3&gt;escapeBody is false &lt;/h3&gt;
          &lt;spring:htmlEscape defaultHtmlEscape=&quot;true&quot;&gt;
               &lt;spring:escapeBody htmlEscape=&quot;false&quot;&gt;
&nbsp;
            &lt;h5&gt;  less than &lt; &lt;/h5&gt;
            &lt;h5&gt;  greater than &gt; &lt;/h5&gt;
              &lt;/spring:escapeBody&gt;
          &lt;/spring:htmlEscape&gt;
&nbsp;
 &lt;h3&gt;htmlEscape is true &lt;/h3&gt;
         &lt;h3&gt;escapeBody is true &lt;/h3&gt;
          &lt;spring:htmlEscape defaultHtmlEscape=&quot;true&quot;&gt;
               &lt;spring:escapeBody htmlEscape=&quot;true&quot;&gt;
&nbsp;
            &lt;h5&gt;  less than &lt; &lt;/h5&gt;
            &lt;h5&gt;  greater than &gt; &lt;/h5&gt;
              &lt;/spring:escapeBody&gt;
          &lt;/spring:htmlEscape&gt;
&nbsp;
    &lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

</p>
<p>4.Building and running the application</p>
<h4><b>Output</b></h4>
<p><b>Access page:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">http://localhost:8084/HtmlEscape/index.htm</pre></td></tr></table></div>

<p></b></p>
<p><img src="images/2010/11/spring-htmlEscape&amp;escapeBody/spring-htmlEscape&amp;escapeBody-1.jpg" width="585" height="350"></p>
<h2>Spring Framework Articles</h2>
<p align="justify">
<ul>
<li><a href="http://www.javabeat.net/articles/spring/1/">Spring Framework Articles</a></li>
<li>Buy <a href="http://astore.amazon.com/javabeat-20?_encoding=UTF8&amp;node=11" target="_blank">Spring Framework Books</a> from <a href="http://astore.amazon.com/javabeat-20" target="_blank">Java Books Store</a></li>
<li><a href="http://www.javabeat.net/articles/18-introduction-to-spring-mvc-web-framework-web-tier-1.html">Introduction to Spring MVC Web Framework &#8211; Web Tier</a> by <a href="http://www.javabeat.net/authors/Raja/">Raja</a></li>
<li><a href="http://www.javabeat.net/articles/42-integrating-spring-framework-with-hibernate-orm-framewo-1.html">Integrating Spring Framework with Hibernate ORM Framework</a> by <a href="http://www.javabeat.net/authors/Christy/">Christy</a></li>
<li><a href="http://www.javabeat.net/articles/71-introduction-to-spring-web-framework-1.html">Introduction to Spring Web Framework</a></li>
<li><a href="http://www.javabeat.net/articles/51-76%20introduction-to-springs-aspect-oriented-programminga-1.html">Introduction to Spring&#8217;s Aspect Oriented Programming(AOP)</a></li>
<li><a href="http://www.javabeat.net/articles/175-introduction-to-spring-web-services-1.html">Introduction to Spring Web Services</a></li>
</ul>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2010/11/spring-html-escape-and-escape-body-tags-springhtmlescape-and-springescapebody/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring BIND and NESTEDPATH Tags (&lt;spring:bind&gt; and &lt;spring:nestedPath&gt;)</title>
		<link>http://www.javabeat.net/2010/11/spring-bind-and-nestedpath-tags-springbind-and-springnestedpath/</link>
		<comments>http://www.javabeat.net/2010/11/spring-bind-and-nestedpath-tags-springbind-and-springnestedpath/#comments</comments>
		<pubDate>Mon, 29 Nov 2010 23:01:58 +0000</pubDate>
		<dc:creator>AbhilashEtikala</dc:creator>
				<category><![CDATA[Spring Framework]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/examples/?p=472</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>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. Read Integrating Struts With Spring Syntax to use Spring tag library 1 &#60;%@taglib uri=&#34;http://www.springframework.org/tags&#34; prefix=&#34;spring&#34;&#62; [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><h2>Spring  Tag Library</h2>
<p><b>Spring MVC</b> 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.</p>
<ul>
<li>Read <a href="http://www.javabeat.net/articles/70-integrating-struts-with-spring-1.html">Integrating Struts With Spring</a></li>
</ul>
<h2>Syntax to use Spring tag library</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">	&lt;%@taglib  uri=&quot;http://www.springframework.org/tags&quot; prefix=&quot;spring&quot;&gt;</pre></td></tr></table></div>

<h2>Bind and NestedPath tags </h2>
<p><b><spring:bind></b>-This tag provides support for the evaluation of the certain bean or bean property.The status may includes the actual value of the certain bean or bean property as well as possible errors and the expression  for the databinding functionality.</p>
<p><b><spring:nestedPath></b>-This tag provide support and assist with nested beans or bean properties.It exports a &#8220;nestedPath&#8221; variable of type String.<br />
The BindTag will automatically detect the nested path and automatically prepend it to its own path to form a complete path to the bean or bean property.<br />
This tag will also prepend any existing nested path that is currently set. Thus, you can nest multiple nested path tags.</p>
<h2>Example for <spring:bind> and <spring:nestedPath> tags</h2>
<p>1.Modify the <b>web.xml</b> to configure the <b>Dispatcher Servlet</b>.</p>
<p><b>web.xml</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&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.htm&lt;/welcome-file&gt;
        &lt;/welcome-file-list&gt;
    &lt;/web-app&gt;</pre></td></tr></table></div>

<p>2.Create an <b>dispatcher-servlet.xml</b> file which contains all the configuration beans to handle the user requests.It handles the user request and dispatches  to respective controllers.</p>
<p><b>dispatcher-servlet.xml</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&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;
&nbsp;
    &lt;bean class=&quot;org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping&quot;/&gt;
&nbsp;
&nbsp;
  &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.htm&quot;&gt;
&lt;ref bean=&quot;helloService&quot;/&gt;
&lt;/entry&gt;
&lt;/map&gt;
        &lt;/property&gt;
    &lt;/bean&gt;
&nbsp;
    &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;
&nbsp;
    &lt;!--
   The Index Controller
    --&gt;
        &lt;bean class=&quot;controller.HelloController&quot; id=&quot;helloService&quot;/&gt;
&lt;/beans&gt;</pre></td></tr></table></div>

<p>
3.Create a Jsp file for taking input from the user <b>nameView.jsp</b> which contains all the form fields with Spring <b>bind</b> tags.Later we set the FormView as this file to accept input.</p>
<p><b>nameView.jsp</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&nbsp;
&nbsp;
&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://www.springframework.org/tags/form&quot; prefix=&quot;form&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;
&nbsp;
&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;JSP Page&lt;/title&gt;
    &lt;/head&gt;
    &lt;body bgcolor=&quot;#DDDDDD&quot;&gt;
        &lt;h1&gt;Spring nestedpath and bind tag example&lt;/h1&gt;
            &lt;spring:nestedPath path=&quot;name&quot;&gt;
         Enter Your Details...
    &lt;form action=&quot;&quot; method=&quot;post&quot;&gt;
       Name:
        &lt;spring:bind path=&quot;name&quot;&gt;
            &lt;input type=&quot;text&quot; name=&quot;${status.expression}&quot; value=&quot;${status.value}&quot;&gt;&lt;br /&gt;
        &lt;/spring:bind&gt;
        Country:
          &lt;spring:bind path=&quot;country&quot;&gt;
            &lt;input type=&quot;text&quot; name=&quot;${status.expression}&quot; value=&quot;${status.value}&quot;&gt;&lt;br /&gt;
        &lt;/spring:bind&gt;
        Email:
         &lt;spring:bind path=&quot;email&quot;&gt;
            &lt;input type=&quot;text&quot; name=&quot;${status.expression}&quot; value=&quot;${status.value}&quot;&gt;
        &lt;/spring:bind&gt;
        &lt;input type=&quot;submit&quot; value=&quot;OK&quot;&gt;
    &lt;/form&gt;
&lt;/spring:nestedPath&gt;
&nbsp;
    &lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

</p>
<p>4.Create another Jsp file <b>helloView.jsp</b> which is a View for Spring to display the output. In this file we use Expression Language to display the details from as the bean properties.In Spring we can set the success view page from the controller.</p>
<p><b>helloView.jsp</b></p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot; %&gt;
&lt;%@page import=&quot;java.util.Enumeration&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;
&nbsp;
&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;JSP Page&lt;/title&gt;
    &lt;/head&gt;
   &lt;body bgcolor=&quot;#DDDDDD&quot;&gt;
    &lt;h1&gt;Spring nestedpath and bind tag example&lt;/h1&gt;
&nbsp;
    &lt;h3&gt;Your Details are...&lt;/h3&gt;
    &lt;h4&gt;${name}&lt;/h4&gt;
    &lt;h4&gt;${country}&lt;/h4&gt;
    &lt;h4&gt;${email}&lt;/h4&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>5.Create a Java class file <b>Name.java</b> this bean contains the 3 private variables to store the values which are binded using <b>bind tag</b>.Binding is associated with setting the bean property by using respective setter methods.Using getter methods we can retrieve the values in the controller.  </p>
<p><b>Name.java</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">public class Name {
&nbsp;
     private String name;
     private String country;
     private String email;
&nbsp;
    public String getCountry() {
        return country;
    }
&nbsp;
    public void setCountry(String country) {
        this.country = country;
    }
&nbsp;
    public String getEmail() {
        return email;
    }
&nbsp;
    public void setEmail(String email) {
        this.email = email;
    }
&nbsp;
    public String getName() {
        return name;
    }
&nbsp;
    public void setName(String name) {
        this.name = name;
    }
&nbsp;
}</pre></td></tr></table></div>

<p>6.Create a <b>HelloController.java</b> file which extends <b>SimpleFormController</b> to control the user request and return respective ModelAndView object. In this controller we can set the FormView,Success View and Bean as setCommand.</p>
<p><b>HelloController.java</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&nbsp;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
&nbsp;
public class HelloController extends SimpleFormController {
&nbsp;
    private HelloService helloService;
&nbsp;
    public void setHelloService(HelloService helloService) {
        this.helloService = helloService;
    }
    public HelloController() {
&nbsp;
        setCommandClass(Name.class);
        setCommandName(&quot;name&quot;);
        setSuccessView(&quot;helloView&quot;);
        setFormView(&quot;nameView&quot;);
    }
&nbsp;
&nbsp;
  @Override
protected ModelAndView onSubmit(Object command) throws Exception {
    Name name = (Name)command;
    ModelAndView mv = new ModelAndView(getSuccessView());
&nbsp;
    mv.addObject(&quot;name&quot;, name.getName());
    mv.addObject(&quot;country&quot;,name.getCountry());
    mv.addObject(&quot;email&quot;,name.getEmail());
&nbsp;
    return mv;
}
&nbsp;
}</pre></td></tr></table></div>

<p>7.Building and running the application</p>
<h4><b>Output</b></h4>
<p><b>Access page:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">http://localhost:8080/HelloSpring/index.html</pre></td></tr></table></div>

<p></b></p>
<p><img src="images/2010/11/spring-nestedPath&amp;bind/spring-nestedPath&amp;bind-1.jpg" width="585" height="224"></p>
<p><b>Success View page is displayed with the details</b></p>
<p><img src="images/2010/11/spring-nestedPath&amp;bind/spring-nestedPath&amp;bind-2.jpg" width="585" height="224"></p>
<h2>Spring Framework Articles</h2>
<p align="justify">
<ul>
<li><a href="http://www.javabeat.net/articles/spring/1/">Spring Framework Articles</a></li>
<li>Buy <a href="http://astore.amazon.com/javabeat-20?_encoding=UTF8&amp;node=11" target="_blank">Spring Framework Books</a> from <a href="http://astore.amazon.com/javabeat-20" target="_blank">Java Books Store</a></li>
<li><a href="http://www.javabeat.net/articles/18-introduction-to-spring-mvc-web-framework-web-tier-1.html">Introduction to Spring MVC Web Framework &#8211; Web Tier</a> by <a href="http://www.javabeat.net/authors/Raja/">Raja</a></li>
<li><a href="http://www.javabeat.net/articles/42-integrating-spring-framework-with-hibernate-orm-framewo-1.html">Integrating Spring Framework with Hibernate ORM Framework</a> by <a href="http://www.javabeat.net/authors/Christy/">Christy</a></li>
<li><a href="http://www.javabeat.net/articles/71-introduction-to-spring-web-framework-1.html">Introduction to Spring Web Framework</a></li>
<li><a href="http://www.javabeat.net/articles/51-76%20introduction-to-springs-aspect-oriented-programminga-1.html">Introduction to Spring&#8217;s Aspect Oriented Programming(AOP)</a></li>
<li><a href="http://www.javabeat.net/articles/175-introduction-to-spring-web-services-1.html">Introduction to Spring Web Services</a></li>
</ul>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2010/11/spring-bind-and-nestedpath-tags-springbind-and-springnestedpath/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring MESSAGE and THEME Tags (&lt;spring:message&gt; and &lt;spring:theme&gt;)</title>
		<link>http://www.javabeat.net/2010/11/spring-message-and-theme-tags-springmessage-and-springtheme/</link>
		<comments>http://www.javabeat.net/2010/11/spring-message-and-theme-tags-springmessage-and-springtheme/#comments</comments>
		<pubDate>Mon, 29 Nov 2010 23:01:05 +0000</pubDate>
		<dc:creator>AbhilashEtikala</dc:creator>
				<category><![CDATA[Spring Framework]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/examples/?p=470</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>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. Read Integrating Struts With Spring Syntax to use Spring tag library 1 &#60;%@taglib uri=&#34;http://www.springframework.org/tags&#34; prefix=&#34;spring&#34;&#62; [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><h2>Spring  Tag Library</h2>
<p><b>Spring MVC</b> 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.</p>
<ul>
<li>Read <a href="http://www.javabeat.net/articles/70-integrating-struts-with-spring-1.html">Integrating Struts With Spring</a></li>
</ul>
<h2>Syntax to use Spring tag library</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">	&lt;%@taglib  uri=&quot;http://www.springframework.org/tags&quot; prefix=&quot;spring&quot;&gt;</pre></td></tr></table></div>

<h2>Message and Theme tags </h2>
<p><b><spring:message></b>-This tag provides internationalization support using MessageSource interface which provides functionality to retrieve messages.If &#8220;code&#8221; isn&#8217;t set or cannot be resolved, &#8220;text&#8221; will be used as default message.</p>
<p><b><spring:theme></b>-This tag is used to retrieve a theme message in the scope of the current page.Messages are looked up using the ApplicationContext&#8217;s ThemeSource.</p>
<h2>Example for <spring:message> and <spring:theme> tags</h2>
<p>1.Modify the <b>web.xml</b> to configure the <b>Dispatcher Servlet</b>.</p>
<p><b>web.xml</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&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.htm&lt;/welcome-file&gt;
        &lt;/welcome-file-list&gt;
    &lt;/web-app&gt;</pre></td></tr></table></div>

<p>2.Create an <b>dispatcher-servlet.xml</b> file which contains all the configuration beans to handle the user requests.It handles the user request and dispatches  to respective controllers.</p>
<p>Here create two beans which are instances of <b>ResourceBundleThemeSource</b> and <b>ResourceBundleMessageSource</b> class loads the property files.</p>
<p><b>dispatcher-servlet.xml</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&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;
&nbsp;
    &lt;bean class=&quot;org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping&quot;/&gt;
&nbsp;
 &lt;bean id=&quot;themeSource&quot; class=&quot;org.springframework.ui.context.support.ResourceBundleThemeSource&quot;&gt;
&lt;/bean&gt;
&nbsp;
	&lt;bean id=&quot;messageSource&quot; class=&quot;org.springframework.context.support.ResourceBundleMessageSource&quot;&gt;
        &lt;property name=&quot;basename&quot; value=&quot;messages&quot;/&gt;
		&lt;/bean&gt;
&nbsp;
  &lt;bean id=&quot;urlMapping&quot; class=&quot;org.springframework.web.servlet.handler.SimpleUrlHandlerMapping&quot;&gt;
         &lt;property name=&quot;mappings&quot;&gt;
            &lt;props&gt;
                &lt;prop key=&quot;index.htm&quot;&gt;indexController&lt;/prop&gt;
            &lt;/props&gt;
        &lt;/property&gt;
    &lt;/bean&gt;
&nbsp;
    &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;
&nbsp;
    &lt;!--
   The Index Controller
    --&gt;
&nbsp;
	 &lt;bean name=&quot;indexController&quot;
          class=&quot;org.springframework.web.servlet.mvc.ParameterizableViewController&quot;
          p:viewName=&quot;index&quot; /&gt;
&nbsp;
&nbsp;
&lt;/beans&gt;</pre></td></tr></table></div>

<p>
3.Create a Jsp file <b>index.jsp</b> which contains the spring message and theme tag.</p>
<p><b>index.jsp</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&nbsp;
&nbsp;
&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;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
   &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&nbsp;
&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;link rel=&quot;stylesheet&quot;  href='&lt;spring:theme code='default'/&gt;' type=&quot;text/css&quot;/&gt;
&nbsp;
   &lt;/head&gt;
&nbsp;
    &lt;body &gt;
&nbsp;
       &lt;h2&gt;Hello this is the blue theme&lt;/h2&gt;
       &lt;spring:message code=&quot;message.header&quot;/&gt;
&nbsp;
    &lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

</p>
<p>4.Create properties file <b>theme.properties </b> which contains the css file location. Here we used two keys &#8216;default&#8217;,'blue&#8217; and their corresponding css files.</p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">default=themes/default.css
blue=themes/blue.css</pre></td></tr></table></div>

</p>
<p>5.Create properties file <b>messages.properties </b> which contains the messages with their corresponding keys. These messages are retrieved from the page using <b>spring:message </b> tag.</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">message.header=Welcome to Java Beat</pre></td></tr></table></div>

</p>
<p>6.Creating two Style Sheets <b>default.css</b> and <b>blue.css </b> these style sheets are located from the properties file and gets loaded to the currnet page when request is made. <b> spring:theme </b> tag can be used to retrieve the theme.</p>
<p><b>default.css</b></p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">body{
    background-color:#DDDDDD;
}</pre></td></tr></table></div>

</p>
<p><b>blue.css</b></p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">body{
    background-color:#3333FF;
}</pre></td></tr></table></div>

</p>
<p>7.Building and running the application</p>
<h4><b>Output</b></h4>
<p><b>Access page:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">http://localhost:8084/SpringTheme/index.htm</pre></td></tr></table></div>

<p></b></p>
<p><img src="images/2010/11/spring-theme&amp;message/spring-theme&amp;message-1.jpg" width="585" height="224"></p>
<p><b>Blue background style.</b></p>
<p><img src="images/2010/11/spring-theme&amp;message/spring-theme&amp;message-2.jpg" width="585" height="224"></p>
<h2>Spring Framework Articles</h2>
<p align="justify">
<ul>
<li><a href="http://www.javabeat.net/articles/spring/1/">Spring Framework Articles</a></li>
<li>Buy <a href="http://astore.amazon.com/javabeat-20?_encoding=UTF8&amp;node=11" target="_blank">Spring Framework Books</a> from <a href="http://astore.amazon.com/javabeat-20" target="_blank">Java Books Store</a></li>
<li><a href="http://www.javabeat.net/articles/18-introduction-to-spring-mvc-web-framework-web-tier-1.html">Introduction to Spring MVC Web Framework &#8211; Web Tier</a> by <a href="http://www.javabeat.net/authors/Raja/">Raja</a></li>
<li><a href="http://www.javabeat.net/articles/42-integrating-spring-framework-with-hibernate-orm-framewo-1.html">Integrating Spring Framework with Hibernate ORM Framework</a> by <a href="http://www.javabeat.net/authors/Christy/">Christy</a></li>
<li><a href="http://www.javabeat.net/articles/71-introduction-to-spring-web-framework-1.html">Introduction to Spring Web Framework</a></li>
<li><a href="http://www.javabeat.net/articles/51-76%20introduction-to-springs-aspect-oriented-programminga-1.html">Introduction to Spring&#8217;s Aspect Oriented Programming(AOP)</a></li>
<li><a href="http://www.javabeat.net/articles/175-introduction-to-spring-web-services-1.html">Introduction to Spring Web Services</a></li>
</ul>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2010/11/spring-message-and-theme-tags-springmessage-and-springtheme/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring Framework FORM  Tags</title>
		<link>http://www.javabeat.net/2010/11/spring-framework-form-tags/</link>
		<comments>http://www.javabeat.net/2010/11/spring-framework-form-tags/#comments</comments>
		<pubDate>Tue, 09 Nov 2010 21:45:41 +0000</pubDate>
		<dc:creator>AbhilashEtikala</dc:creator>
				<category><![CDATA[Spring Framework]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/examples/?p=468</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>Spring FORM 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. Read Integrating Struts With Spring Syntax to use Spring Form tag library 1 &#60;%@taglib [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><h2>Spring FORM Tag Library</h2>
<p><b>Spring MVC</b> 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.</p>
<ul>
<li>Read <a href="http://www.javabeat.net/articles/70-integrating-struts-with-spring-1.html">Integrating Struts With Spring</a></li>
</ul>
<h2>Syntax to use Spring Form  tag library</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">	&lt;%@taglib  uri=&quot;http://www.springframework.org/tags/form&quot; prefix=&quot;form&quot;&gt;</pre></td></tr></table></div>

<h2>Form tags used in this example</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;p&gt;&lt;b&gt;&lt;form:form&gt;&lt;/b&gt;-Renders an HTML 'form' tag and exposes a binding path to inner tags for binding.
&lt;p&gt;&lt;b&gt;&lt;form:input&gt;&lt;/b&gt;-Renders an HTML 'input' tag with type 'text' using the bound value.
&lt;p&gt;&lt;b&gt;&lt;form:errors&gt;&lt;/b&gt;-Renders field errors in an HTML 'span' tag.
&lt;p&gt;&lt;b&gt;&lt;form:password&gt;&lt;/b&gt;-Renders an HTML 'input' tag with type 'password' using the bound value.
&lt;p&gt;&lt;b&gt;&lt;form:radiobutton&gt;&lt;/b&gt;-Renders an HTML 'input' tag with type 'radio'.
&lt;p&gt;&lt;b&gt;&lt;form:select&gt;&lt;/b&gt;-Renders an HTML 'select' element. Supports databinding to the selected option.
&lt;p&gt;&lt;b&gt;&lt;form:option&gt;&lt;/b&gt;-Renders a single HTML 'option'. Sets 'selected' as appropriate based on bound value.
&lt;p&gt;&lt;b&gt;&lt;form:textarea&gt;&lt;/b&gt;-Renders an HTML 'textarea'.
&lt;p&gt;&lt;b&gt;&lt;form:checkbox&gt;&lt;/b&gt;-Renders an HTML 'input' tag with type 'checkbox'.</pre></td></tr></table></div>

<h2>Simple Registration Form Example</h2>
<p>1.Modify the <b>web.xml</b> to configure the <b>Dispatcher Servlet</b>.</p>
<p><b>web.xml</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&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;*.html&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.jsp&lt;/welcome-file&gt;
        &lt;/welcome-file-list&gt;
    &lt;/web-app&gt;</pre></td></tr></table></div>

<p>2.Create an <b>dispatcher-servlet.xml</b> file which contains all the configuration beans to handle the user requests.It handles the user request and dispatches  to respective controllers.</p>
<p><b>dispatcher-servlet.xml</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&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;
&nbsp;
    &lt;bean class=&quot;org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping&quot;/&gt;
&nbsp;
&nbsp;
    &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;registrationController&quot;/&gt;
&lt;/entry&gt;
&lt;/map&gt;
&lt;/property&gt;
&nbsp;
 &lt;/bean&gt;
&nbsp;
&nbsp;
&lt;bean id=&quot;registrationValidator&quot; class=&quot;registrationValidator&quot;/&gt;
&lt;bean id=&quot;registrationController&quot; class=&quot;RegistrationFormController&quot;&gt;
&nbsp;
&lt;property name=&quot;sessionForm&quot;&gt;&lt;value&gt;false&lt;/value&gt;&lt;/property&gt;
&nbsp;
&lt;property name=&quot;commandName&quot; value=&quot;registration&quot;&gt;&lt;/property&gt;
&nbsp;
&lt;property name=&quot;commandClass&quot; value=&quot;Registration&quot;&gt;&lt;/property&gt;
&nbsp;
&lt;property name=&quot;validator&quot;&gt;&lt;ref bean=&quot;registrationValidator&quot;/&gt;&lt;/property&gt;
&lt;property name=&quot;formView&quot;&gt;&lt;value&gt;index&lt;/value&gt;&lt;/property&gt;
&nbsp;
&lt;property name=&quot;successView&quot;&gt;&lt;value&gt;success&lt;/value&gt;&lt;/property&gt;
&nbsp;
&lt;/bean&gt;
&nbsp;
&lt;bean id=&quot;messageSource&quot; class=&quot;org.springframework.context.support.ResourceBundleMessageSource&quot;&gt;
        &lt;property name=&quot;basename&quot; value=&quot;messages&quot;/&gt;
    &lt;/bean&gt;
&nbsp;
     &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;/beans&gt;</pre></td></tr></table></div>

<p>
3.Create a Jsp file for taking input from the user <b>index.jsp</b> which contains all the form fields with Spring Form tags.</p>
<p><b>index.jsp</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&nbsp;
&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@taglib  uri=&quot;http://www.springframework.org/tags/form&quot; prefix=&quot;form&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;
&nbsp;
&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;
&nbsp;
    &lt;body bgcolor=&quot;DDDDDD&quot;&gt;
        &lt;h1&gt;Spring Form tags Example&lt;/h1&gt;
        &lt;form:form commandName=&quot;registration&quot; method=&quot;POST&quot;&gt;
&nbsp;
           User Name: &lt;form:input path=&quot;username&quot;/&gt;&lt;font color=&quot;red&quot;&gt;
               &lt;form:errors path=&quot;username&quot;/&gt;&lt;/font&gt;&lt;br /&gt;
           Password: &lt;form:password path=&quot;password&quot;/&gt;&lt;font color=&quot;red&quot;&gt;
           &lt;form:errors path=&quot;password&quot;/&gt;&lt;/font&gt;&lt;br /&gt;
           First Name: &lt;form:input path=&quot;fname&quot;/&gt;&lt;br /&gt;
           Last Name: &lt;form:input path=&quot;lname&quot;/&gt;&lt;br /&gt;
           Gender: &lt;form:radiobutton path=&quot;gender&quot; value=&quot;male&quot;/&gt;Male
               &lt;form:radiobutton path=&quot;gender&quot; value=&quot;female&quot;/&gt;Female&lt;br /&gt;
                   Country :&lt;form:select path=&quot;country&quot; &gt;
                       &lt;form:option value=&quot;india&quot;&gt;India&lt;/form:option&gt;
                       &lt;form:option value=&quot;india&quot;&gt;USA&lt;/form:option&gt;
                       &lt;form:option value=&quot;india&quot;&gt;Australia&lt;/form:option&gt;
                   &lt;/form:select&gt;&lt;br /&gt;
                   Address: &lt;form:textarea path=&quot;addr&quot;/&gt;&lt;br /&gt;
                       Select any :&lt;form:checkbox path=&quot;cb&quot; value=&quot;checkbox1&quot;/&gt;
                       Check Box1
                       &lt;form:checkbox path=&quot;cb&quot; value=&quot;checkbox2&quot;/&gt;
                       Check Box2&lt;br /&gt;
&nbsp;
            &lt;input type=&quot;submit&quot; value=&quot;submit&quot;/&gt;
        &lt;/form:form&gt;
&nbsp;
    &lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

</p>
<p>4.Create another Jsp file <b>success.jsp</b> which is a View for Spring to display the output. In this file we use Expression Language to display the details.</p>
<p><b>success.jsp</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot; %&gt;
&lt;%@page import=&quot;java.util.Enumeration&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;
&nbsp;
&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;Spring Form Tags Example&lt;/title&gt;
    &lt;/head&gt;
    &lt;body bgcolor=&quot;DDDDDD&quot;&gt;
        &lt;h1&gt;Spring Form tags examples&lt;/h1&gt;
        &lt;h2&gt;
         User Name: ${uname}&lt;br /&gt;
         First Name: ${fname}&lt;br /&gt;
         Last Name: ${lname}&lt;br /&gt;
         Gender: ${gender}&lt;br /&gt;
         Country: ${country}&lt;br /&gt;
         Address: ${addr}&lt;br /&gt;
         Selected Check box: ${cb}
&lt;/h2&gt;
&nbsp;
    &lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>5.Create a Java class file <b>Registration.java</b> which contains the business logic for registration application. Here this file contains 8 private variables with their respective getter and setter methods to store the details for registration. </p>
<p><b>Registration.java</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">public class Registration {
&nbsp;
    private String username;
    private String password;
    private String fname;
    private String lname;
    private String gender;
    private String country;
    private String addr;
    private String cb;
&nbsp;
    public String getAddr() {
        return addr;
    }
&nbsp;
    public void setAddr(String addr) {
        this.addr = addr;
    }
&nbsp;
    public String getCb() {
        return cb;
    }
&nbsp;
    public void setCb(String cb) {
        this.cb = cb;
    }
&nbsp;
    public String getCountry() {
        return country;
    }
&nbsp;
    public void setCountry(String country) {
        this.country = country;
    }
&nbsp;
    public String getGender() {
        return gender;
    }
&nbsp;
    public void setGender(String gender) {
        this.gender = gender;
    }
&nbsp;
    public Registration() {
    }
&nbsp;
    public String getFname() {
        return fname;
    }
&nbsp;
    public void setFname(String fname) {
        this.fname = fname;
    }
&nbsp;
    public String getLname() {
        return lname;
    }
&nbsp;
    public void setLname(String lname) {
        this.lname = lname;
    }
&nbsp;
    public String getPassword() {
        return password;
    }
&nbsp;
    public void setPassword(String password) {
        this.password = password;
    }
&nbsp;
    public String getUsername() {
        return username;
    }
&nbsp;
    public void setUsername(String username) {
        this.username = username;
    }
&nbsp;
}</pre></td></tr></table></div>

<p>6.Create a <b>RegistrationFormController.java</b> file which extends <b>SimpleFormController</b> to control the user request and return respective ModelAndView object.</p>
<p><b>RegistrationFormController.java</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&nbsp;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
&nbsp;
public class RegistrationFormController extends SimpleFormController {
&nbsp;
@Override
    protected ModelAndView onSubmit(Object command) throws Exception {
&nbsp;
        Registration reg=(Registration)command;
&nbsp;
        String uname=reg.getUsername();
        String fname=reg.getFname();
        String lname=reg.getLname();
&nbsp;
        String gender=reg.getGender();
        String country=reg.getCountry();
        String cb=reg.getCb();
        String addr=reg.getAddr();
&nbsp;
         ModelAndView mv = new ModelAndView(getSuccessView());
&nbsp;
          mv.addObject(&quot;uname&quot;,uname);
          mv.addObject(&quot;fname&quot;,fname);
          mv.addObject(&quot;lname&quot;,lname);
          mv.addObject(&quot;gender&quot;,gender);
          mv.addObject(&quot;country&quot;,country);
          mv.addObject(&quot;cb&quot;,cb);
          mv.addObject(&quot;addr&quot;,addr);
&nbsp;
        return mv;
    }
&nbsp;
}</pre></td></tr></table></div>

<p>7.Create a <b>registrationValidator.java</b> file to validate the form fields like username and password should not be empty.DispatcherServlet is responsible to give property to add Validator to the user request and perform validation.</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p><b>registrationValidator.java</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
&nbsp;
public class registrationValidator implements Validator
{
&nbsp;
    public boolean supports(Class cl) {
        return Registration.class.isAssignableFrom(cl);
&nbsp;
    }
&nbsp;
    public void validate(Object ob, Errors errors) {
       Registration reg=(Registration)ob;
       if (reg.getUsername() == null || reg.getUsername().length() == 0) {
            errors.rejectValue(&quot;username&quot;, &quot;error.empty.username&quot;);
        }
&nbsp;
       else if (reg.getPassword() == null || reg.getPassword().length() == 0) {
            errors.rejectValue(&quot;password&quot;, &quot;error.empty.password&quot;);
        }
&nbsp;
    }
&nbsp;
}</pre></td></tr></table></div>

<p>8.Create or Modify <b>messages.properties</b> file which contains the messages for their respective keys.In this file we write messages for two keys empty username and empty password.</p>
<p><b>messages.properties</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">error.empty.username=Please Enter User name
error.empty.password=Please Enter Password</pre></td></tr></table></div>

<p>9.Building and running the application</p>
<h4><b>Output</b></h4>
<p><b>Access page:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">http://localhost:8080/Registration/index.html</pre></td></tr></table></div>

<p></b></p>
<p><img src="images/2010/11/spring-form/spring-form-1.jpg" width="585" height="224"></p>
<p><b>When password is emtpy respective error message is displayed using spring errors tag</b></p>
<p><img src="images/2010/11/spring-form/spring-form-2.jpg" width="585" height="224"></p>
<p><b>We can submit the form only after the correct validation of username and password.</b></p>
<p><img src="images/2010/11/spring-form/spring-form-3.jpg" width="585" height="224"></p>
<p><b>Output is displayed with all the values submitted for registration</b></p>
<p><img src="images/2010/11/spring-form/spring-form-4.jpg" width="585" height="224"></p>
<h2>Spring Framework Articles</h2>
<p align="justify">
<ul>
<li><a href="http://www.javabeat.net/articles/spring/1/">Spring Framework Articles</a></li>
<li>Buy <a href="http://astore.amazon.com/javabeat-20?_encoding=UTF8&amp;node=11" target="_blank">Spring Framework Books</a> from <a href="http://astore.amazon.com/javabeat-20" target="_blank">Java Books Store</a></li>
<li><a href="http://www.javabeat.net/articles/18-introduction-to-spring-mvc-web-framework-web-tier-1.html">Introduction to Spring MVC Web Framework &#8211; Web Tier</a> by <a href="http://www.javabeat.net/authors/Raja/">Raja</a></li>
<li><a href="http://www.javabeat.net/articles/42-integrating-spring-framework-with-hibernate-orm-framewo-1.html">Integrating Spring Framework with Hibernate ORM Framework</a> by <a href="http://www.javabeat.net/authors/Christy/">Christy</a></li>
<li><a href="http://www.javabeat.net/articles/71-introduction-to-spring-web-framework-1.html">Introduction to Spring Web Framework</a></li>
<li><a href="http://www.javabeat.net/articles/51-76%20introduction-to-springs-aspect-oriented-programminga-1.html">Introduction to Spring&#8217;s Aspect Oriented Programming(AOP)</a></li>
<li><a href="http://www.javabeat.net/articles/175-introduction-to-spring-web-services-1.html">Introduction to Spring Web Services</a></li>
</ul>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2010/11/spring-framework-form-tags/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Struts LOGIC messagesPresent and messagesNotPresent Tag ( &lt; logic:messagesPresent &gt;  and &lt; logic:messagesNotPresent &gt;)</title>
		<link>http://www.javabeat.net/2010/10/struts-logic-messagespresent-and-messagesnotpresent-tag-logicmessagespresent-and-logicmessagesnotpresent/</link>
		<comments>http://www.javabeat.net/2010/10/struts-logic-messagespresent-and-messagesnotpresent-tag-logicmessagespresent-and-logicmessagesnotpresent/#comments</comments>
		<pubDate>Tue, 19 Oct 2010 03:14:57 +0000</pubDate>
		<dc:creator>AbhilashEtikala</dc:creator>
				<category><![CDATA[Struts]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/examples/?p=466</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>Struts LOGIC Tag Library Struts LOGIC tag library provides tags that are useful in managing conditional generation of output text, looping over object collections for repetitive generation of output text, and application flow management. Read Integrating Struts With Spring Syntax to use Struts LOGIC tag library 1 &#60;code&#62;&#38;amp;lt%@ taglib prefix=&#34;logic&#34; uri=&#34;http://struts.apache.org/tags-logic&#34; %&#38;amp;gt &#38;lt logic:messagesPresent &#38;gt [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><h2>Struts LOGIC Tag Library</h2>
<p>Struts <b>LOGIC tag library</b> provides tags that are useful in managing conditional generation of output text, looping over object collections for repetitive generation of output text, and application flow management. </p>
<ul>
<li>Read <a href="http://www.javabeat.net/articles/70-integrating-struts-with-spring-1.html">Integrating Struts With Spring</a></li>
</ul>
<h2>Syntax to use Struts LOGIC  tag library</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">	&lt;code&gt;&amp;amp;lt%@ taglib prefix=&quot;logic&quot; uri=&quot;http://struts.apache.org/tags-logic&quot; %&amp;amp;gt</pre></td></tr></table></div>

<h2>&amp;lt logic:messagesPresent &amp;gt and &amp;lt logic:messagesNotPresent&amp;gt</h2>
<p><b>&amp;lt logic:messagesPresent &amp;gt	</b>-This tag is used if an <i>ActionMessages object, ActionErrors object, a String, or a String array</i> is <b>present</b> in any scope the nested body content of this tag is evaluated.  </p>
<p><b>&amp;lt logic:messagesNotPresent &amp;gt	</b>- This tag is used if an <i>ActionMessages object, ActionErrors object, a String, or a String array</i> is <b>not present</b> in any scope the nested body content of this tag is evaluated.</p>
<p>By default both the tags will retrieve the bean it will iterate over from the Globals.ERROR_KEY constant string, but if <i>message</i> attribute is set to &#8216;true&#8217; the bean will be retrieved from the Globals.MESSAGE_KEY constant string.If <i>message</i> attribute  is set to &#8216;true&#8217;, any value assigned to the name attribute will be ignored.</p>
<h2>Example Code for &amp;lt logic:messagesPresent &amp;gt and &amp;lt logic:messagesNotPresent&amp;gt</h2>
<p>1.Create of Modify the <b>index.jsp page</b> which is the welcome page for the users.It forwards to the action class.</p>
<p><b>index.jsp</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&amp;lt;%@page contentType=&quot;text/html&quot;%&amp;gt;
&amp;lt;%@page pageEncoding=&quot;UTF-8&quot;%&amp;gt;
&nbsp;
&amp;lt;jsp:forward page=&quot;logicmessage.do&quot;/&amp;gt;</pre></td></tr></table></div>

<p>2.Create an Jsp page and name it as <b>LogicMessageTag.jsp</b>.It is the output page for user which contains the logic tags to to check whether the page contains any error messages or not.</p>
<p><b>LogicMessageTag.jsp</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&amp;lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&amp;gt;
&amp;lt;%@taglib  uri=&quot;http://struts.apache.org/tags-logic&quot; prefix=&quot;logic&quot; %&amp;gt;
&amp;lt;%@taglib uri=&quot;http://struts.apache.org/tags-bean&quot; prefix=&quot;bean&quot; %&amp;gt;
&nbsp;
&amp;lt;html&amp;gt;
    &amp;lt;head&amp;gt;
        &amp;lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&amp;gt;
        &amp;lt;title&amp;gt;Struts Logic Message Present not Present Tag&amp;lt;/title&amp;gt;
    &amp;lt;/head&amp;gt;
    &amp;lt;body bgcolor=&quot;DDDDDD&quot;&amp;gt;
        &amp;lt;h1&amp;gt;struts logic:messagesPresent and logic:messagesNotPresent tag&amp;lt;/h1&amp;gt;
        &amp;lt;logic:messagesPresent &amp;gt;
	&amp;lt;h4&amp;gt;There are errors on this page!
    The error message is
    &amp;lt;bean:message key=&quot;common.errors.text&quot;/&amp;gt;
    &amp;lt;/h4&amp;gt;
&amp;lt;/logic:messagesPresent&amp;gt;
&amp;lt;logic:messagesNotPresent message=&quot;true&quot;&amp;gt;
    &amp;lt;h4&amp;gt;There are no errors messages on this page!&amp;lt;/h4&amp;gt;
&amp;lt;/logic:messagesNotPresent&amp;gt;
&nbsp;
    &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;</pre></td></tr></table></div>

<p>3.Create a Form bean.Form bean is used to hold the properties of the submitted form which is a sub class of ActionForm.It contains only one property of type String to hold the text.This form contains the validate method to add error messages to the request page.</p>
<ul>
<li>Buy <a href="http://astore.amazon.com/javabeat-20?_encoding=UTF8&amp;node=13" target="_blank">Struts Books</a> from <a href="http://astore.amazon.com/javabeat-20" target="_blank">Java Books Store</a></li>
<p>.
	</ul>
</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p><b>LogicMessageForm.java</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
&nbsp;
public class LogicMessageForm extends org.apache.struts.action.ActionForm {
&nbsp;
    private String text;
&nbsp;
    public String getText() {
        return text;
    }
&nbsp;
    public void setText(String text) {
        this.text = text;
    }
&nbsp;
&nbsp;
    public LogicMessageForm() {
        super();
&nbsp;
    }
&nbsp;
&nbsp;
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
&nbsp;
         ActionErrors errors = new ActionErrors();
&nbsp;
	   errors.add(&quot;common.errors.text&quot;,
		new ActionMessage(&quot;common.errors.text&quot;));
        return errors;
    }
}</pre></td></tr></table></div>

<p>4.<b>ApplicationResource.properties</b> file contains the messages and their corresponding keys.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">common.errors.text=Text Required</pre></td></tr></table></div>

<p>5.Simple Action class <b>LogicMessageAction.java</b> which is a sub class of <i>Action</i> class used to process the user&#8217;s request. </p>
<p><b>LogicMessageAction.java</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
&nbsp;
public class LogicMessageAction extends org.apache.struts.action.Action {
&nbsp;
&nbsp;
    private final static String SUCCESS = &quot;success&quot;;
&nbsp;
&nbsp;
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
&nbsp;
        return mapping.findForward(SUCCESS);
    }
}</pre></td></tr></table></div>

<p>6.Create or modify struts config file <b>struts-config.xml</b> with action mappings.Struts-config file contains the information about the configuration of the struts framework to the application.It contains the action mappings which helps to select Action,ActionForm and other information for specific user&#8217;s request&#8217;s.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&nbsp;
&amp;amp;lt?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&amp;amp;gt
&nbsp;
&amp;amp;lt!DOCTYPE struts-config PUBLIC
          &quot;-//Apache Software Foundation//DTD Struts Configuration 1.2//EN&quot;
          &quot;http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd&quot;&amp;amp;gt
&nbsp;
&amp;amp;lt struts-config&amp;amp;gt
    &amp;amp;lt form-beans&amp;amp;gt
        &amp;amp;lt form-bean name=&quot;LogicMessageForm&quot; type=&quot;com.myapp.struts.LogicMessageForm&quot;/&amp;amp;gt
    &amp;amp;lt/form-beans&amp;amp;gt
&nbsp;
&amp;amp;lt action-mappings&amp;amp;gt
        &amp;amp;lt action input=&quot;/LogicMessageTag.jsp&quot; name=&quot;LogicMessageForm&quot; path=&quot;/logicmessage&quot; scope=&quot;request&quot; validate=&quot;true&quot; type=&quot;com.myapp.struts.LogicMessageAction&quot;&amp;amp;gt
        &amp;amp;lt forward name=&quot;success&quot; path=&quot;/LogicMessageTag.jsp&quot;/&amp;amp;gt
&nbsp;
            &amp;amp;lt/action&amp;amp;gt
&amp;amp;lt/action-mappings&amp;amp;gt
     &amp;lt;message-resources parameter=&quot;com/myapp/struts/ApplicationResource&quot;/&amp;gt;
	&amp;amp;lt/struts-config&amp;amp;gt</pre></td></tr></table></div>

<p>7.Building and running the application</p>
<h4><b>Output</b></h4>
<p><b>Access page:
<pre><code>http://localhost:8080/logicmessage/</code></pre>
<p></b></p>
<p><img src="images/2010/10/struts-logicmessage/struts-logicmessage-1.jpg" width="585" height="204"></p>
<h2>Struts Framework Articles</h2>
<ul>
<li>Buy <a href="http://astore.amazon.com/javabeat-20?_encoding=UTF8&amp;node=13" target="_blank">Struts Books</a> from <a href="http://astore.amazon.com/javabeat-20" target="_blank">Java Books Store</a></li>
<li><a href="http://www.javabeat.net/articles/67-struts-20-introduction-and-validations-using-annotatio-1.html">Struts 2.0 Introduction and Validations using Annotations</a></li>
<li><a href="http://www.javabeat.net/articles/24-introduction-to-struts-actions-1.html">Introduction to Struts Actions</a></li>
<li><a href="http://www.javabeat.net/articles/69-whats-new-in-struts-20-struts-20-framework-1.html">What&#8217;s new in Struts 2.0?</a></li>
<li><a href="http://www.javabeat.net/articles/132-internationalization-and-taglibs-in-struts-12-1.html">Internationalization and Taglibs in Struts 1.2</a></li>
<p>	</u></p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2010/10/struts-logic-messagespresent-and-messagesnotpresent-tag-logicmessagespresent-and-logicmessagesnotpresent/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Struts LOGIC match and notMatch Tag ( &lt; logic:match &gt;  and &lt; logic:notMatch &gt;)</title>
		<link>http://www.javabeat.net/2010/10/struts-logic-match-and-notmatch-tag-logicmatch-and-logicnotmatch/</link>
		<comments>http://www.javabeat.net/2010/10/struts-logic-match-and-notmatch-tag-logicmatch-and-logicnotmatch/#comments</comments>
		<pubDate>Mon, 18 Oct 2010 03:14:21 +0000</pubDate>
		<dc:creator>AbhilashEtikala</dc:creator>
				<category><![CDATA[Struts]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/examples/?p=464</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>Struts LOGIC match and notMatch Tag ( &#60; logic:match &#62; and &#60; logic:notMatch &#62;) Struts LOGIC Tag Library Struts LOGIC tag library provides tags that are useful in managing conditional generation of output text, looping over object collections for repetitive generation of output text, and application flow management. Read Integrating Struts With Spring Syntax to [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><h1>Struts LOGIC match and notMatch Tag ( &lt; logic:match &gt;  and &lt; logic:notMatch &gt;) </h1>
<h2>Struts LOGIC Tag Library</h2>
<p>Struts <b>LOGIC tag library</b> provides tags that are useful in managing conditional generation of output text, looping over object collections for repetitive generation of output text, and application flow management. </p>
<ul>
<li>Read <a href="http://www.javabeat.net/articles/70-integrating-struts-with-spring-1.html">Integrating Struts With Spring</a></li>
</ul>
<h2>Syntax to use Struts LOGIC  tag library</h2>
<pre>
	<code>&amp;lt%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic" %&amp;gt</code>
</pre>
<h2>&amp;lt logic:match &amp;gt and &amp;lt logic:notMatch&amp;gt</h2>
<p><b>&amp;lt logic:match &amp;gt	</b>-This tag matches the selector attributes(String) by specified constant variable.If the value <b>is a substring</b> (appropriately limited by the <i>location</i> attribute), the nested body content of this tag is evaluated  </p>
<p><b>&amp;lt logic:notMatch &amp;gt	</b>- This tag macthes between the selector attributes(String) by specified constant variable. If the value <b>is not a substring</b> (appropriately limited by the <i>location</i> attribute), the nested body content of this tag is evaluated.</p>
<h2>Example Code for &amp;lt logic:match &amp;gt and &amp;lt logic:notMatch&amp;gt</h2>
<p>1.Create of Modify the <b>index.jsp page</b> whic is the welcome page for the users.It displays a Textbox nested inside the form where the text is to be entered.</p>
<p><b>index.jsp</b></p>
<pre><code>
&amp;lt %@page contentType="text/html" pageEncoding="UTF-8"%&amp;gt
&amp;lt %@taglib  uri="http://struts.apache.org/tags-html" prefix="html" %&amp;gt
&amp;lt !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd"&amp;gt

&amp;lt html&amp;gt
    &amp;lt head&amp;gt
        &amp;lt meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&amp;gt
        &amp;lt title&amp;gt Struts Match Tag Example&amp;lt /title&amp;gt
    &amp;lt /head&amp;gt
    &amp;lt body bgcolor="DDDDDD"&amp;gt

        &amp;lt html:form action="/logicmatch"&amp;gt
        &amp;lt h3&amp;gt Enter Some Text:&amp;lt /h3&amp;gt
        &amp;lt html:text name="LogicMatchForm" property="text"/&amp;gt
        &amp;lt html:submit/&amp;gt


        &amp;lt /html:form&amp;gt
    &amp;lt /body&amp;gt
&amp;lt /html&amp;gt
</code></pre>
<p>2.Create an Jsp page and name it as <b>LogicMatchTag.jsp</b>.It is the output page for user which contains the logic tags to Match the text and display whether the specified constant value is a substring or not.</p>
<p><b>LogicMatchTag.jsp</b></p>
<pre><code>
&amp;lt%@page contentType="text/html" pageEncoding="UTF-8"%&amp;gt
&amp;lt%@taglib prefix="html" uri="http://jakarta.apache.org/struts/tags-html" %&amp;gt
&amp;lt%@taglib prefix="logic" uri="http://jakarta.apache.org/struts/tags-logic" %&amp;gt


&amp;lt html&amp;gt
    &amp;lt head&amp;gt
        &amp;lt meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&amp;gt
        &amp;lt title&amp;gt Struts Logic Match Tag&amp;lt /title&amp;gt
    &amp;lt /head&amp;gt
    &amp;lt body bgcolor="DDDDDD"&amp;gt
        &amp;lt h1&gt;Struts logic:match and logic:notMatch tag&amp;lt /h1&amp;gt
        &amp;lt logic:match name="LogicMatchForm" property="text" value="java&amp;gt
&amp;lt h4&amp;gt Using &lt;logic:match&gt; tag the given text contains "java"&amp;lt /h4&amp;gt

            &amp;lt /logic:match&amp;gt
            &amp;ltlogic:notMatch name="LogicMatchForm" property="text" value="struts"&amp;gt
&amp;lt h4&amp;gt  Using &lt;logic:notMatch&gt; tag the given text does not contains "struts"&amp;lt /h4&amp;gt

            &amp;lt /logic:notMatch&amp;gt
    &amp;lt /body&amp;gt
&amp;lt /html&amp;gt

</code></pre>
<p>3.Create a Form bean.Form bean is used to hold the properties of the submitted form which is a sub class of ActionForm.It contains only one property of type String to hold the text.</p>
<ul>
<li>Buy <a href="http://astore.amazon.com/javabeat-20?_encoding=UTF8&amp;node=13" target="_blank">Struts Books</a> from <a href="http://astore.amazon.com/javabeat-20" target="_blank">Java Books Store</a></li>
<p>.
	</ul>
</p>
<p><b>LogicMatchForm.java</b></p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<pre><code>
package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class LogicMatchForm extends org.apache.struts.action.ActionForm {

    private String text;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public LogicMatchForm() {
        super();

    }

}

</code></pre>
<p>4.Simple Action class <b>LogicMatchAction.java</b> which is a sub class of <i>Action</i> class used to process the user&#8217;s request. </p>
<p><b>LogicMatchAction.java</b></p>
<pre><code>
package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

public class LogicMatchAction extends org.apache.struts.action.Action {


    private final static String SUCCESS = "success";

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward(SUCCESS);
    }
}


</code></pre>
<p>5.Create or modify struts config file <b>struts-config.xml</b> with action mappings.Struts-config file contains the information about the configuration of the struts framework to the application.It contains the action mappings which helps to select Action,ActionForm and other information for specific user&#8217;s request&#8217;s.</p>
<pre><code>

&amp;lt?xml version="1.0" encoding="UTF-8" ?&amp;gt

&amp;lt!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"&amp;gt

&amp;lt struts-config&amp;gt
    &amp;lt form-beans&amp;gt
        &amp;lt form-bean name="LogicMatchForm" type="com.myapp.struts.LogicMatchForm"/&amp;gt
    &amp;lt/form-beans&amp;gt

&amp;lt action-mappings&amp;gt
        &amp;lt action name="LogicMatchForm" path="/logicmatch" scope="request" type="com.myapp.struts.LogicMatchAction" validate="false"&amp;gt
        &amp;lt forward name="success" path="/LogicMatchTag.jsp"/&amp;gt

            &amp;lt/action&amp;gt
&amp;lt/action-mappings&amp;gt

	&amp;lt/struts-config&amp;gt

</code></pre>
<p>6.Building and running the application</p>
<h4><b>Output</b></h4>
<p><b>Access page:
<pre><code>http://localhost:8080/logicmatch/</code></pre>
<p></b></p>
<p><img src="images/2010/10/struts-logicmatch/struts-logicmatch-1.jpg" width="585" height="204"></p>
<p><b>After evaluation the result is displayed</b></p>
<p><img src="images/2010/10/struts-logicmatch/struts-logicmatch-2.jpg" width="585" height="204"></p>
<h2>Struts Framework Articles</h2>
<ul>
<li>Buy <a href="http://astore.amazon.com/javabeat-20?_encoding=UTF8&amp;node=13" target="_blank">Struts Books</a> from <a href="http://astore.amazon.com/javabeat-20" target="_blank">Java Books Store</a></li>
<li><a href="http://www.javabeat.net/articles/67-struts-20-introduction-and-validations-using-annotatio-1.html">Struts 2.0 Introduction and Validations using Annotations</a></li>
<li><a href="http://www.javabeat.net/articles/24-introduction-to-struts-actions-1.html">Introduction to Struts Actions</a></li>
<li><a href="http://www.javabeat.net/articles/69-whats-new-in-struts-20-struts-20-framework-1.html">What&#8217;s new in Struts 2.0?</a></li>
<li><a href="http://www.javabeat.net/articles/132-internationalization-and-taglibs-in-struts-12-1.html">Internationalization and Taglibs in Struts 1.2</a></li>
<p>	</u></p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2010/10/struts-logic-match-and-notmatch-tag-logicmatch-and-logicnotmatch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
