We are what we repeatedly do. Excellence, therefore, is not an act, but a habit.
                    - Aristotle
 
Search
Search

 
 
Navigation menu
Navigation menu
 
 
 
 
Size_box_tl   Size_box_tr
 

Spring 3 maven 2 web application step by step

Before writting this I admin that there are lots of tutorials like this but what I found is that they are not step by step guides, with missing vital parts of process of making it all happen from scratch. This makes you revert to the documentation and therefore defies the point of going through the tutorial. With this one I hope to make it a bit clearer, though I do not attempt to make it IDE specific, therefore thre will be no guidance on how to do it in specific IDE. The layout will be to work with standard Maven project structure.

First things first - the POM:


  4.0.0
  dp.examples
  shoppingcart
  0.0.1-SNAPSHOT
  war
  ShoppingCart
  
  
  	
  		
  			maven-compiler-plugin
  			
  				1.5
  				1.5
  			
  		
  	
  
  
  
	
	    3.0.5.RELEASE
	
  
  
  
	  
	      log4j
	      log4j
	      1.2.15
	      
	        
	          javax.mail
	          mail
	        
	        
	          javax.jms
	          jms
	        
	        
	          com.sun.jdmk
	          jmxtools
	        
	        
	          com.sun.jmx
	          jmxri
	        
	      
	      runtime
	    
  
  
       
  

		
		
		  org.springframework
		  spring-core
		  ${org.springframework.version}
		
		
		
		
		  org.springframework
		  spring-expression
		  ${org.springframework.version}
		
		
		
		
		  org.springframework
		  spring-beans
		  ${org.springframework.version}
		
		
		
		
		  org.springframework
		  spring-aop
		  ${org.springframework.version}
		
		
		
		
		  org.springframework
		  spring-context
		  ${org.springframework.version}
		
		
		
		
		  org.springframework
		  spring-context-support
		  ${org.springframework.version}
		
		
		
		
		  org.springframework
		  spring-tx
		  ${org.springframework.version}
		
		
		
		
		  org.springframework
		  spring-jdbc
		  ${org.springframework.version}
		
		
		
		
		  org.springframework
		  spring-orm
		  ${org.springframework.version}
		
		
		
		
		  org.springframework
		  spring-oxm
		  ${org.springframework.version}
		
		
		
		
		  org.springframework
		  spring-web
		  ${org.springframework.version}
		
		
		
		
		  org.springframework
		  spring-webmvc
		  ${org.springframework.version}
		
		
		
		
		  org.springframework
		  spring-webmvc-portlet
		  ${org.springframework.version}
		
		
		
		
		  org.springframework
		  spring-test
		  ${org.springframework.version}
		  test
		
		  	
  

At this point we have done all we need maven wise and can safely proceed to next step.

BTW if you are using eclipse and started of with a maven2 project and now trying to convert it to web projects I think you need to read this

We are making a webapp, so the second thing is web.xml (That should be in the src/main/webapp/WEB-INF/):




  
  
    log4jConfigLocation
    classpath:META-INF/properties/log4j.properties
  
  
    org.springframework.web.util.Log4jConfigListener
  
  
  
  
    contextConfigLocation
    classpath*:WEB-INF/spring/*.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  
  
  
  
    spring
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      /WEB-INF/spring/*.xml
    
    1
  
  
  
    spring
    /
  

  
    
      index.jsp
    
  


Then the Spring context configuration (for webapp that is located in WEB-INF/spring/ directory as specified in web.xml parameters to the servlet):




	
	
		
	
	
	
	
		
	
	
	
	

	
	

	
	

		
	
		
		
	

	
	

	
	

	
	
		
		
	

	
	
		
		
	



Basic controller:

package dp.example.shoppingcart.web;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import dp.example.shoppingcart.dto.Item;
import dp.example.shoppingcart.service.CartService;

/**
 * Basic controller
 * 
 * @author DPavlov
 */
@Controller
public class CartController {
	
	private CartService cartService;
	
	private int counter = 1;

	@Autowired
	public CartController(CartService cartService) {
		this.cartService = cartService;
	}
	
	@RequestMapping(value = "/cart", method = RequestMethod.GET)
	public ModelAndView getCart() {
		
		final List list = cartService.getItemsInCart();
		ModelAndView mav = new ModelAndView("cart");
		mav.addObject("items", list);
		mav.addObject("add", "Item" + counter++);
		return mav;
		
	}
	
	@RequestMapping(value = "/cart/addtocart/{item}", method = RequestMethod.GET)
	public ModelAndView addToCart(@PathVariable final String item) {
		
		cartService.addToCart(item);
		return getCart();
		
	}
	
	@RequestMapping(value = "/cart/removefromcart/{item}", method = RequestMethod.GET)
	public ModelAndView removeFromCart(@PathVariable final String item) {
		
		cartService.removeFromCart(item);
		return getCart();
		
	}
	

}

Basic view (if you look at the spring context.xml file you will see that we are using basic spring resolver that look for views in WEB-INF/views/ and uses view name as file name with extension .jsp):

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

Items

  Items:
  
	${item.articleNo} @ ${item.price}, qty: ${item.quantity}  
			[-] remove 
			[+] add
	
  
  add "${add}"


Here is the full project hierarchy for the above classes:

Here is link to the full source for this:

denis-pavlov-spring3mvc-mvn2-example.zip

Here is screenshot of the outcome:

PLEASE BARE IN MIND THIS IS AN EXAMPLE TO SHOW HOW SPRING 3 MVC WORKS - THIS IS NOT PRODUCTION CODE AND NEVER WAS INTENDED TO IMMITATE ONE.



© Inspire Software, Denys Pavlov, 2005-2012
© Inspire Software, Denys Pavlov, 2005-2012
 
Size_box_bl   Size_box_br