“Hello, World” Web Application using Spring MVC in NetBeans IDE 6.7
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.
- 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.
- 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.
- 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]
package hello;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;public class HelloController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
String message = “Hello, World!”;
return new ModelAndView(“response”, “msg”, message);
}
}
[/java] - Open dispatcher-servlet.xml and add a mapping from hello.htm URI to HelloController controller class as shown below.
indexController helloController - 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.
Hello World!
Message: ${msg}
- 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.
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
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?
i cant see the code inside [html] code tags..
is there the code for download?
owh, thanks for this, and now i know about “hello word” structure…