SolveItproblems

SolveITProblems is a project of QFormat.net intellectual club

SolveITProblems это проект интеллектуального клуба QFormat.net

суббота, 23 марта 2013 г.

Maven + Spring MVC. Step by Step

Hi.
Are you ready to test your first Maven+Spring MVC web-app?

So you need only 7 magical steps.

Step 1.
Create new maven web-application in netbeans or any other IDE



Step 2.
Editing your POM.XML
//Adding Spring MVC
...
            <dependency>  
       <groupId>org.springframework</groupId>  
       <artifactId>spring-webmvc</artifactId>  
       <version>3.0.6.RELEASE</version>  
     </dependency>  
   </dependencies>  

//Adding Jetty as a web-serer
  <plugin>  
         <groupId>org.mortbay.jetty</groupId>  
         <artifactId>jetty-maven-plugin</artifactId>  
       </plugin>  
Step 3.
Create web.xml under WEB-INF and configure it to catch all incomig requests by Spring's DispatcherServlet:

  <servlet>  
     <servlet-name>springMVC</servlet-name>  
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
   </servlet>  
   <servlet-mapping>  
     <servlet-name>springMVC</servlet-name>  
     <url-pattern>*.htm</url-pattern>  
   </servlet-mapping>  

Step 4.
Another XML configuration file under the WEB-INF: springMVC-servlet.xml:



 <?xml version="1.0" encoding="UTF-8"?>  
 <beans xmlns="http://www.springframework.org/schema/beans"  
      xmlns:context="http://www.springframework.org/schema/context"  
      xmlns:mvc="http://www.springframework.org/schema/mvc"   
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xsi:schemaLocation="  
     http://www.springframework.org/schema/beans     
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
     http://www.springframework.org/schema/context   
     http://www.springframework.org/schema/context/spring-context-3.0.xsd  
     http://www.springframework.org/schema/mvc  
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
 //this code will scan all your package to find Contollers  
   <context:component-scan base-package="com.bitep.mavenspring" />  
 //here you indicate that you will use jsp files as views at this directory - WEB-INF/jsp/   
 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
   <property name="prefix" value="/WEB-INF/jsp/"></property>  
   <property name="suffix" value=".jsp"></property>  
 </bean>  
 </beans>  

Step 5.
Lets create our first Controller. Create a Java file at the package you have indicated upper at  "context"

 package com.bitep.mavenspring;  
 import org.springframework.stereotype.Controller;  
 import org.springframework.web.bind.annotation.RequestMapping;  
 import org.springframework.web.servlet.ModelAndView;  
 /**  
  *  
  * @author Vusal Khalilov  
  */  
 @Controller  
 @RequestMapping(value="test")  
 public class TestController {  
   @RequestMapping(value="")  
   public ModelAndView hello(){  
     ModelAndView mav = new ModelAndView();  
     mav.setViewName("test");  
     return mav;  
   }  
 }  

Step 6.
Its time for View. Create folder named "jsp" under the WEB-INF. (We show this path at Step 4.)
Create file named "test.jsp" , write anything you want in it :)

Step 7.
You can run it with jetty:run command, or simply Run it with your IDE. If you write http://localhost:8080/test.htm you'll be redirected to your view.

Be happy!

Комментариев нет:

Отправка комментария