eclipse maven搭建springmvc

1、新建一个Maven Project

eclipse maven搭建springmvc_第1张图片

 2、选择工作空间

eclipse maven搭建springmvc_第2张图片

 3、搭建Web工程,我们选择maven-archetype-webapp类型

eclipse maven搭建springmvc_第3张图片

 4、填写项目参数,如图

eclipse maven搭建springmvc_第4张图片

 5、以上步骤完成时的工程结构目录

eclipse maven搭建springmvc_第5张图片

添加source folder

eclipse maven搭建springmvc_第6张图片

6 转换成web工程

eclipse maven搭建springmvc_第7张图片

7、将WebContent下的META-INF移动到webapp目录下,然后删除WebContent

eclipse maven搭建springmvc_第8张图片

8、修改pom.xml,引入依赖包


  4.0.0
  com.test.springmvc
  SpringMVC
  war
  0.0.1-SNAPSHOT
  SpringMVC Maven Webapp
  http://maven.apache.org
    
      4.1.1.RELEASE
  
  
  
    
      junit
      junit
      3.8.1
      test
    
    
    
        org.springframework
        spring-core
        ${spring.version}
    
    
        org.springframework
        spring-web
        ${spring.version}
    
    
        org.springframework
        spring-webmvc
        ${spring.version}
    
    
    
        javax.servlet
        javax.servlet-api
        3.1.0
    
  
  
    
          
              
                maven-compiler-plugin  
                2.3.2  
                  
                    1.7  
                    1.7  
                  
              
              
                maven-war-plugin  
                2.2  
                  
                    3.0  
                    false  
                  
              
          
        ${project.artifactId}_${project.version}_${maven.build.timestamp}  
     

9、修改web.xml



    
    
    
    
        contextConfigLocation
        classpath*:applicationContext.xml
    
    
    
        org.springframework.web.context.ContextLoaderListener
    
   
    
    
    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
          
            contextConfigLocation  
            
            classpath*:spring-mvc.xml
          
        1  
    
    
        springmvc
        /
    
    
     
      
    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
    
    
        encodingFilter
        /*
    
    
    
      
        index.jsp  
      

10、新建applicationContext.xml和spring-mvc.xml

applicationContext.xml:




    
    
        
    
     
    

spring-mvc.xml:



    
      
      
    
    
      
    	
	
    
       
      
          
          
      
    
    
11、修改发布目录
eclipse maven搭建springmvc_第9张图片

框架搭建结束

验证:

创建HelloController.java:

package com.test.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.test.service.HelloService;

@Controller
@RequestMapping(value="/")
public class HelloController {

	@Autowired
	private HelloService helloService;
	
	@RequestMapping(value="hello")
	public String hello() {
		System.out.println("contorller say hello");
		helloService.hello();
		return "hello";
	}
}

创建HelloService.java:

package com.test.service;

import org.springframework.stereotype.Service;

@Service
public class HelloService {

	public void hello() {
		System.out.println("service say hello");
	}
}

新建hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>




Insert title here


hello

发布到tomcat启动,然后访问链接 http://localhost:9091/SpringMVC/hello

浏览器显示hello,后台输出:

eclipse maven搭建springmvc_第10张图片


你可能感兴趣的:(spring)