Friday 20 June 2014

Spring Web App Template

I don't create new web apps that often and therefore can easily forget the necessary bits!  This is my memory jogger for how do to these.

Notes: In eclipse create a new java project with the correct maven folder structure.  This is the sample folder structure


Location: project_home/pom.xml
Notes: The basic maven pom which has the correct values for the spring classes although the spring versions can be updated. 

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">


    <name>Sample Web App</name>
    <description />



        <!-- test -->



Notes: After putting this pom in place convert to a maven project.  In eclipse right click on the project and go to Configure - Convert to Maven Project.  This should create a folder called Maven Dependencies which contains all the libraries defined by the pom.
Right click on the project and go to properties.  
 - Select 'Project Facets' and make sure it is a faceted form with Dynamic Web Module, Java and JavaScript are all checked.
 - Click on Deployment Assembly and make sure that Source /src/main/webapp is mapped to Deploy Path / and also that Source: Maven Dependencies are mapped to Deploy Path /WEB-INF/lib.
 - Click on Web Project Settings and check the context-root value
 - Click on Java Build Path and add main/java, main/resources, test/java and test/resources as source folders.

Location: project_home/src/main/webapp/WEB-INF/web.xml
Notes: This is the file that is read by the application server to determine details about the servlet it needs to run.  It contains the main servlet class and mappings of which requests get sent to this servlet.  In this case the servlet is the spring MessageDispatchServlet as this is a spring app.
 <web-app xmlns="" xmlns:xsi=""  
  <display-name>Web App</display-name>

Location: project_home/src/mainwebapp/WEB-INF/servlet.xml
Notes: The servlet xml is the file that is used by the spring MessageDispatcherServlet and is the root of the spring configuration for the web application.

<beans xmlns=""  
    xsi:schemaLocation="          ">  
    <context:component-scan base-package="" />  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix">  
        <property name="suffix">  

Location: project_home/src/main/java/com/me/app/web/controller/
Notes: This is a sample controller that maps to a jsp.


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

 * Sample controller.
public class Greeting
     * Method to say hello.
    public ModelAndView hello()
        final ModelAndView mav = new ModelAndView("hello");
     return mav;

Location: project_home/src/webapp/WEB-INF/jsp/hello.jsp
Notes: This is a very simple jsp file that is used to just smoke test that the application is running.

    <title>Sample Web App</title>
      <h2>Sample Web App</h2>  

No comments:

Post a Comment