我是靠谱客的博主 无私鞋垫,最近开发中收集的这篇文章主要介绍maven 搭建springmvc web项目,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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


开发环境:myeclipse2016 

Maven部分

1.配置maven的JDK


2.配置本地Maven


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



WEB项目的创建

New----web project


一直next记得勾选这一个



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



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.0</modelVersion>  
  3.   <groupId>springmvctest</groupId>  
  4.   <artifactId>springmvctest</artifactId>  
  5.   <version>0.0.1-SNAPSHOT</version>  
  6.   <packaging>war</packaging>  
  7.   <name>springmvctest</name>  
  8.   <description/>  
  9.   <properties>  
  10.     <webVersion>3.0</webVersion>  
  11.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  12.     <spring.version>4.1.1.RELEASE</spring.version>  
  13.           <cxf.version>2.7.15</cxf.version>  
  14.         <slf4j-version>1.7.12</slf4j-version>  
  15.         <log4j-version>1.2.17</log4j-version>  
  16.         <mybatis-version>3.3.0</mybatis-version>  
  17.         <mybatis-spring-version>1.2.3</mybatis-spring-version>  
  18.   </properties>  
  19.   <dependencies>  
  20.     <dependency>  
  21.       <groupId>org.glassfish</groupId>  
  22.       <artifactId>bean-validator</artifactId>  
  23.       <version>3.0-JBoss-4.0.2</version>  
  24.       <scope>provided</scope>  
  25.     </dependency>  
  26.     <dependency>  
  27.       <groupId>org.glassfish</groupId>  
  28.       <artifactId>javax.enterprise.deploy</artifactId>  
  29.       <version>3.0.1</version>  
  30.       <scope>provided</scope>  
  31.     </dependency>  
  32.     <dependency>  
  33.       <groupId>org.glassfish</groupId>  
  34.       <artifactId>javax.jms</artifactId>  
  35.       <version>3.0.1</version>  
  36.       <scope>provided</scope>  
  37.     </dependency>  
  38.     <dependency>  
  39.       <groupId>org.glassfish</groupId>  
  40.       <artifactId>javax.management.j2ee</artifactId>  
  41.       <version>3.0.1</version>  
  42.       <scope>provided</scope>  
  43.     </dependency>  
  44.     <dependency>  
  45.       <groupId>org.eclipse.persistence</groupId>  
  46.       <artifactId>javax.persistence</artifactId>  
  47.       <version>2.0.0</version>  
  48.       <scope>provided</scope>  
  49.     </dependency>  
  50.     <dependency>  
  51.       <groupId>org.glassfish</groupId>  
  52.       <artifactId>javax.resource</artifactId>  
  53.       <version>3.0.1</version>  
  54.       <scope>provided</scope>  
  55.     </dependency>  
  56.     <dependency>  
  57.       <groupId>org.glassfish</groupId>  
  58.       <artifactId>javax.security.auth.message</artifactId>  
  59.       <version>3.0.1</version>  
  60.       <scope>provided</scope>  
  61.     </dependency>  
  62.     <dependency>  
  63.       <groupId>org.glassfish</groupId>  
  64.       <artifactId>javax.security.jacc</artifactId>  
  65.       <version>3.0.1</version>  
  66.       <scope>provided</scope>  
  67.     </dependency>  
  68.     <dependency>  
  69.       <groupId>org.glassfish</groupId>  
  70.       <artifactId>javax.servlet</artifactId>  
  71.       <version>3.0.1</version>  
  72.       <scope>provided</scope>  
  73.     </dependency>  
  74.     <dependency>  
  75.       <groupId>org.glassfish</groupId>  
  76.       <artifactId>javax.servlet.jsp</artifactId>  
  77.       <version>3.0.1</version>  
  78.       <scope>provided</scope>  
  79.     </dependency>  
  80.     <dependency>  
  81.       <groupId>org.glassfish</groupId>  
  82.       <artifactId>javax.servlet.jsp.jstl</artifactId>  
  83.       <version>3.0.1</version>  
  84.       <scope>provided</scope>  
  85.     </dependency>  
  86.     <dependency>  
  87.       <groupId>javax.xml.bind</groupId>  
  88.       <artifactId>jaxb-api-osgi</artifactId>  
  89.       <version>2.2.1</version>  
  90.       <scope>provided</scope>  
  91.     </dependency>  
  92.     <dependency>  
  93.       <groupId>javax.ws.rs</groupId>  
  94.       <artifactId>jsr311-api</artifactId>  
  95.       <version>1.1.1</version>  
  96.       <scope>provided</scope>  
  97.     </dependency>  
  98.     <dependency>  
  99.       <groupId>org.glassfish.web</groupId>  
  100.       <artifactId>jstl-impl</artifactId>  
  101.       <version>1.2</version>  
  102.       <scope>provided</scope>  
  103.     </dependency>  
  104.     <dependency>  
  105.       <groupId>javax.mail</groupId>  
  106.       <artifactId>mail</artifactId>  
  107.       <version>1.4.3</version>  
  108.       <scope>provided</scope>  
  109.     </dependency>  
  110.     <dependency>  
  111.       <groupId>javax.xml</groupId>  
  112.       <artifactId>webservices-api-osgi</artifactId>  
  113.       <version>2.0.1</version>  
  114.       <scope>provided</scope>  
  115.     </dependency>  
  116.     <dependency>  
  117.       <groupId>org.jboss.weld</groupId>  
  118.       <artifactId>weld-osgi-bundle</artifactId>  
  119.       <version>1.0.1-SP3</version>  
  120.       <scope>provided</scope>  
  121.     </dependency>  
  122.     <dependency>  
  123.       <groupId>org.glassfish.web</groupId>  
  124.       <artifactId>javax.servlet.jsp.jstl</artifactId>  
  125.       <version>1.2.1</version>  
  126.     </dependency>  
  127.      <!-- 日志配置-->  
  128.     <dependency>  
  129.         <groupId>log4j</groupId>  
  130.         <artifactId>log4j</artifactId>  
  131.         <version>${log4j-version}</version>  
  132.     </dependency>  
  133.     <dependency>  
  134.         <groupId>org.slf4j</groupId>  
  135.         <artifactId>slf4j-log4j12</artifactId>  
  136.         <version>${slf4j-version}</version>  
  137.     </dependency>  
  138.       <dependency>  
  139.         <groupId>org.slf4j</groupId>  
  140.         <artifactId>slf4j-api</artifactId>  
  141.         <version>${slf4j-version}</version>  
  142.     </dependency>  
  143.     <dependency>  
  144.         <groupId>javax.servlet</groupId>  
  145.         <artifactId>javax.servlet-api</artifactId>  
  146.         <version>3.1.0</version>  
  147.     </dependency>  
  148.     <dependency>  
  149.        <groupId>org.apache.commons</groupId>  
  150.        <artifactId>commons-lang3</artifactId>  
  151.        <version>3.3</version>  
  152.     </dependency>  
  153.     <dependency>  
  154.        <groupId>com.alibaba</groupId>  
  155.        <artifactId>fastjson</artifactId>  
  156.        <version>1.2.1</version>  
  157.     </dependency>  
  158.     <!-- 数据源配置 -->  
  159.     <dependency>  
  160.         <groupId>com.mchange</groupId>  
  161.         <artifactId>c3p0</artifactId>  
  162.         <version>0.9.5.1</version>  
  163.     </dependency>  
  164.     <!--数据库相关, mysql, mybatis-->  
  165.     <dependency>  
  166.         <groupId>mysql</groupId>  
  167.         <artifactId>mysql-connector-java</artifactId>  
  168.         <version>5.1.37</version>  
  169.     </dependency>  
  170.     <dependency>  
  171.         <groupId>org.mybatis</groupId>  
  172.         <artifactId>mybatis-spring</artifactId>  
  173.         <version>${mybatis-spring-version}</version>  
  174.     </dependency>  
  175.     <dependency>  
  176.         <groupId>org.mybatis</groupId>  
  177.         <artifactId>mybatis</artifactId>  
  178.         <version>${mybatis-version}</version>  
  179.     </dependency>  
  180.     <dependency>  
  181.         <groupId>org.mybatis.generator</groupId>  
  182.         <artifactId>mybatis-generator-core</artifactId>  
  183.         <version>1.3.2</version>  
  184.     </dependency>  
  185.     <!-- spring用到的包 -->  
  186.     <dependency>  
  187.         <groupId>org.springframework</groupId>  
  188.         <artifactId>spring-web</artifactId>  
  189.         <version>${spring.version}</version>  
  190.     </dependency>  
  191.     <dependency>  
  192.         <groupId>org.springframework</groupId>  
  193.         <artifactId>spring-core</artifactId>  
  194.         <version>${spring.version}</version>  
  195.     </dependency>  
  196.     <dependency>  
  197.         <groupId>org.springframework</groupId>  
  198.         <artifactId>spring-context</artifactId>  
  199.         <version>4.1.1.RELEASE</version>  
  200.     </dependency>  
  201.     <dependency>  
  202.         <groupId>org.springframework</groupId>  
  203.         <artifactId>spring-beans</artifactId>  
  204.         <version>${spring.version}</version>  
  205.     </dependency>  
  206.     <dependency>  
  207.         <groupId>org.springframework</groupId>  
  208.         <artifactId>spring-jdbc</artifactId>  
  209.         <version>${spring.version}</version>  
  210.     </dependency>  
  211.     <dependency>  
  212.         <groupId>org.springframework</groupId>  
  213.         <artifactId>spring-webmvc</artifactId>  
  214.         <version>${spring.version}</version>  
  215.     </dependency>  
  216.     <dependency>  
  217.         <groupId>org.springframework</groupId>  
  218.         <artifactId>spring-tx</artifactId>  
  219.         <version>${spring.version}</version>  
  220.     </dependency>  
  221.     <!--测试-->  
  222.     <dependency>  
  223.         <groupId>junit</groupId>  
  224.         <artifactId>junit</artifactId>  
  225.         <version>4.11</version>  
  226.         <scope>test</scope>  
  227.     </dependency>  
  228.     <dependency>  
  229.         <groupId>org.springframework</groupId>  
  230.         <artifactId>spring-test</artifactId>  
  231.         <version>4.1.1.RELEASE</version>  
  232.         <scope>test</scope>  
  233.     </dependency>  
  234.   
  235.     <dependency>  
  236.       <groupId>javax</groupId>  
  237.       <artifactId>javaee-api</artifactId>  
  238.       <version>7.0</version>  
  239.       <scope>provided</scope>  
  240.     </dependency>  
  241.     <dependency>  
  242.       <groupId>org.glassfish.web</groupId>  
  243.       <artifactId>javax.servlet.jsp.jstl</artifactId>  
  244.       <version>1.2.2</version>  
  245.     </dependency>  
  246.   </dependencies>  
  247.   <build>  
  248.     <plugins>  
  249.       <plugin>  
  250.         <artifactId>maven-compiler-plugin</artifactId>  
  251.         <version>2.3.2</version>  
  252.         <configuration>  
  253.           <source>1.6</source>  
  254.           <target>1.6</target>  
  255.         </configuration>  
  256.       </plugin>  
  257.       <plugin>  
  258.         <artifactId>maven-war-plugin</artifactId>  
  259.         <version>2.6</version>  
  260.         <configuration>  
  261.           <failOnMissingWebXml>false</failOnMissingWebXml>  
  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-8</value>  
  29.                         <value>application/json;charset=utf-8</value>  
  30.                         <value>application/x-www-form-urlencoded</value>  
  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>springmvctest</display-name>  
  4.   <welcome-file-list>  
  5.     <welcome-file>index.html</welcome-file>  
  6.     <welcome-file>index.htm</welcome-file>  
  7.     <welcome-file>index.jsp</welcome-file>  
  8.     <welcome-file>default.html</welcome-file>  
  9.     <welcome-file>default.htm</welcome-file>  
  10.     <welcome-file>default.jsp</welcome-file>  
  11.   </welcome-file-list>  
  12.   <servlet>  
  13.         <servlet-name>springmvctest</servlet-name>  
  14.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  15.         <init-param>  
  16.             <param-name>contextConfigLocation</param-name>  
  17.             <param-value>classpath:spring/applicationContext.xml</param-value>  
  18.         </init-param>  
  19.         <load-on-startup>1</load-on-startup>  
  20.     </servlet>  
  21.     <servlet-mapping>  
  22.         <servlet-name>springmvctest</servlet-name>  
  23.         <url-pattern>/</url-pattern>  
  24.     </servlet-mapping>  
  25.     <filter>  
  26.         <filter-name>encodingFilter</filter-name>  
  27.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  28.         <init-param>  
  29.             <param-name>encoding</param-name>  
  30.             <param-value>UTF-8</param-value>  
  31.         </init-param>  
  32.         <init-param>  
  33.             <param-name>forceEncoding</param-name>  
  34.             <param-value>true</param-value>  
  35.         </init-param>  
  36.     </filter>  
  37.     <filter-mapping>  
  38.         <filter-name>encodingFilter</filter-name>  
  39.         <url-pattern>/*</url-pattern>  
  40.     </filter-mapping>  
  41.     <session-config>  
  42.         <session-timeout>60</session-timeout>  
  43.     </session-config>  
  44. </web-app>  

小例子

包结构

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

如果控制台出现


访问出现


表示springmvc搭建成功


最后

以上就是无私鞋垫为你收集整理的maven 搭建springmvc web项目的全部内容,希望文章能够帮你解决maven 搭建springmvc web项目所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部