Every Flavour Beans

“The time has come…to talk of many [technologies].” –Lewis Carroll(‘The Walrus and the Carpenter’)
Development Tools. Web Frameworks. GNU/Linux. Nokia N800. Video Encoding.

October 3, 2009

“Hello, World” Web Application using Spring MVC in NetBeans IDE 6.7

Filed under: General — tabrez @ 7:28 pm

If you are looking to learn web application development with Spring Web MVC framework NetBeans is probably the best IDE to get started with. You don't have to worry about which framework library files to download, where to copy them etc. and moreover some of the basic configuration is automatically created as part of project template creation wizard. Below is an example "Hello, World" web application created using Spring Web MVC framework in NetBeans IDE.

  1. Start NetBeans IDE and create a new project File -> New Project...
    Select "Java Web" in Categories section and "Web Application" in Projects section and click Next.

    Enter a name for the project in "Project Name:" field(say "HelloWorld") and click Next.
    Change any of the server settings(like selecting a different web server instead of the default Glassfish) if you want to, otherwise just click Next. Finally, make sure that you check the "Spring Web MVC 2.5" checkbox and any additional frameworks you plan to use for this web application(like Hibernate) and press Finish.

  2. NetBeans does a nice job of creating a template Spring Web MVC project for you by copying the required spring jar files in web application build directory, generating configuration files with sample content etc. You can see that web.xml has the required configuration lines already. Also have a look at dispatcher-servlet.xml file to see handler mapping bean and view resolver bean already declared. (Netbeans should have it opened automatically; if not, open it from HelloWorld -> Web Pages -> WEB-INF.) Run the application to see what the sample project currently does.

Now let us add our own code to the application.

  1. First step is to create the controller class. Right-click on Source Packages and select New -> Java Class. Enter HelloController in the "Class Name:" field and hello in "Package:" field. Click Finish.

    Enter the following code in the HelloController.java file.

    JAVA:
    1. package hello;
    2.  
    3. import javax.servlet.http.HttpServletRequest;
    4. import javax.servlet.http.HttpServletResponse;
    5. import org.springframework.web.servlet.ModelAndView;
    6. import org.springframework.web.servlet.mvc.AbstractController;
    7.  
    8. public class HelloController extends AbstractController {
    9.  
    10.     @Override
    11.     protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
    12.         String message = "Hello, World!";
    13.         return new ModelAndView("response", "msg", message);
    14.     }   
    15. }

  2. Open dispatcher-servlet.xml and add a mapping from hello.htm URI to HelloController controller class as shown below.
    XML:
    1. ...
    2.     <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    3.         <property name="mappings">
    4.             <props>
    5.                 <prop key="index.htm">indexController</prop>
    6.                 <prop key="hello.htm">helloController</prop>
    7.             </props>
    8.         </property>
    9.     </bean>
    10.    
    11.     <bean id="helloController" class="hello.HelloController" />
    12. ...

  3. Time to create the response page. Right-click on jsp directory(Web Pages->WEB-INF->jsp) and create a new JSP file. Since we are returning "response" as the view name from our controller class, enter "response" in the "JSP File Name:" field. Click Finish.
    Now open the response.jsp page we just created and enter the following code in it.

    HTML:
    1.         <h1>Hello World!</h1>
    2.         Message: ${msg}
    3.     </body>

  4. Run the application and change the request uri in the browser address bar to http://localhost:8080/HelloWorld/hello.htm and press Enter. You should see the following response page.

NetBeans IDE saves a bit of time by generating a configured Spring Web MVC project for us to start with. With most other popular IDEs, we need to install the required library files and create the basic configuration files manually. I will describe the procedure using Eclipse IDE in my next post.


If you want to receive future posts by email, enter your email address here:

Related Posts:

  • “Hello, World” Java Web Application using Java SE 6 + Tomcat 5.5 + Maven 2
  • Struts 2 Plugin for NetBeans IDE – nbstruts2support
  • “Hello, World” Web Application using Struts 2 in IntelliJ IDEA 8.0 M1
  • “Hello, World” Web Application in Ruby on Rails using console
  • “Hello, World” Web Application using Struts 2 in NetBeans IDE 6.1
  • “Hello, World” Web Application in Ruby on Rails using Aptana Studio
  • Setting Up Rails Development Environment on Windows Vista/XP


  • 2 Comments »

    1. I was banging my head, why i could not get it right. Previously i used Eclipse. Only to get spring started on eclipse got me quite some time. SimpleUrlHandlerMapping was not what i was used to. So it again spin my head on netbeans. I did not even like to turn over the book for the solution, because working on spring was always a headache for me. But your post made all those pain go away.

      indexController
      simpleController

      is what we need now. Thanks

      Quote

      Comment by Razor — October 24, 2009 @ 7:44 pm

    2. I am trying to do this project but when I go to the categories the "Java Web" option doesn't appear in my Netbeans 6.7.1 IDE. I tried uninstalling and installing it but it still doesn't work. Any suggestions?

      Quote

      Comment by Saint — November 19, 2009 @ 4:26 am

    RSS feed for comments on this post. TrackBack URI

    Leave a comment


    Copyright (c) 2006, 2007 Tabrez Iqbal.
    Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. Verbatim copying and distribution of this entire article is permitted in any medium without royalty provided this notice is preserved. A copy of the license is included in the section entitled "GNU Free Documentation License".


    Powered by WordPress
    This website is hosted by Dreamhost