使用eclipse创建一个maven+springMVC工程

1、创建一个maven项目




2.选择一个workspace


3.Artifact Id为项目名  packaging选择war包 ,然后finsh



4.建立好项目后打开 ,发现jdk(javaSE-1.5/javaSE-1.7)需要我们自己更换,点击项目右键 Build Path ,删除javaSE-1.5或javaSE-1.7


扫描二维码关注公众号,回复: 2955951 查看本文章




换成我们自己安装的1.7版本jdk




java compiler这里也换一下版本:



至此,我们的maven项目创建完成,下面开始整合springMVC

5.pom.xml中设置依赖的jar包


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>demo</groupId>
  <artifactId>test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<!-- 设置 Spring 的版本 -->
		<spring.vesion>4.0.4.RELEASE</spring.vesion>
	</properties>
	
	<dependencies>  
        <!-- junit -->  
        <dependency>  
            <groupId>junit</groupId>  
            <artifactId>junit</artifactId>  
            <version>4.12-beta-3</version>  
            <scope>test</scope>  
        </dependency>  
        <!-- log4j -->  
        <dependency>  
            <groupId>log4j</groupId>  
            <artifactId>log4j</artifactId>  
            <version>1.2.17</version>  
        </dependency>  
          
        <dependency>  
            <groupId>jstl</groupId>  
            <artifactId>jstl</artifactId>  
            <version>1.2</version>  
        </dependency>  
          
        <!-- spring 的基本依赖 开始 -->  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-core</artifactId>  
            <version>4.1.2.RELEASE</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-expression</artifactId>  
            <version>4.1.2.RELEASE</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-beans</artifactId>  
            <version>4.1.2.RELEASE</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-aop</artifactId>  
            <version>4.1.2.RELEASE</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context</artifactId>  
            <version>4.1.2.RELEASE</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context-support</artifactId>  
            <version>4.1.2.RELEASE</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-tx</artifactId>  
            <version>4.1.2.RELEASE</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-web</artifactId>  
            <version>4.1.2.RELEASE</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-jdbc</artifactId>  
            <version>4.1.2.RELEASE</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-webmvc</artifactId>  
            <version>4.1.2.RELEASE</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-aspects</artifactId>  
            <version>4.1.2.RELEASE</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-test</artifactId>  
            <version>4.1.2.RELEASE</version>  
        </dependency>  
        <!-- spring 的基本依赖 结束 -->  
  </dependencies>  
</project>
6.webapp添加WEB-INF文件夹,依次添加xml文件web.xml

这里的<param-value>classpath:spring/ApplicationContext-mvc.xml</param-value> 配置将在下一步配置好

<?xml version="1.0" encoding="UTF-8"?>  
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
    version="2.5">  
    <!-- 区分项目名称,防止默认重名 -->    
    <context-param>    
        <param-name>test</param-name>    
        <param-value>maven.example.root</param-value>    
    </context-param>    
    
    <!-- Spring的log4j监听器 -->    
    <listener>    
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>    
    </listener>    
    
    <!-- 字符集 过滤器  -->    
    <filter>    
        <filter-name>CharacterEncodingFilter</filter-name>    
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    
        <init-param>    
            <param-name>encoding</param-name>    
            <param-value>UTF-8</param-value>    
        </init-param>    
        <init-param>    
            <param-name>forceEncoding</param-name>    
            <param-value>true</param-value>    
        </init-param>    
    </filter>    
    <filter-mapping>    
        <filter-name>CharacterEncodingFilter</filter-name>    
        <url-pattern>/*</url-pattern>    
    </filter-mapping>    
    
    <!-- Spring view分发器 -->    
    <servlet>    
        <servlet-name>springMvc</servlet-name>    
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
        <init-param>    
            <param-name>contextConfigLocation</param-name>    
            <param-value>classpath:spring/ApplicationContext-mvc.xml</param-value>    
        </init-param>    
        <load-on-startup>1</load-on-startup>    
    </servlet>    
    <servlet-mapping>    
        <servlet-name>springMvc</servlet-name>    
        <url-pattern>*.do</url-pattern>    
    </servlet-mapping>    
  
</web-app>  

7.在resource下创建spring文件夹,并创建ApplicationContext-mvc.xml

这里扫描包暂时是com.test.controller,返回的jsp页面需要在WEB-INF下新建一个jsp文件夹存放jsp页面

<?xml version="1.0" encoding="UTF-8"?>    
<beans xmlns="http://www.springframework.org/schema/beans"     
       xmlns:aop="http://www.springframework.org/schema/aop"     
       xmlns:context="http://www.springframework.org/schema/context"    
       xmlns:mvc="http://www.springframework.org/schema/mvc"     
       xmlns:tx="http://www.springframework.org/schema/tx"     
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
       xsi:schemaLocation="http://www.springframework.org/schema/aop     
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd     
        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     
        http://www.springframework.org/schema/tx     
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">    
    
    <mvc:annotation-driven />    
    <context:component-scan base-package="com.test.controller"/>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">    
        <property name="prefix" value="/WEB-INF/jsp/" />    
        <property name="suffix" value=".jsp" />    
    </bean>    
    
</beans>  

8.分别新建一个controller和jsp页面



controlller:

package com.test.controller;

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

@Controller 
public class TestController {
	@RequestMapping(value="index.do")    
    public ModelAndView index_jsp(ModelAndView mv){    
		mv.addObject("test", "Hellow world");    
        System.out.println("index.jsp");    
        mv.setViewName("index");
        return mv;
    }    
}
jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>    
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">    
<html>    
    <head>    
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    
        <title>test</title>    
    </head>    
        
    <body>    
       ${test}
    </body>    
</html> 

9.最后一步,运行测试:


10.mavem打war也十分方便,自动打入workspace,直接右击pom.xml→run as → maven install

控制台会输出信息



再看看我们的项目结构:


猜你喜欢

转载自blog.csdn.net/yufeng005/article/details/77581685