| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | |
| <html> | |
| <head> | |
| <meta http-equiv="content-type" content="text/html; charset=UTF-8"> | |
| <meta name="description" content="An introduction to using the Spring Framework | |
| in NetBeans IDE"> | |
| <meta name="keywords" content="NetBeans, IDE, integrated development environment, | |
| Spring framework, frameworks, MVC, Model View Controller, web application"> | |
| <link rel="stylesheet" type="text/css" href="../../../netbeans.css"> | |
| <title>Introduction to Spring Web MVC - NetBeans IDE Tutorial</title> | |
| </head> | |
| <body> | |
| <!-- | |
| Copyright (c) 2009, 2010, 2011, Oracle and/or its affiliates. All rights reserved. | |
| --> | |
| <h1>Introduction to Spring Web MVC</h1> | |
| <p>This document shows you how to construct a simple web | |
| <a href="http://www.oracle.com/technetwork/articles/javase/index-142890.html">MVC</a> | |
| application using the <a href="http://www.springframework.org/">Spring Framework</a>. The | |
| application enables a user to enter her name in a text field, and upon clicking OK, the | |
| name is returned and displayed on a second page with a welcome greeting.</p> | |
| <p>The Spring Framework is a popular open source application framework that can make | |
| Java EE development easier. It consists of a container, a framework for managing | |
| components, and a set of snap-in services for web user interfaces, transactions, | |
| and persistence. A part of the Spring Framework is Spring Web MVC, an extensible MVC | |
| framework for creating web applications.</p> | |
| <p>The IDE provides built-in support for Spring Framework 4.x and 3.x. Framework libraries | |
| are packaged with the IDE and are automatically added to the project classpath | |
| when the framework is selected. Configuration settings are provided, such as | |
| naming and mapping of the Spring Web MVC <code>DispatcherServlet</code>. The JSTL | |
| library can optionally be registered upon project creation. Support for Spring XML | |
| bean configuration files is also provided, including the following functionality:</p> | |
| <ul> | |
| <li><strong>Code completion</strong>. Invoked in Spring XML configuration files | |
| for Java classes as well as bean references.</li> | |
| <li><strong>Navigation</strong>. Hyperlinking of Java classes and properties | |
| mentioned in Spring bean definitions, as well as hyperlinking to other | |
| Spring bean references.</li> | |
| <li><strong>Refactoring</strong>. Renaming of references to Java classes in | |
| Spring XML configuration files.</li> | |
| </ul> | |
| <p>For more information on the Spring Framework, visit | |
| <a href="http://www.springsource.org/">http://www.springsource.org/</a>. | |
| For a more fine-grained explanation of how Spring Framework artifacts behave | |
| and interact with other objects in an application, see the official | |
| <a href="http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/">Spring | |
| Framework Reference Documentation</a>, or consult the | |
| <a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/">Spring | |
| Framework API documentation</a>.</p> | |
| <p><strong>Contents</strong></p> | |
| <img src="../../../images_www/articles/73/netbeans-stamp-80-74-73.png" class="stamp" alt="Content on this page applies to NetBeans IDE 7.2, 7.3, 7.4 and 8.0" title="Content on this page applies to the NetBeans IDE 7.2, 7.3, 7.4 and 8.0" > | |
| <ul class="toc"> | |
| <li><a href="#setting">Setting up a New Project with Spring Web MVC Support</a> | |
| <ul> | |
| <li><a href="#creating">Creating a Spring Web MVC Skeleton Project</a></li> | |
| <li><a href="#running">Running the Skeleton Project</a></li> | |
| </ul></li> | |
| <li><a href="#overview">Overview of the Application</a></li> | |
| <li><a href="#service">Implementing a Service</a></li> | |
| <li><a href="#controller">Implementing the Controller and Model</a></li> | |
| <li><a href="#view">Implementing the Views</a></li> | |
| <li><a href="#seeAlso">See Also</a></li> | |
| </ul> | |
| <p><strong>To complete this tutorial, you need the following software and resources.</strong></p> | |
| <table> | |
| <tbody> | |
| <tr> | |
| <th class="tblheader" scope="col">Software or Resource</th> | |
| <th class="tblheader" scope="col">Version Required</th> | |
| </tr> | |
| <tr> | |
| <td class="tbltd1"><a href="https://netbeans.org/downloads/index.html">NetBeans | |
| IDE</a></td> | |
| <td class="tbltd1">7.2, 7.3, 7.4, 8.0, Java EE</td> | |
| </tr> | |
| <tr> | |
| <td class="tbltd1"><a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">Java Development Kit (JDK)</a></td> | |
| <td class="tbltd1">version 7 or 8</td> | |
| </tr> | |
| <tr> | |
| <td class="tbltd1"><a href="http://glassfish.dev.java.net/public/downloadsindex.html">GlassFish server</a></td> | |
| <td class="tbltd1">3.x, 4.x</td> | |
| </tr> | |
| </tbody> | |
| </table> | |
| <p><strong class="notes">Notes:</strong></p> | |
| <ul> | |
| <li>The Java installation of the IDE enables you to optionally install and register the GlassFish server with the IDE.</li> | |
| <li>If you need to compare your project with a working solution, you can | |
| <a href="https://netbeans.org/projects/samples/downloads/download/Samples%252FJava%2520Web%252FHelloSpring69.zip">download | |
| the sample application</a>.</li> | |
| </ul> | |
| <h2 id="setting">Setting up a New Project with Spring Web MVC Support</h2> | |
| <ul> | |
| <li><a href="#creating">Creating a Spring Web MVC Skeleton Project</a></li> | |
| <li><a href="#running">Running the Skeleton Project</a></li> | |
| </ul> | |
| <div class="indent"> | |
| <h3 id="creating">Creating a Spring Web MVC Skeleton Project</h3> | |
| <p>Start by creating a new project for a web application using the Spring Framework.</p> | |
| <ol> | |
| <li>Choose New Project (Ctrl-Shift-N; ⌘-Shift-N on Mac) from the IDE's File menu. Select the Java Web | |
| category, then under Projects select Web Application. Click Next.</li> | |
| <li>In Project Name, type in <strong>HelloSpring</strong>. Click Next.</li> | |
| <li>In Step 3: Server and Settings, deselect the Enable Contexts and Dependency | |
| Injection option, as you are not working with the <a href="http://jcp.org/en/jsr/detail?id=299">JSR-299</a> | |
| specification in this tutorial.</li> | |
| <li>Confirm that the GlassFish server is selected in the Server drop-down list. Click Next. | |
| <p>The Java EE version depends upon the version of the server that is selected. | |
| The default Java EE version is Java EE 7 Web when the selected server is GlassFish Server 4.0. | |
| </p></li> | |
| <li>In Step 4, the Frameworks panel, select Spring Web MVC.</li> | |
| <li>Select <strong>Spring Framework 3.x</strong> in the Spring Library drop-down list. | |
| <br> | |
| <img src="../../../images_www/articles/80/web/spring/frameworks-window.png" | |
| class="margin-around b-all" | |
| title="Spring Web MVC displayed in the Frameworks panel" | |
| alt="Spring Web MVC displayed in the Frameworks panel"> | |
| <p class="alert"> | |
| Note that the IDE enables you to add the Spring 4.x library | |
| to your project, but for this tutorial you will use SimpleFormController that is not supported in Spring 4.x. | |
| Also, when you select Spring Web MVC, note that the JSTL (JavaServer Pages Standard | |
| Tag Library) library is added to the classpath during project creation by default. | |
| Deselect this option (as in the above screenshot), since you do not require JSTL | |
| for this tutorial. </p></li> | |
| <li>Click the Configuration tab and note that the wizard enables you to specify | |
| the name and mapping of the Spring Dispatcher servlet. | |
| <br> | |
| <img src="../../../images_www/articles/72/web/spring/spring-configuration.png" | |
| title="Specify the name and mapping of the Spring Dispatcher servlet under the Configuration tab" | |
| class="margin-around b-all" alt="Spring configuration options"></li> | |
| <li>Click Finish. The IDE creates a project for the entire application, including | |
| all metadata, as well as the project's Ant build script which you can inspect | |
| from the Files window (Ctrl-2; ⌘-2 on Mac). You can view the template | |
| structure from the Projects window (Ctrl-1; ⌘-1 on Mac). Also note that | |
| four files open by default in the IDE's editor: <code>dispatcher-servlet.xml</code>, | |
| <code>applicationContext.xml</code>, <code>redirect.jsp</code>, and <code>index.jsp</code>.</li> | |
| <li>In the Projects window, expand the new project's Libraries node and note that | |
| the Spring JARs are included in the project's classpath. | |
| <br> | |
| <img src="../../../images_www/articles/72/web/spring/spring-libraries.png" | |
| title="Spring JARs are listed under the project's Libraries node" | |
| class="margin-around b-all" alt="Spring JARs listed under project's Libraries node"> | |
| </li> | |
| </ol> | |
| <h3 id="running">Running the Skeleton Project</h3> | |
| <p>Before making any changes to project files, try running the new project in the | |
| IDE:</p> | |
| <ol> | |
| <li>Click the Run Project ( | |
| <img src="../../../images_www/articles/72/web/spring/run-project-btn.png" | |
| alt="Run Project button"> ) in the IDE's main toolbar. The IDE automatically | |
| starts the GlassFish server if it is not already running, compiles the project, | |
| then deploys it to the server. Note any output displayed in the IDE's Output | |
| window (Ctrl-4; ⌘-4 on Mac). The generated output completes with a | |
| <code>BUILD SUCCESSFUL</code> message. | |
| <br> | |
| <img src="../../../images_www/articles/72/web/spring/output.png" | |
| alt="Output window displaying information when running the project" | |
| title="Output window displays information when running the project" | |
| class="margin-around b-all"> | |
| <br> | |
| The IDE's default browser starts up, and you see content from the | |
| welcome page view (<code>/WEB-INF/jsp/index.jsp</code>). | |
| <br> | |
| <img src="../../../images_www/articles/72/web/spring/browser-output.png" | |
| class="margin-around b-all" width="668" | |
| title="Welcome page output is displayed in browser" | |
| alt="Welcome page output displayed in browser"></li> | |
| </ol> | |
| <p class="tips">When you run your project in the IDE, the project is compiled and deployed | |
| to the server, and then opens in your default browser. Furthermore, the IDE | |
| provides a Deploy on Save feature, which is activated by default for web | |
| projects. When you save files in the editor, your project is automatically | |
| recompiled and deployed to the server. To view changes, you can simply | |
| refresh pages in your browser.</p> | |
| <p>In order to understand what just took place, start by examining the project's | |
| deployment descriptor (<code>web.xml</code>). To open this file in the Source | |
| Editor, right-click the <code>WEB-INF</code> > <code>web.xml</code> node in the | |
| Projects window and choose Edit. The default entry point for the application | |
| is <code>redirect.jsp</code>:</p> | |
| <div class="indent"> | |
| <pre class="examplecode"> | |
| <welcome-file-list> | |
| <welcome-file>redirect.jsp</welcome-file> | |
| </welcome-file-list></pre> | |
| </div> | |
| <p>Within <code>redirect.jsp</code>, there is a redirect statement that points all | |
| requests to <code>index.htm</code>:</p> | |
| <div class="indent"> | |
| <pre class="examplecode"><% response.sendRedirect("index.htm"); %></pre> | |
| </div> | |
| <p>In the deployment descriptor, note that all requests for URL patterns that | |
| match <code>*.htm</code> are mapped to Spring's | |
| <a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/servlet/DispatcherServlet.html"><code>DispatcherServlet</code></a>.</p> | |
| <div class="indent"> | |
| <pre class="examplecode"> | |
| <servlet> | |
| <servlet-name>dispatcher</servlet-name> | |
| <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> | |
| <load-on-startup>2</load-on-startup> | |
| </servlet> | |
| <servlet-mapping> | |
| <servlet-name>dispatcher</servlet-name> | |
| <url-pattern>*.htm</url-pattern> | |
| </servlet-mapping></pre> | |
| </div> | |
| <p class="tips">The fully qualified name of the dispatcher servlet, as shown above, | |
| is <code>org.springframework.web.servlet.DispatcherServlet</code>. This | |
| class is contained in the Spring library, which was added to the project | |
| classpath when the project was created. You can verify this in the Projects | |
| window by drilling down from the Libraries node. Locate the <code>spring-webmvc-3.1.1.RELEASE.jar</code>, | |
| then expand it to find <code>org.springframework.web.servlet</code> > | |
| <code>DispatcherServlet</code>.</p> | |
| <p>The <code>DispatcherServlet</code> handles incoming requests based on configuration | |
| settings found in <code>dispatcher-servlet.xml</code>. Open <code>dispatcher-servlet.xml</code> | |
| by clicking on its tab in the editor. Note the following code.</p> | |
| <div class="indent"> | |
| <pre class="examplecode"> | |
| <bean id="urlMapping" class="org.springframework.web.servlet.handler.<a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/servlet/handler/SimpleUrlHandlerMapping.html">SimpleUrlHandlerMapping</a>"> | |
| <property name="mappings"> | |
| <props> | |
| <prop key="/index.htm">indexController</prop> | |
| </props> | |
| </property> | |
| </bean> | |
| <bean id="viewResolver" | |
| class="org.springframework.web.servlet.view.<a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/servlet/view/InternalResourceViewResolver.html">InternalResourceViewResolver</a>" | |
| p:prefix="/WEB-INF/jsp/" | |
| p:suffix=".jsp" /> | |
| <bean name="indexController" | |
| class="org.springframework.web.servlet.mvc.<a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/servlet/mvc/ParameterizableViewController.html">ParameterizableViewController</a>" | |
| p:viewName="index" /></pre> | |
| </div> | |
| <p>Three beans are defined in this file: <code>indexController</code>, <code>viewResolver</code>, | |
| and <code>urlMapping</code>. When the <code>DispatcherServlet</code> receives a | |
| request that matches <code>*.htm</code> such as <code>index.htm</code>, it looks | |
| for a controller within <code>urlMapping</code> that can accommodate the request. | |
| Above, you see that there is a <code>mappings</code> property that links <code>/index.htm</code> | |
| to <code>indexController</code>.</p> | |
| <p>The runtime environment then searches for the definition of a bean named <code>indexController</code>, | |
| which is conveniently provided by the skeleton project. Note that <code>indexController</code> | |
| extends <a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/servlet/mvc/ParameterizableViewController.html"><code>ParameterizableViewController</code></a>. | |
| This is another class provided by Spring, which simply returns a view. Above, note that | |
| <code>p:viewName="index"</code> specifies the logical view name, which is resolved | |
| using the <code>viewResolver</code> by prepending <code>/WEB-INF/jsp/</code> and appending | |
| <code>.jsp</code> to it. This allows the runtime to locate the file within the application | |
| directory, and respond with the welcome page view (<code>/WEB-INF/jsp/index.jsp</code>).</p> | |
| </div> | |
| <h2 id="overview">Overview of the Application</h2> | |
| <p>The application you create is comprised of two JSP pages (which can be referred to as <em>views</em> | |
| in <a href="http://www.oracle.com/technetwork/articles/javase/index-142890.html">MVC</a> | |
| terminology). The first view contains an HTML form with an input field asking for the user's | |
| name. The second view is a page that simply displays a hello message containing the user's name.</p> | |
| <p>The views are managed by a <em>controller</em>, which receives requests to the application | |
| and decides which views to return. It also passes to the views any information that they | |
| need to display (this is called a <em>model</em>). This application's controller is named | |
| <code>HelloController</code>.</p> | |
| <p>In a complex web application, the business logic is not contained directly in the controller. | |
| Instead, another entity, named a <em>service</em>, is used by the controller whenever it | |
| needs to perform some business logic. In our application, the business logic is limited to | |
| the act of processing the hello message, and for this purpose you create a <code>HelloService</code>. | |
| <a name="service"></a> | |
| <h2>Implementing a Service</h2> | |
| <p>Now that you are sure your environment is set up properly, you can begin | |
| extending the skeleton project according to your needs. Start by creating | |
| the <code>HelloService</code> class.</p> | |
| <ol> | |
| <li>Click the New File ( | |
| <img src="../../../images_www/articles/72/web/spring/new-file-btn.png" | |
| alt="New File button"> ) button in the IDE's toolbar. (Alternatively, press Ctrl-N; | |
| ⌘-N on Mac.)</li> | |
| <li>Select the <strong>Java</strong> category, then select <strong>Java Class</strong> | |
| and click Next.</li> | |
| <li>In the New Java Class wizard that displays, type in <strong>HelloService</strong> | |
| for Class Name, and enter <strong>service</strong> for Package Name to create | |
| a new package for the class.</li> | |
| <li>Click Finish. The IDE creates the new class and opens it in the editor.</li> | |
| </ol> | |
| <p>The <code>HelloService</code> class performs a very simple service. It takes a name | |
| as a parameter, and prepares and returns a <code>String</code> that includes the name. | |
| In the editor, create the following <code>sayHello()</code> method for the | |
| class (changes in <strong>bold</strong>).</p> | |
| <div class="indent"> | |
| <pre class="examplecode"> | |
| public class HelloService { | |
| <strong>public static String sayHello(String name) { | |
| return "Hello " + name + "!"; | |
| }</strong> | |
| }</pre></div> | |
| <h2 id="controller">Implementing the Controller and Model</h2> | |
| <p>You can use a | |
| <a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/servlet/mvc/SimpleFormController.html"><code>SimpleFormController</code></a> | |
| to handle user data and determine which view to return.</p> | |
| <p class="notes"><strong>Note: </strong>SimpleFormController is deprecated in Spring 3.x. It is used in this tutorial for demonstration purposes. However, annotated controllers should be used instead of XML files.</p> | |
| <ol> | |
| <li>Open the New File wizard by pressing Ctrl-N (⌘-N on Mac). Under Categories | |
| select <strong>Spring Framework</strong>; under File Types select <strong>Simple | |
| Form Controller</strong>. | |
| <br> | |
| <img src="../../../images_www/articles/72/web/spring/simple-form-controller.png" | |
| class="b-all margin-around" style="width:668px" | |
| alt="New File wizard - Spring Framework category" | |
| title="NetBeans IDE provides templates for various Spring artifacts"> | |
| <br> | |
| <span class="tips">NetBeans IDE provides templates for various Spring artifacts, | |
| including the Spring XML Configuration File, the | |
| <a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/servlet/mvc/AbstractController.html"><code>AbstractController</code></a>, | |
| and the <a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/servlet/mvc/SimpleFormController.html"><code>SimpleFormController</code></a>.</span> | |
| </li> | |
| <li>Click Next.</li> | |
| <li>Name the class <strong>HelloController</strong> and create a new package for it by | |
| typing <strong>controller</strong> in the Package text field. Click Finish. The IDE | |
| creates the new class and opens it in the editor.</li> | |
| <li>Specify controller properties by uncommenting the setter methods that display by | |
| default in the class template. To uncomment the code snippet, highlight the code | |
| as in the image below, then press Ctrl-/ (⌘-/ on Mac). | |
| <br> | |
| <img src="../../../images_www/articles/72/web/spring/comment-out.png" | |
| class="b-all margin-around" | |
| alt="Code snippet highlighted in editor" | |
| title="Highlight code snippets, then press Ctrl-/ to toggle comments"> | |
| <br> | |
| <span class="tips">Pressing Ctrl-/ (⌘-/ on Mac) toggles comments in the editor.</span> | |
| </li> | |
| <li>Make changes as follows (shown in <strong>bold</strong>). | |
| <pre class="examplecode"> | |
| public HelloController() { | |
| <a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/servlet/mvc/BaseCommandController.html#setCommandClass(java.lang.Class)">setCommandClass</a>(<strong>Name</strong>.class); | |
| <a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/servlet/mvc/BaseCommandController.html#setCommandName(java.lang.String)">setCommandName</a>("<strong>name</strong>"); | |
| <a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/servlet/mvc/SimpleFormController.html#setSuccessView(java.lang.String)">setSuccessView</a>("<strong>hello</strong>View"); | |
| <a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/servlet/mvc/SimpleFormController.html#setFormView(java.lang.String)">setFormView</a>("<strong>name</strong>View"); | |
| }</pre> | |
| <p>Setting the <code>FormView</code> enables you to set the name of the view that | |
| is used to display the form. This is the page that contains the text field | |
| allowing users to enter their name. Setting the <code>SuccessView</code> likewise | |
| lets you set the name of the view that should display upon a successful submit. | |
| When you set the <code>CommandName</code> you define the name of the command in | |
| the model. In this case, the command is the form object with request parameters | |
| bound onto it. Setting the <code>CommandClass</code> allows you set the name of the | |
| command class. An instance of this class gets populated and validated upon each | |
| request.</p> | |
| <p>Note that an error is flagged for <code>Name</code> in the <code>setCommandClass()</code> method:</p> | |
| <img src="../../../images_www/articles/72/web/spring/set-command-class.png" class="b-all margin-around" | |
| alt="The editor displaying an error for setCommandClass()" | |
| title="An error badge displays for setCommandClass()"> | |
| <p> | |
| You now need to create the <code>Name</code> class as a simple bean to hold information | |
| for each request.</p></li> | |
| <li>In the Projects window, right-click on the project node and choose New > Java | |
| Class. The New Java Class wizard displays.</li> | |
| <li>Enter <strong>Name</strong> for the Class Name, and for Package select <strong>controller</strong> | |
| from the drop-down list.</li> | |
| <li>Click Finish. The <code>Name</code> class is created and opens in the editor.</li> | |
| <li>For the <code>Name</code> class, create a field named <code>value</code>, then create | |
| accessor methods (i.e., getter and setter methods) for this field. Start by declaring | |
| the <code>value</code> field: | |
| <pre class="examplecode"> | |
| public class Name { | |
| <strong>private String value;</strong> | |
| }</pre> | |
| <p class="tips">To quickly type out '<code>private</code>' you can type '<code>pr</code>' | |
| then press Tab. The '<code>private</code>' access modifier is automatically added to | |
| the line. This is an example of using the editor's code templates. For a full list of | |
| code templates, choose Help > Keyboard Shortcuts Card.</p> | |
| <br><a name="createAccessors"></a> | |
| The IDE can create accessor methods for you. In the editor, right-click on <code>value</code> | |
| and choose Insert Code (or press Alt-Insert; Ctrl-I on Mac). In the popup menu, choose Getter | |
| and Setter. | |
| <br> | |
| <img src="../../../images_www/articles/72/web/spring/generate-code.png" | |
| class="margin-around b-all" | |
| alt="The Generate Code popup menu displayed in editor" | |
| title="The Generate Code popup menu enables you to set accessor methods"></li> | |
| <li>In the dialog that displays, select the <code>value : String</code> option, then | |
| click OK. The <code>getValue()</code> and <code>setValue()</code> methods are added to | |
| the <code>Name</code> class: | |
| <pre class="examplecode"> | |
| public String getValue() { | |
| return value; | |
| } | |
| public void setValue(String value) { | |
| this.value = value; | |
| }</pre></li> | |
| <li>Press Ctrl-Tab and choose <code>HelloController</code> to switch back to the | |
| <code>HelloController</code> class. Note that the previous error badge has | |
| disappeared since the <code>Name</code> class now exists.</li> | |
| <li>Delete the <code>doSubmitAction()</code> | |
| method and uncomment the | |
| <a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/servlet/mvc/SimpleFormController.html#setFormView(java.lang.String)"><code>onSubmit()</code></a> | |
| method. The <code>onSubmit()</code> method enables you to create your own | |
| <code>ModelAndView</code>, which is what is required here. Make the following | |
| changes: | |
| <pre class="examplecode">@Override | |
| protected ModelAndView onSubmit( | |
| HttpServletRequest request, | |
| HttpServletResponse response, | |
| Object command, | |
| BindException errors) throws Exception { | |
| Name name = (Name) command; | |
| ModelAndView mv = new ModelAndView(getSuccessView()); | |
| mv.addObject("helloMessage", helloService.sayHello(name.getValue())); | |
| return mv; | |
| }</pre> | |
| As indicated above, the <code>command</code> is recast as a <code>Name</code> object. | |
| An instance of <code>ModelAndView</code> is created, and the success view is | |
| obtained using a getter in <code>SimpleFormController</code>. Finally, the model | |
| is populated with data. The only item in our model is the hello message obtained | |
| from the <code>HelloService</code> created earlier. You use the <code>addObject()</code> | |
| method to add the hello message to the model under the name <code>helloMessage</code>.</li> | |
| <li>Fix import errors by right-clicking in the editor and choosing Fix Imports | |
| (Ctrl-Shift-I; ⌘-Shift-I on Mac). | |
| <br> | |
| <img src="../../../images_www/articles/72/web/spring/fix-imports70.png" | |
| class="margin-around b-all" alt="Fix All Imports dialog" | |
| title="Press Ctrl-Shift-I to fix imports in your file"> | |
| <p class="notes"><strong>Note.</strong> Confirm that <strong><tt>org.springframework.validation.BindException</tt></strong> and | |
| <strong><tt>org.springframework.web.servlet.ModelAndView</tt></strong> are selected in the Fix All Imports dialog box.</p></li> | |
| <li>Click OK. The following import statement is added to the top of the file: | |
| <pre class="examplecode">import <a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/servlet/ModelAndView.html">org.springframework.web.servlet.ModelAndView</a>;</pre> | |
| As stated in the API documentation, this class "represents a model and view returned | |
| by a handler, to be resolved by a <code>DispatcherServlet</code>. The view can take the | |
| form of a <code>String</code> view name which will need to be resolved by a <code>ViewResolver</code> | |
| object; alternatively a <code>View</code> object can be specified directly. The model is | |
| a <code>Map</code>, allowing the use of multiple objects keyed by name." | |
| <br><br> | |
| Note that at this stage, not all errors are fixed because the class still cannot identify the | |
| <code>HelloService</code> class, nor make use of its <code>sayHello()</code> method.</li> | |
| <li>Within <code>HelloController</code>, declare a private field named <code>HelloService</code>: | |
| <pre class="examplecode">private HelloService helloService;</pre> | |
| Then create a public setter method for the field: | |
| <pre class="examplecode">public void setHelloService(HelloService helloService) { | |
| this.helloService = helloService; | |
| }</pre> | |
| Finally, right-click in the editor and choose Fix Imports (Ctrl-Shift-I; ⌘-Shift-I | |
| on Mac). The following statement is added to the top of the file: | |
| <pre class="examplecode">import service.HelloService;</pre> | |
| All errors should now be fixed.</li> | |
| <li>Register <code>HelloService</code> in <code>applicationContext.xml</code>. Open | |
| <code>applicationContext.xml</code> in the editor and enter the following | |
| bean declaration: | |
| <pre class="examplecode"> | |
| <bean name="helloService" class="service.HelloService" /></pre> | |
| <div class="tips">Spring support in the IDE includes code completion | |
| within XML configuration files for Java classes as well as bean references. To | |
| invoke code completion, press Ctrl-Space when working in the editor: | |
| <br> | |
| <img src="../../../images_www/articles/72/web/spring/code-completion.png" | |
| alt="Code completion invoked when pressing Ctrl-Space" | |
| title="Code completion invoked when pressing Ctrl-Space" | |
| class="b-all margin-around"></div></li> | |
| <li>Register <code>HelloController</code> in <code>dispatcher-servlet.xml</code>. Open | |
| <code>dispatcher-servlet.xml</code> in the editor and enter the following | |
| bean declaration: | |
| <pre class="examplecode"> | |
| <bean class="controller.HelloController" p:helloService-ref="helloService"/></pre></li> | |
| </ol> | |
| <h2 id="view">Implementing the Views</h2> | |
| <p>To implement the view for this project, you need to create two JSP pages. The first, | |
| which you will call <code>nameView.jsp</code>, serves as the welcome page and allows | |
| users to input a name. The other page, <code>helloView.jsp</code>, displays a greeting | |
| message that includes the input name. Begin by creating <code>helloView.jsp</code>.</p> | |
| <ol> | |
| <li id="create-jsp">In the Projects window, right-click the WEB-INF > <code>jsp</code> | |
| node and choose New > JSP. The New JSP File wizard opens. Name the file <strong>helloView</strong>.</li> | |
| <li>Click Finish. The new JSP page is created in the <code>jsp</code> folder and opens | |
| in the editor.</li> | |
| <li>In the editor, change the file's title to <code>Hello</code>, and change | |
| the output message to retrieve the <code>helloMessage</code> of the <code>ModelandView</code> | |
| object that is created in <code>HelloController</code>. | |
| <pre class="examplecode"> | |
| <head> | |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
| <title><strong>Hello</strong></title> | |
| </head> | |
| <body> | |
| <h1><strong>${helloMessage}</strong></h1> | |
| </body> | |
| </pre></li> | |
| <li>Create another JSP page in the same manner <a href="#create-jsp">as above</a>, | |
| but name it <code>nameView</code>.</li> | |
| <li>In the editor, add the following Spring tag library declaration to <code>nameView.jsp</code>. | |
| <pre class="examplecode"><%@taglib uri="http://www.springframework.org/tags" prefix="spring" %></pre> | |
| This imports the <a href="http://static.springframework.org/spring/docs/2.5.x/reference/spring.tld.html">Spring | |
| tag library</a>, which contains tags useful when implementing views as JSP pages. | |
| <li>Change the contents of the <code><title></code> and <code><h1></code> | |
| tags to read: <code>Enter Your Name</code>.</li> | |
| <li>Enter the following code beneath the <code><h1></code> tags: | |
| <pre class="examplecode"> | |
| <spring:nestedPath path="name"> | |
| <form action="" method="post"> | |
| Name: | |
| <spring:bind path="value"> | |
| <input type="text" name="${status.expression}" value="${status.value}"> | |
| </spring:bind> | |
| <input type="submit" value="OK"> | |
| </form> | |
| </spring:nestedPath> | |
| </pre> | |
| <a href="http://static.springframework.org/spring/docs/2.5.x/reference/spring.tld.html#spring.tld.bind">spring:bind</a> | |
| allows you to bind a bean property. The bind tag provides a bind status and | |
| value, which you use as the name and value of the input field. This way, when | |
| the form is submitted, Spring will know how to extract the submitted value. | |
| Here, our command class (<code>controller.Name</code>) has a <code>value</code> | |
| property, therefore you set the <code>path</code> to <code>value</code>. | |
| <br><br> | |
| <a href="http://static.springframework.org/spring/docs/2.5.x/reference/spring.tld.html#spring.tld.nestedPath">spring:nestedPath</a> | |
| enables you to prepend a specified path to a bean. So, when used with <code>spring:bind</code> | |
| as shown above, the path to the bean becomes: <code>name.value</code>. As you recall, the | |
| command name of <code>HelloController</code> is <code>name</code>. Therefore, this path refers | |
| to the <code>value</code> property of a bean named <code>name</code> in the page scope. | |
| <li>Change the relative entry point for the application. Currently, the project entry | |
| point is still <code>index.htm</code> which, as described in <a href="#running">Running | |
| the Skeleton Project</a> above, redirects to <code>WEB-INF/jsp/index.jsp</code>. You | |
| can specify an entry point for the project when it is deployed and run. In the Projects | |
| window, right-click the project node and choose Properties. The Project Properties | |
| dialog displays. Under Categories select Run. In the Relative URL field, type in | |
| <code>/hello.htm</code>, then click OK. | |
| <br><br> | |
| At this moment you may wonder where the mapping of <code>hello.htm</code> to | |
| <code>HelloController</code> is located. You have not added a mapping to the | |
| <code>urlMapping</code> bean, as is the case for <code>index.htm</code>, the | |
| skeleton project's welcome page. This is possible with a bit of Spring magic | |
| provided by the following bean definition in <code>dispatcher-servlet.xml</code>: | |
| <pre class="examplecode"><bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/></pre> | |
| This bean is responsible for automatically creating an URL mapping for all controllers | |
| registered in the file. It takes the fully-qualified class name of the controller | |
| (in our case, <code>controller.HelloController</code>) and strips the package name | |
| and <code>Controller</code> suffix, then uses the result as a URL mapping. Therefore, | |
| for <code>HelloController</code> it creates a <code>hello.htm</code> mapping. This magic | |
| however does not work for controllers that are included in the Spring Framework, | |
| such as <code>ParameterizableViewController</code>. They require an explicit mapping.</li> | |
| <li>In the Projects window right-click the project node and choose Run. This compiles, | |
| deploys and runs the project. Your default browser opens, displaying <code>hello.htm</code> | |
| as the project's <code>nameView</code>: | |
| <br> | |
| <img src="../../../images_www/articles/72/web/spring/name-view.png" | |
| alt="nameView displayed in a browser" | |
| title="nameView is displayed in a browser" | |
| class="b-all margin-around"> | |
| <br> | |
| Enter your name in the text field and click enter. The <code>helloView</code> | |
| displays with a greeting message: | |
| <br> | |
| <img src="../../../images_www/articles/72/web/spring/hello-view.png" | |
| alt="helloView displayed in a browser" | |
| title="helloView is displayed in a browser" | |
| class="b-all margin-around"></li> | |
| </ol> | |
| <div class="feedback-box"> | |
| <a href="/about/contact_form.html?to=3&subject=Feedback:%20Introduction%20to%20Spring">Send | |
| Us Your Feedback</a></div> | |
| <br style="clear:both;"> | |
| <h2 id="seeAlso">See Also</h2> | |
| <p>This concludes the Introduction to the Spring Framework in NetBeans IDE. This document | |
| demonstrated how to construct a simple web MVC application in the NetBeans IDE using | |
| the Spring Framework, and introduced you to the IDE's interface for developing web | |
| applications.</p> | |
| <p>You are encouraged to continue learning about the Spring Framework by working through | |
| other tutorials in NetBeans IDE, such as | |
| <a href="http://sites.google.com/site/springmvcnetbeans/step-by-step/">Developing a | |
| Spring Framework MVC Application Step-by-Step using NetBeans and the GlassFish server</a>. | |
| This is the official <a href="http://static.springframework.org/docs/Spring-MVC-step-by-step/">Spring | |
| Framework tutorial</a> by Thomas Risberg which has been adapted for NetBeans IDE by | |
| Arulazi Dhesiaseelan.</p> | |
| <p>Many of the Spring NetBeans Module capabilities can also be applied to non-web based | |
| Spring framework applications. | |
| <p>For other related tutorials, see the following resources:</p> | |
| <ul> | |
| <li><a href="../../docs/web/framework-adding-support.html">Adding Support for a Web Framework</a>. | |
| A basic guide describing how to add support by installing a web framework | |
| plugin using the NetBeans Update Center.</li> | |
| <li><a href="jsf20-intro.html">Introduction to JavaServer Faces 2.0</a>. Demonstrates | |
| how to add JSF 2.0 support to an existing project, wire managed beans, and take | |
| advantage of Facelets templating.</li> | |
| </ul> | |
| </body> | |
| </html> |