maven 搭建springmvc web项目

不了解maven的先看这篇文章进行了解  http://blog.csdn.net/xin917480852/article/details/52679319


开发环境:myeclipse2016 

Maven部分

1.配置maven的JDK

maven 搭建springmvc web项目_第1张图片

2.配置本地Maven

maven 搭建springmvc web项目_第2张图片

3.配置本地仓库以及Maven的Setting.xml位置

maven 搭建springmvc web项目_第3张图片


WEB项目的创建

New----web project

maven 搭建springmvc web项目_第4张图片

一直next记得勾选这一个

maven 搭建springmvc web项目_第5张图片

maven 搭建springmvc web项目_第6张图片

一直next直到Finish,创建出来的项目结构如下图

maven 搭建springmvc web项目_第7张图片


Springmvc部分

在maven的pom.xml中添加我们所需要的依赖库,如果本地仓库没有的话我们会去全球中央仓库下载

pom.xml中这个配置

[html] view plain copy
  1. <span style="font-size:18px;"><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">  
  2.   <modelVersion>4.0.0modelVersion>  
  3.   <groupId>springmvctestgroupId>  
  4.   <artifactId>springmvctestartifactId>  
  5.   <version>0.0.1-SNAPSHOTversion>  
  6.   <packaging>warpackaging>  
  7.   <name>springmvctestname>  
  8.   <description/>  
  9.   <properties>  
  10.     <webVersion>3.0webVersion>  
  11.     <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>  
  12.     <spring.version>4.1.1.RELEASEspring.version>  
  13.           <cxf.version>2.7.15cxf.version>  
  14.         <slf4j-version>1.7.12slf4j-version>  
  15.         <log4j-version>1.2.17log4j-version>  
  16.         <mybatis-version>3.3.0mybatis-version>  
  17.         <mybatis-spring-version>1.2.3mybatis-spring-version>  
  18.   properties>  
  19.   <dependencies>  
  20.     <dependency>  
  21.       <groupId>org.glassfishgroupId>  
  22.       <artifactId>bean-validatorartifactId>  
  23.       <version>3.0-JBoss-4.0.2version>  
  24.       <scope>providedscope>  
  25.     dependency>  
  26.     <dependency>  
  27.       <groupId>org.glassfishgroupId>  
  28.       <artifactId>javax.enterprise.deployartifactId>  
  29.       <version>3.0.1version>  
  30.       <scope>providedscope>  
  31.     dependency>  
  32.     <dependency>  
  33.       <groupId>org.glassfishgroupId>  
  34.       <artifactId>javax.jmsartifactId>  
  35.       <version>3.0.1version>  
  36.       <scope>providedscope>  
  37.     dependency>  
  38.     <dependency>  
  39.       <groupId>org.glassfishgroupId>  
  40.       <artifactId>javax.management.j2eeartifactId>  
  41.       <version>3.0.1version>  
  42.       <scope>providedscope>  
  43.     dependency>  
  44.     <dependency>  
  45.       <groupId>org.eclipse.persistencegroupId>  
  46.       <artifactId>javax.persistenceartifactId>  
  47.       <version>2.0.0version>  
  48.       <scope>providedscope>  
  49.     dependency>  
  50.     <dependency>  
  51.       <groupId>org.glassfishgroupId>  
  52.       <artifactId>javax.resourceartifactId>  
  53.       <version>3.0.1version>  
  54.       <scope>providedscope>  
  55.     dependency>  
  56.     <dependency>  
  57.       <groupId>org.glassfishgroupId>  
  58.       <artifactId>javax.security.auth.messageartifactId>  
  59.       <version>3.0.1version>  
  60.       <scope>providedscope>  
  61.     dependency>  
  62.     <dependency>  
  63.       <groupId>org.glassfishgroupId>  
  64.       <artifactId>javax.security.jaccartifactId>  
  65.       <version>3.0.1version>  
  66.       <scope>providedscope>  
  67.     dependency>  
  68.     <dependency>  
  69.       <groupId>org.glassfishgroupId>  
  70.       <artifactId>javax.servletartifactId>  
  71.       <version>3.0.1version>  
  72.       <scope>providedscope>  
  73.     dependency>  
  74.     <dependency>  
  75.       <groupId>org.glassfishgroupId>  
  76.       <artifactId>javax.servlet.jspartifactId>  
  77.       <version>3.0.1version>  
  78.       <scope>providedscope>  
  79.     dependency>  
  80.     <dependency>  
  81.       <groupId>org.glassfishgroupId>  
  82.       <artifactId>javax.servlet.jsp.jstlartifactId>  
  83.       <version>3.0.1version>  
  84.       <scope>providedscope>  
  85.     dependency>  
  86.     <dependency>  
  87.       <groupId>javax.xml.bindgroupId>  
  88.       <artifactId>jaxb-api-osgiartifactId>  
  89.       <version>2.2.1version>  
  90.       <scope>providedscope>  
  91.     dependency>  
  92.     <dependency>  
  93.       <groupId>javax.ws.rsgroupId>  
  94.       <artifactId>jsr311-apiartifactId>  
  95.       <version>1.1.1version>  
  96.       <scope>providedscope>  
  97.     dependency>  
  98.     <dependency>  
  99.       <groupId>org.glassfish.webgroupId>  
  100.       <artifactId>jstl-implartifactId>  
  101.       <version>1.2version>  
  102.       <scope>providedscope>  
  103.     dependency>  
  104.     <dependency>  
  105.       <groupId>javax.mailgroupId>  
  106.       <artifactId>mailartifactId>  
  107.       <version>1.4.3version>  
  108.       <scope>providedscope>  
  109.     dependency>  
  110.     <dependency>  
  111.       <groupId>javax.xmlgroupId>  
  112.       <artifactId>webservices-api-osgiartifactId>  
  113.       <version>2.0.1version>  
  114.       <scope>providedscope>  
  115.     dependency>  
  116.     <dependency>  
  117.       <groupId>org.jboss.weldgroupId>  
  118.       <artifactId>weld-osgi-bundleartifactId>  
  119.       <version>1.0.1-SP3version>  
  120.       <scope>providedscope>  
  121.     dependency>  
  122.     <dependency>  
  123.       <groupId>org.glassfish.webgroupId>  
  124.       <artifactId>javax.servlet.jsp.jstlartifactId>  
  125.       <version>1.2.1version>  
  126.     dependency>  
  127.        
  128.     <dependency>  
  129.         <groupId>log4jgroupId>  
  130.         <artifactId>log4jartifactId>  
  131.         <version>${log4j-version}version>  
  132.     dependency>  
  133.     <dependency>  
  134.         <groupId>org.slf4jgroupId>  
  135.         <artifactId>slf4j-log4j12artifactId>  
  136.         <version>${slf4j-version}version>  
  137.     dependency>  
  138.       <dependency>  
  139.         <groupId>org.slf4jgroupId>  
  140.         <artifactId>slf4j-apiartifactId>  
  141.         <version>${slf4j-version}version>  
  142.     dependency>  
  143.     <dependency>  
  144.         <groupId>javax.servletgroupId>  
  145.         <artifactId>javax.servlet-apiartifactId>  
  146.         <version>3.1.0version>  
  147.     dependency>  
  148.     <dependency>  
  149.        <groupId>org.apache.commonsgroupId>  
  150.        <artifactId>commons-lang3artifactId>  
  151.        <version>3.3version>  
  152.     dependency>  
  153.     <dependency>  
  154.        <groupId>com.alibabagroupId>  
  155.        <artifactId>fastjsonartifactId>  
  156.        <version>1.2.1version>  
  157.     dependency>  
  158.       
  159.     <dependency>  
  160.         <groupId>com.mchangegroupId>  
  161.         <artifactId>c3p0artifactId>  
  162.         <version>0.9.5.1version>  
  163.     dependency>  
  164.       
  165.     <dependency>  
  166.         <groupId>mysqlgroupId>  
  167.         <artifactId>mysql-connector-javaartifactId>  
  168.         <version>5.1.37version>  
  169.     dependency>  
  170.     <dependency>  
  171.         <groupId>org.mybatisgroupId>  
  172.         <artifactId>mybatis-springartifactId>  
  173.         <version>${mybatis-spring-version}version>  
  174.     dependency>  
  175.     <dependency>  
  176.         <groupId>org.mybatisgroupId>  
  177.         <artifactId>mybatisartifactId>  
  178.         <version>${mybatis-version}version>  
  179.     dependency>  
  180.     <dependency>  
  181.         <groupId>org.mybatis.generatorgroupId>  
  182.         <artifactId>mybatis-generator-coreartifactId>  
  183.         <version>1.3.2version>  
  184.     dependency>  
  185.       
  186.     <dependency>  
  187.         <groupId>org.springframeworkgroupId>  
  188.         <artifactId>spring-webartifactId>  
  189.         <version>${spring.version}version>  
  190.     dependency>  
  191.     <dependency>  
  192.         <groupId>org.springframeworkgroupId>  
  193.         <artifactId>spring-coreartifactId>  
  194.         <version>${spring.version}version>  
  195.     dependency>  
  196.     <dependency>  
  197.         <groupId>org.springframeworkgroupId>  
  198.         <artifactId>spring-contextartifactId>  
  199.         <version>4.1.1.RELEASEversion>  
  200.     dependency>  
  201.     <dependency>  
  202.         <groupId>org.springframeworkgroupId>  
  203.         <artifactId>spring-beansartifactId>  
  204.         <version>${spring.version}version>  
  205.     dependency>  
  206.     <dependency>  
  207.         <groupId>org.springframeworkgroupId>  
  208.         <artifactId>spring-jdbcartifactId>  
  209.         <version>${spring.version}version>  
  210.     dependency>  
  211.     <dependency>  
  212.         <groupId>org.springframeworkgroupId>  
  213.         <artifactId>spring-webmvcartifactId>  
  214.         <version>${spring.version}version>  
  215.     dependency>  
  216.     <dependency>  
  217.         <groupId>org.springframeworkgroupId>  
  218.         <artifactId>spring-txartifactId>  
  219.         <version>${spring.version}version>  
  220.     dependency>  
  221.       
  222.     <dependency>  
  223.         <groupId>junitgroupId>  
  224.         <artifactId>junitartifactId>  
  225.         <version>4.11version>  
  226.         <scope>testscope>  
  227.     dependency>  
  228.     <dependency>  
  229.         <groupId>org.springframeworkgroupId>  
  230.         <artifactId>spring-testartifactId>  
  231.         <version>4.1.1.RELEASEversion>  
  232.         <scope>testscope>  
  233.     dependency>  
  234.   
  235.     <dependency>  
  236.       <groupId>javaxgroupId>  
  237.       <artifactId>javaee-apiartifactId>  
  238.       <version>7.0version>  
  239.       <scope>providedscope>  
  240.     dependency>  
  241.     <dependency>  
  242.       <groupId>org.glassfish.webgroupId>  
  243.       <artifactId>javax.servlet.jsp.jstlartifactId>  
  244.       <version>1.2.2version>  
  245.     dependency>  
  246.   dependencies>  
  247.   <build>  
  248.     <plugins>  
  249.       <plugin>  
  250.         <artifactId>maven-compiler-pluginartifactId>  
  251.         <version>2.3.2version>  
  252.         <configuration>  
  253.           <source>1.6source>  
  254.           <target>1.6target>  
  255.         configuration>  
  256.       plugin>  
  257.       <plugin>  
  258.         <artifactId>maven-war-pluginartifactId>  
  259.         <version>2.6version>  
  260.         <configuration>  
  261.           <failOnMissingWebXml>falsefailOnMissingWebXml>  
  262.         configuration>  
  263.       plugin>  
  264.     plugins>  
  265.   build>  
  266. project>span>  


springmvc配置

在src/main/resources下面创建个spring目录用来存放applicationContext.xml
后期会用的的配置很多,所以我一般会根据不同用途创建不同别名的spring配置文件,然后用applicationContext.xml统一起来。
这里我创建了 applicationContext.xml以及applicationContext-mvc.xml
applicationContext.xml配置:

[html] view plain copy
  1. <span style="font-size:18px;">xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  6.      <import resource="classpath:spring/applicationContext-mvc.xml" />  
  7. beans>span>  
applicationContext-mvc.xml
[html] view plain copy
  1. <span style="font-size:18px;">xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8.        http://www.springframework.org/schema/context   
  9.        http://www.springframework.org/schema/context/spring-context-3.0.xsd   
  10.        http://www.springframework.org/schema/mvc   
  11.        http://www.springframework.org/schema/mvc/spring-mvc.xsd"  
  12.        default-lazy-init="true">  
  13.           
  14.     <context:component-scan base-package="com.xiaoxin.demo"/>  
  15.        
  16.     <mvc:resources location="/WEB-INF/pages/" mapping="/pages/**"/>  
  17.       
  18.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  19.         <property name="prefix" value="/WEB-INF/pages/"/>  
  20.         <property name="suffix" value=".jsp"/>  
  21.     bean>  
  22.       
  23.     <mvc:annotation-driven>  
  24.         <mvc:message-converters register-defaults="true">  
  25.             <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">  
  26.                 <property name="supportedMediaTypes">  
  27.                     <list>  
  28.                         <value>text/plain;charset=utf-8value>  
  29.                         <value>application/json;charset=utf-8value>  
  30.                         <value>application/x-www-form-urlencodedvalue>  
  31.                     list>  
  32.                 property>  
  33.             bean>  
  34.         mvc:message-converters>  
  35.     mvc:annotation-driven>      
  36. beans>span>  
置springMvc需要用到的DispatcherServlet,指向的地址是classpath:spring/applicationContext.xml所以要在web.xml中配置服务
[html] view plain copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <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_3_0.xsd" id="WebApp_ID" version="3.0">  
  3.   <display-name>springmvctestdisplay-name>  
  4.   <welcome-file-list>  
  5.     <welcome-file>index.htmlwelcome-file>  
  6.     <welcome-file>index.htmwelcome-file>  
  7.     <welcome-file>index.jspwelcome-file>  
  8.     <welcome-file>default.htmlwelcome-file>  
  9.     <welcome-file>default.htmwelcome-file>  
  10.     <welcome-file>default.jspwelcome-file>  
  11.   welcome-file-list>  
  12.   <servlet>  
  13.         <servlet-name>springmvctestservlet-name>  
  14.         <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>  
  15.         <init-param>  
  16.             <param-name>contextConfigLocationparam-name>  
  17.             <param-value>classpath:spring/applicationContext.xmlparam-value>  
  18.         init-param>  
  19.         <load-on-startup>1load-on-startup>  
  20.     servlet>  
  21.     <servlet-mapping>  
  22.         <servlet-name>springmvctestservlet-name>  
  23.         <url-pattern>/url-pattern>  
  24.     servlet-mapping>  
  25.     <filter>  
  26.         <filter-name>encodingFilterfilter-name>  
  27.         <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>  
  28.         <init-param>  
  29.             <param-name>encodingparam-name>  
  30.             <param-value>UTF-8param-value>  
  31.         init-param>  
  32.         <init-param>  
  33.             <param-name>forceEncodingparam-name>  
  34.             <param-value>trueparam-value>  
  35.         init-param>  
  36.     filter>  
  37.     <filter-mapping>  
  38.         <filter-name>encodingFilterfilter-name>  
  39.         <url-pattern>/*url-pattern>  
  40.     filter-mapping>  
  41.     <session-config>  
  42.         <session-timeout>60session-timeout>  
  43.     session-config>  
  44. web-app>  

小例子

包结构

maven 搭建springmvc web项目_第8张图片

controller的写法

[html] view plain copy
  1. <span style="font-size:18px;">package com.xiaoxin.demo.common;  
  2.   
  3. import org.springframework.stereotype.Controller;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.bind.annotation.RequestMethod;  
  6.   
  7. @Controller  
  8. @RequestMapping(value="/sendSpringmvc")  
  9. public class SendToSpringmvcController {  
  10.      @RequestMapping(value="/view",method = RequestMethod.GET)  
  11.         public String index(){  
  12.             System.out.println("进来了");  
  13.             return "myFirstSpringmvc";  
  14.         }  
  15.   
  16. }  
  17. span>  

访问http://localhost:8080/springmvctest/sendSpringmvc/view

如果控制台出现

maven 搭建springmvc web项目_第9张图片

访问出现

maven 搭建springmvc web项目_第10张图片

表示springmvc搭建成功


你可能感兴趣的:(maven 搭建springmvc web项目)