Archive | Spring MVC Newbie RSS for this section

Ayumilove Develop a Spring Framework MVC Application using NetBeans, GlassFish and MySQL (Step by Step) Dummies Guide to Spring MVC (Chapter 2/6)

Developing a Spring Framework MVC application step-by-step using NetBeans and GlassFish
Authors Thomas Risberg, Rick Evans, Portia Tung

Modified by Ayumilove for the 3 Open Source Softwares below:
NetBeans 6.7 (IDE – Integrated Development Environment to develop web applications and software)
GlassFish (Application Server – Consists of Web Container, JavaBean Container)
MySQL (RDBMS – Relational Database Management System)

Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically.

Prerequisite software
The following prerequisite software and environment setup is assumed.
You should also be reasonably comfortable using the following technologies.
* Java SDK 1.5/1.6
* GlassFish V2 UR1 (comes with NetBeans 6.7)
* Spring Web MVC Framework (comes with NetBeans 6.7)
* NetBeans 6.7

Chapter 1 Spring Web MVC Complete Web Application ‘AyumiloveSpring1’ can be downloaded here.

Appendix A. References


Original version of this tutorial based on Eclipse and Tomcat.
Modified version of this tutorial based on Netbeans and GlassFish.
NetBeans IDE 6.7 Download
Spring Framework Download

Chapter 2 Spring Web MVC Tutorial for Newbies
2.1. Configure JSTL and add JSP header file
2.2. Improve the controller
2.3. Decouple the view from the controller
2.4. Summary

Chapter 2. Developing and Configuring the Views and the Controller

This is Part 2 of a step-by-step tutorial on how to develop a web application from scratch using the Spring Framework. In Part 1 we configured the environment and set up a basic application that we will now flesh out.

This is what we have implemented so far:
Part 1: index.jsp
An introduction page, ‘index.jsp’, the welcome page of the application.
It was used to test our setup was correct.
We will later change this to actually provide a link into our application.
Part 2: springapp-servlet.xml
A DispatcherServlet (front controller) with a corresponding ‘springapp-servlet.xml’ configuration file.
A page controller, HelloController, with limited functionality – it just returns a ModelAndView.
We currently have an empty model and will be providing a full model later on.
Part 3: HelloControllerTests
A unit test class for the page controller, HelloControllerTests,
to verify the name of the view is the one we expect.
Part 4: Hello.jsp
A view, ‘hello.jsp’, that again is extremely basic.
The good news is the whole setup works and we are now ready to add more functionality.

Advertisements

Ayumilove Develop a Spring Framework MVC Application using NetBeans, GlassFish and MySQL (Step by Step) Dummies Guide to Spring MVC (Chapter 1/6)

Developing a Spring Framework MVC application step-by-step using NetBeans and GlassFish
Authors Thomas Risberg, Rick Evans, Portia Tung

Modified by Ayumilove for the 3 Open Source Softwares below:
NetBeans 6.7 (IDE – Integrated Development Environment to develop web applications and software)
GlassFish (Application Server – Consists of Web Container, JavaBean Container)
MySQL (RDBMS – Relational Database Management System)

Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically.

Prerequisite software
The following prerequisite software and environment setup is assumed.
You should also be reasonably comfortable using the following technologies.
* Java SDK 1.5/1.6
* GlassFish V2 UR1 (comes with NetBeans 6.7)
* Spring Web MVC Framework (comes with NetBeans 6.7)
* NetBeans 6.7

Chapter 1 Spring Web MVC Complete Web Application ‘AyumiloveSpring1’ can be downloaded here.

Appendix A. References


Original version of this tutorial based on Eclipse and Tomcat.
Modified version of this tutorial based on Netbeans and GlassFish.
NetBeans IDE 6.7 Download
Spring Framework Download

Chapter 1 Spring Web MVC Tutorial for Newbies
1.1 Create the Netbeans Spring Project
1.2. Create ‘index.jsp’
1.3. Deploy the application to GlassFish
1.4. Check the application works
1.5. Download the Spring Framework
1.6. Modify ‘web.xml’ in the ‘WEB-INF’ directory
1.7. Spring libraries
1.8. Create the Controller
1.9. Write a test for the Controller
1.10. Create the View
1.11. Compile and deploy the application
1.12. Try out the application
1.13. Summary

Summary of Chapter 1
Chapter 1 of this tutorial walks you through on how to setup a simple spring web mvc application, which consists of a front controller (also known as dispatcher servlet), a view (hello.jsp) and a controller (HelloController.java). It also guides on how to create a testing class (HelloControllerTests.java) in order to check the logic behind HelloController whether it works smoothly or does not produce expected results.
Read More…

Spring MVC Newbie Tutorial 1- Hello World (NetBeans 6.7 + GlassFish v3 Prelude)

Spring MVC Newbie Tutorial 1- Hello World


This document takes you through the basics of using NetBeans IDE to develop MVC web application. It demonstrates how to create a simple web application (uses model, view, controller design pattern), deploy it to a server, and view its presentation in a browser.

Requirement(s)


To follow this tutorial, you need the following software and resources.
NetBeans IDE – Java or Web & Java EE installation (NetBeans 6.1, 6.5 or 6.7)
Java Development Kit (JDK) – (JDK version 6 or version 5)
GlassFish application server – (GF Version 2 or Version 3 Prelude)

How Spring MVC works


Basically the same way as Struts:
Spring Struts Structure Flow

Based on the HTTP request URL, the DispatcherServlet calls the corresponding Controller.
A View is rendered and sent as HTTP response.

Spring Servlet Configuration


Let’s now create the Spring configuration file ‘WEB-INF/springmvc-servlet.xml’
(name based on the servlet name above):

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
 
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
 
  <welcome-file-list>
    <welcome-file>
      jsp/index.jsp
    </welcome-file>
  </welcome-file-list>
 
</web-app>

Map URL /hello_world.html to Controller HelloWorldController
Declare View Resolver: when view ‘view_name’ is called (from the Controller),
the file ‘/jsp/view_name.jsp’ will be used.

Controller


Let’s create the Controller ‘WEB-INF/src/springmvc/web/HelloWorldController.java’:

package springmvc.web;
 
import java.io.IOException;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
 
public class HelloWorldController implements Controller {
 
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
 
		String aMessage = "Hello World MVC!";
 
		ModelAndView modelAndView = new ModelAndView("hello_world");
		modelAndView.addObject("message", aMessage);
 
		return modelAndView;

This Controller calls the view ‘hello_world’, passing ‘message’ to it (like a Struts attribute).

View


Now the view: ‘jsp/hello_world.jsp’:




	<p>This is my message: ${message}</p>


Summary

Reference


Spring MVC Fast Tutorial (Apache Tomcat)