Wednesday, May 4, 2016

Starting with: a dynamic web project with Spring 3 MVC

Now we can see a rapid guide to quickly see how to run a simple dynamic web project using project Spring MVC 3. The very essential to see how it works.

- open eclipse
- create a new dynamic web project(in my case I set 2.5 value into Dynamic web module version combo selection)
- this is the content of web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Spring3 MVC Application</display-name>

    <servlet>
        <servlet-name>spring-web</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring-web</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>


- always here, under WEB-INF, create a new spring-web-servlet.xml
(note that name first part"spring-web" have to be the same of what you declared as servlet-name in the web.xml mapping to the DispatcherServlet)
with this content:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <context:component-scan base-package="com.web" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/jsp/" />          
        <property name="suffix" value=".jsp" />      
    </bean>

    <mvc:annotation-driven />
  
</beans>


- under src folder, create a new package named: com.web.controller

- and put into WebController.java:

package com.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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;

@Controller
public class WebController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Spring 3 MVC Example");
        return "home";
    }

    @RequestMapping(value = "/main/{name:.+}", method = RequestMethod.GET)
    public ModelAndView hello(@PathVariable("name") String name) {
        ModelAndView model = new ModelAndView();
        model.setViewName("main");
        model.addObject("msg", name);
        return model;
    }

}

- under WEB-INF/lib put into these libs(you need to search and download all of them):
aopalliance-1.0.jar
commons-logging-1.1.3.jar
jstl-1.2.jar
spring-aop-3.2.13.RELEASE.jar
spring-beans-3.2.13.RELEASE.jar
spring-context-3.2.13.RELEASE.jar
spring-core-3.2.13.RELEASE.jar
spring-expression-3.2.13.RELEASE.jar
spring-web-3.2.13.RELEASE.jar
spring-webmvc-3.2.13.RELEASE.jar

- create the folder path WEB-INF/views/jsp
- put into these two jsp: home.jsp(welcome page)
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Spring MVC 3 example</title>
    </head>
    <body>
        <div>
            <div>              
                <p>
                    <c:if test="${not empty name}">
                        Welcome to main menu ${name}
                    </c:if>
                </p>
            </div>
            <a href="${pageContext.request.contextPath}/main/maurizio">go to main menu</a>
        </div>
    </body>
</html>


- and main.jsp(target page, only to see the navigation):

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Spring MVC 3 example</title>
    </head>
    <body>
        <div>
            <p>
                <c:if test="${not empty name}">
                    Welcome! Please click above for the main menu... ${name}
                </c:if>       
            </p>           
        </div>
    </body>
</html>


So now you can try all:
- building a war file through right clicking on the project, then Export --> find and select War file, and moving the war file under an active application server.
- configuring an application server inbound to eclipse and adding the entire project to it.

Indipendently to the choosen way open a browser and point to:
http://localhost:8080/startingWithSpringMVC3
and test if all is ok....

You checkout the entire project(under sourceforge.net) from command line with this:
svn checkout svn://svn.code.sf.net/p/mauriziofranco/code/StartingWithSpringMVC3/trunk/startingWithSpringMVC3 mauriziofranco-code

That's all....
Bye...


No comments:

Post a Comment