SolveITProblems is a project of intellectual club

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

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

Maven + Spring MVC. Step by Step

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

//Adding Jetty as a web-serer
Step 3.
Create web.xml under WEB-INF and configure it to catch all incomig requests by Spring's DispatcherServlet:


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

 <?xml version="1.0" encoding="UTF-8"?>  
 <beans xmlns=""  
      xsi:schemaLocation=" ">  
 //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>  

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  
 public class TestController {  
   public ModelAndView hello(){  
     ModelAndView mav = new ModelAndView();  
     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!

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

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