Maven+Eclipse+Spring MVC简单实例

2021年11月23日 阅读数:3
这篇文章主要向大家介绍Maven+Eclipse+Spring MVC简单实例,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

1. ToolsVersion and Preparations:java

Eclipse: 3.5 (eclipse-jee-galileo-win32)web

Maven: 2.0.11spring

Spring MVC : 3.0.5 Releaseexpress

JDK:1.6apache

Eclipse Maven的插件spring-mvc

2.Stepby step:tomcat

2.1 在Eclipse 里创建Maven项目

选择File-> New -> Other,在New窗口中选择 Maven -> Maven Projectmvc

Click ‘Next’ button:app

 

Click ‘Next’ button:框架

在‘Filter’后面的文本框输入:maven-archetype-webapp

 

Click and select "org.apache.maven.archetypes" row, then click ‘Next’ button:

 

Click ‘Finish’ button , thenthe new Maven project will be successfully created.

创建好后的文件结构以下图:

 

2.2 建立Source目录结构,达到标准Maven项目的结构

 

分别添加以下三个Source Folder:

src/main/java , src/test/java , src/test/resources

2.3向Maven项目里添加所依赖的Jar包

 

Copy below pom.xml contents to your project pom.xml.



<projectxmlns="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.0http://maven.apache.org/maven-v4_0_0.xsd">

 <modelVersion>4.0.0</modelVersion>

 <groupId>springmvc.maven.example</groupId>

 <artifactId>springmvc</artifactId>

  <packaging>war</packaging>

  <version>0.0.1-SNAPSHOT</version>

  <name>springmvc MavenWebapp</name>

 <url>http://maven.apache.org</url>


  <properties>

              <context.path>springmvc</context.path>

              <compiler.version>1.5</compiler.version>

           <tomcat.port>8083</tomcat.port>

  </properties> 

  <dependencies>

                  <dependency>

                     <groupId>junit</groupId>

                     <artifactId>junit</artifactId>

                     <version>4.8.1</version>

                     <scope>test</scope>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-aop</artifactId>

                     <version>3.0.5.RELEASE</version>

                     <type>jar</type>

                     <scope>compile</scope>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-asm</artifactId>

                     <version>3.0.5.RELEASE</version>

                     <type>jar</type>

                     <scope>compile</scope>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-aspects</artifactId>

                     <version>3.0.5.RELEASE</version>

                     <type>jar</type>

                     <scope>compile</scope>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-beans</artifactId>

                     <version>3.0.5.RELEASE</version>

                     <type>jar</type>

                     <scope>compile</scope>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-context</artifactId>

                     <version>3.0.5.RELEASE</version>

                     <type>jar</type>

                     <scope>compile</scope>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-context-support</artifactId>

                     <version>3.0.5.RELEASE</version>

                     <type>jar</type>

                     <scope>compile</scope>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-core</artifactId>

                     <version>3.0.5.RELEASE</version>

                     <type>jar</type>

                     <scope>compile</scope>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-expression</artifactId>

                     <version>3.0.5.RELEASE</version>

                     <type>jar</type>

                     <scope>compile</scope>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-jdbc</artifactId>

                     <version>3.0.5.RELEASE</version>

                     <type>jar</type>

                     <scope>compile</scope>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-jms</artifactId>

                     <version>3.0.5.RELEASE</version>

                     <type>jar</type>

                     <scope>compile</scope>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-orm</artifactId>

                     <version>3.0.5.RELEASE</version>

                     <type>jar</type>

                     <scope>compile</scope>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-oxm</artifactId>

                     <version>3.0.5.RELEASE</version>

                     <type>jar</type>

                     <scope>compile</scope>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-tx</artifactId>

                     <version>3.0.5.RELEASE</version>

                     <type>jar</type>

                     <scope>compile</scope>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-web</artifactId>

                     <version>3.0.5.RELEASE</version>

                     <type>jar</type>

                     <scope>compile</scope>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-webmvc</artifactId>

                     <version>3.0.5.RELEASE</version>

                     <type>jar</type>

                     <scope>compile</scope>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-test</artifactId>

                     <version>3.0.5.RELEASE</version>

                     <type>jar</type>

                     <scope>compile</scope>

              </dependency>

             

              <dependency>

                     <groupId>javax.servlet</groupId>

                     <artifactId>jstl</artifactId>

                     <version>1.2</version>

                     <type>jar</type>

                     <scope>compile</scope>

              </dependency>

  

  </dependencies>

  <build>

   <defaultGoal>tomcat:run</defaultGoal>

              <resources>

                     <resource>

                            <directory>${basedir}/src/main/java</directory>

                            <includes>

                                   <include>**/*.xml</include>

                            </includes>

                     </resource>

                     <resource>

                            <directory>${basedir}/src/main/resources</directory>

                            <includes>

                                   <include>**/*.xml</include>

                                   <include>**/*.properties</include>

                            </includes>

                     </resource>

              </resources>

             

              <plugins>

              <plugin>

                            <groupId>org.apache.maven.plugins</groupId>

                            <artifactId>maven-compiler-plugin</artifactId>

                            <configuration>

                                   <source>${compiler.version}</source>

                                   <target>${compiler.version}</target>                                 

                            </configuration>

                     </plugin>

                     <plugin>

                            <groupId>org.codehaus.mojo</groupId>

                            <artifactId>tomcat-maven-plugin</artifactId>

                            <configuration>

                                   <port>${tomcat.port}</port>

                            </configuration>

                     </plugin>

              </plugins>

 

              <finalName>${context.path}</finalName>

  </build>

</project>

 

2.4构建SpringMVC框架

2.4.1 编辑web.xml文件

webx.xml代码以下:



<?xmlversion="1.0" encoding="UTF-8"?> 

    <web-appxmlns="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>webAppRootKey</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>dispatcher</servlet-name> 

           <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 

           <init-param> 

               <param-name>contextConfigLocation</param-name> 

               <param-value>classpath*:dispatcher-servlet.xml</param-value> 

           </init-param> 

           <load-on-startup>1</load-on-startup> 

       </servlet> 

       <servlet-mapping> 

           <servlet-name>dispatcher</servlet-name> 

           <url-pattern>*.do</url-pattern> 

       </servlet-mapping>    

</web-app

2.4.2编写Spring配置文件dispatcher-servlet.xml

(src/main/resources/dispatcher-servlet.xml)

 

如要添加MVC驱动、注解检测、视图解析等。dispatcher-servlet.xml代码以下:

 



<?xml version="1.0"encoding="UTF-8"?> 

<beansxmlns="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-scanbase-package="springmvc.maven.example" /> 

    <beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"> 

        <propertyname="prefix" value="/WEB-INF/jsp/" /> 

        <propertyname="suffix" value=".jsp" /> 

    </bean> 

</beans>

 

 

2.4.3编写一个Controller层测试类

 

2.4.4编写JSP文件

 

根据dispatcher-servlet.xml和Controller层的代码,咱们须要在WEB-INF下创建一个jsp文件夹,在其下创建一个mySpringMVC.jsp文件,内容以下:


3. Compile, Deploy and Run (All only need one maven command):

Commandmvn clean install –DskipTests=truewar:exploded tomcat:run

 

Open your browser,then type http://localhost:8083/springmvc/mySpringMVC.do in url, then pressEnter key:

自csdn