blob: c6413272d49224bbdf2f5a63fa998f38e0b5169c [file]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>4.3&nbsp;The controller</title><link rel="stylesheet" href="css/stylesheet.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"><link rel="home" href="index.html" title="Creating an application with EclipseRT Virgo Web Server"><link rel="up" href="ch04.html" title="4.&nbsp;The Web Module"><link rel="prev" href="ch04s02.html" title="4.2&nbsp;GreenPages set up"><link rel="next" href="ch04s04.html" title="4.4&nbsp;Deploying a bundle"><!--Begin Google Analytics code--><script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script><script type="text/javascript">
var pageTracker = _gat._getTracker("UA-2728886-3");
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._trackPageview();
</script><!--End Google Analytics code--></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">4.3&nbsp;The controller</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch04s02.html">Prev</a>&nbsp;</td><th width="60%" align="center">4.&nbsp;The Web Module</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="ch04s04.html">Next</a></td></tr></table><hr></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="controller"></a>4.3&nbsp;The controller</h2></div></div></div><p>
The Spring&#8217;s MVC style of web application development is used in which the central type
is the <code class="literal">Controller</code> class.
</p><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="import.greenpages.web"></a>Import the web project</h3></div></div></div><p>
The GreenPages application is divided into OSGi bundles that are represented as Eclipse
projects. In this step import the <code class="literal">greenpages.web</code> project.
</p><p>
Starting with no projects, import the web project by right-clicking in the <span class="emphasis"><em>Package Explorer</em></span>
view and selecting the <span class="emphasis"><em>Import&#8230;</em></span> menu item.
In the dialog that opens, choose <span class="guimenuitem">General</span> &#8594; <span class="guimenuitem">Existing Projects into Workspace</span> and select <span class="emphasis"><em>Next</em></span>.
In the following dialog set the <span class="emphasis"><em>root directory</em></span> to the value of
<code class="literal">$GREENPAGES_HOME/start/greenpages.web</code> and press <span class="emphasis"><em>Finish</em></span>.
</p><p>
(Initially this project may have compiler errors;
this is to be expected particularly if the Maven repository hasn&#8217;t yet been created.)
When this project is imported go to the next step.
</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="controller.controller"></a>The controller class</h3></div></div></div><p>
In the <code class="literal">src/main/java</code> source folder of the <code class="literal">greenpages.web</code> project
the package <code class="classname">greenpages.web</code>
should contain the controller class named
<code class="classname">GreenPagesController</code>.
Create this by right-clicking on the <code class="literal">greenpages.web</code> package in the
<code class="literal">src/main/java</code> source folder and selecting
<span class="guimenuitem">New</span> &#8594; <span class="guimenuitem">Class</span>.
(If <span class="emphasis"><em>Class</em></span> is not offered on the <span class="emphasis"><em>New</em></span> menu
the <span class="emphasis"><em>Java</em></span> perspective may not be being used, in which case look for
the <span class="emphasis"><em>Class</em></span> option under <span class="emphasis"><em>Other&#8230;</em></span> in the <span class="emphasis"><em>Java</em></span> section.)
</p><p>
Name the new class <code class="classname">GreenPagesController</code> and press <span class="emphasis"><em>Finish</em></span>.
</p><div class="mediaobject" align="center"><img src="images/web-module/new-greenpages-controller.png" align="middle"></div><p>
</p><p>
The code should be edited to look like this:
</p><pre class="programlisting">@Controller
<span class="hl-keyword">public</span> <span class="hl-keyword">class</span> GreenPagesController {
&#8230;
@RequestMapping(<span class="hl-string">"/home.htm"</span>)
<span class="hl-keyword">public</span> <span class="hl-keyword">void</span> home() {
}
&#8230;
</pre><p>
</p><p>
The annotations <code class="classname">Controller</code> and <code class="classname">RequestMapping</code>
are from Spring Framework and are imported by adding the lines:
</p><pre class="programlisting"><span class="hl-keyword">import</span> org.springframework.stereotype.Controller;
<span class="hl-keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;
</pre><p>
</p><p>
STS will offer (as a <span class="emphasis"><em>Quick Fix</em></span>) to insert imports for these Spring Framework annotations
the first time they are used.
(Java 1.6 supports annotations, and the Spring Framework libraries are accessible by
linking to the correct Web Server runtime environment or generating the correct dependencies for the Maven plug-in.)
</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="controller.component.scanning"></a>Enabling component scanning</h3></div></div></div><p>
Spring will detect the <code class="classname">@Controller</code> annotation and create a bean of controller type,
<span class="emphasis"><em>provided that</em></span> it scans the classpath for these.
Spring&#8217;s component scanning is enabled by inserting a <code class="literal">context</code> tag
in one of the Spring bean definition files.
</p><p>
Open the <code class="filename">WEB-INF/greenpages-servlet.xml</code> file in the
<code class="literal">src/main/webapp</code> folder and ensure the following lines are present:
</p><pre class="programlisting"> &lt;<span class="hl-comment">!-- enable classpath scanning --</span>&gt;
&lt;<span class="hl-tag">context:component-scan</span> <span class="hl-attribute">base-package</span>=<span class="hl-value">"greenpages.web"</span> /&gt;
</pre><p>
</p><p>
Experiment by adding and removing this line, saving the file after each change.
(<span class="emphasis"><em>Easily done by commenting it&#8212;use the
<span class="emphasis"><em>Toggle Comment</em></span>
shortcut in STS.</em></span>)
Look in the <span class="emphasis"><em>Spring Explorer</em></span> view for a bean named <code class="literal">greenPagesController</code>
dynamically created by the <code class="literal">component-scan</code> tag.
</p></div></div><!--Begin LoopFuse code--><script src="http://loopfuse.net/webrecorder/js/listen.js" type="text/javascript"></script><script type="text/javascript">
_lf_cid = "LF_48be82fa";
_lf_remora();
</script><!--End LoopFuse code--><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch04s02.html">Prev</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="ch04.html">Up</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="ch04s04.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">4.2&nbsp;GreenPages set up&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;4.4&nbsp;Deploying a bundle</td></tr></table></div></body></html>