因在Spring官网上都推荐使用Maven下载相关的Spring jar包,故复习了一下如何使用Maven构建Spring MVC,顺便学习一下Spring MVC。(m2eclipse的插件的安装,在此不作描述。)

一、新建Maven工程,如下图所示

1、右键新建项目


spring mvc环境搭建 spring mvc项目搭建_spring



2、可以在搜索中直接输入maven

spring mvc环境搭建 spring mvc项目搭建_spring mvc环境搭建_02

3、我这里选择的是使用默认的项目空间

spring mvc环境搭建 spring mvc项目搭建_spring mvc环境搭建_03

4、选择Maven webapp

spring mvc环境搭建 spring mvc项目搭建_xml_04

5、输入相应的项目名称

spring mvc环境搭建 spring mvc项目搭建_html_05

注:关于Group Id等字段的意义可参考该文章:http://www.iteye.com/topic/973166

6点击Finish

二、添加Dynamic Web Module

1、点击工程,使用组合键:alt + enter

spring mvc环境搭建 spring mvc项目搭建_html_06

2、选择Project Facets

spring mvc环境搭建 spring mvc项目搭建_spring_07

3、增加目录:src/test/resources

spring mvc环境搭建 spring mvc项目搭建_xml_08

4、复制WebContent/WEB-INF/下的web.xml到 /webmvct/src/main/webapp/WEB-INF目中

5、删除WebContent文件

6、点击工程使用组合键,打开工程选项,删除以下几项:

spring mvc环境搭建 spring mvc项目搭建_xml_09

7、增加目录:webapp 和 Maven Dependenices

spring mvc环境搭建 spring mvc项目搭建_html_10

spring mvc环境搭建 spring mvc项目搭建_xml_11

三、修改 web.xml文件


[html]  view plain  copy


 

1. <?xml version="1.0" encoding="UTF-8"?>  
2. <web-app id="WebApp_ID" version="2.4"  
3. xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
5. <display-name>webmvct</display-name>  
6. <context-param>  
7. <param-name>webAppRootKey</param-name>  
8. <param-value>webmvct.root</param-value>  
9. </context-param>  
10.   
11.       
12. <context-param>  
13. <param-name>log4jConfigLocation</param-name>  
14. <param-value>WEB-INF/log4j.properties</param-value>  
15. </context-param>  
16.       
17. <listener>  
18. <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
19. </listener>  
20.       
21. <listener>  
22. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
23. </listener>  
24.   
25. <servlet>  
26. <servlet-name>webmvct</servlet-name>  
27. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
28. <load-on-startup>1</load-on-startup>  
29. </servlet>  
30. <servlet-mapping>  
31. <servlet-name>webmvct</servlet-name>  
32. <url-pattern>/</url-pattern>  
33. </servlet-mapping>  
34. <welcome-file-list>  
35. <welcome-file>index.jsp</welcome-file>  
36. </welcome-file-list>  
37. </web-app>  
38.


四、在WEB-INF 目录下增加文件: 工程名-servlet.xml

(WEB-INF/webmvct-servlet.xml)


[html]  view plain  copy


 

1. <?xml version="1.0" encoding="UTF-8"?>  
2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
3. xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"  
4. xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"  
5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
6.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
7.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd  
8. >  
9.   
10. <!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 稍后会创建该包:webmvct.cmd -->      
11. <context:component-scan base-package="webmvct.cmd"/>  
12.   
13.       
14. <!-- 定义Spring MVC 的模板文件 -->  
15. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
16. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>     <!-- 支持JSTL -->  
17. <property name="prefix" value="/WEB-INF/jsp/" />              <!-- 模板的路径 -->  
18. <property name="suffix" value=".jsp" />                <!-- 模板文件的后缀-->  
19. </bean>  
20.       
21. <


五、修改pom.xml

可到该网址查询相应的spring包:http://repo.spring.io/webapp/browserepo.html?0


[html]  view plain  copy


 


    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
    3. <modelVersion>4.0.0</modelVersion>  
    4. <groupId>com.learn.cm</groupId>  
    5. <artifactId>webmvct</artifactId>  
    6. <packaging>war</packaging>  
    7. <version>0.0.1-SNAPSHOT</version>  
    8. <name>webmvct Maven Webapp</name>  
    9. <url>http://maven.apache.org</url>  
    10. <dependencies>  
    11. <dependency>  
    12. <groupId>junit</groupId>  
    13. <artifactId>junit</artifactId>  
    14. <version>3.8.1</version>  
    15. <scope>test</scope>  
    16. </dependency>  
    17. <dependency>  
    18. <groupId>org.springframework</groupId>  
    19. <artifactId>spring</artifactId>  
    20. <version>2.5.6</version>  
    21. </dependency>  
    22. <dependency>  
    23. <groupId>org.springframework</groupId>  
    24. <artifactId>spring-webmvc</artifactId>  
    25. <version>2.5.6</version>  
    26. </dependency>  
    27. <dependency>  
    28. <groupId>org.mortbay.jetty</groupId>  
    29. <artifactId>servlet-api-2.5</artifactId>  
    30. <version>6.0.1</version>  
    31. </dependency>  
    32. <dependency>  
    33. <groupId>javax.servlet</groupId>  
    34. <artifactId>jstl</artifactId>  
    35. <version>1.1.2</version>  
    36. </dependency>  
    37.   
    38. <dependency>  
    39. <groupId>log4j</groupId>  
    40. <artifactId>log4j</artifactId>  
    41. <version>1.2.14</version>  
    42. </dependency>  
    43. </dependencies>  
    44. <build>  
    45. <finalName>webmvct</finalName>  
    46. </build>  
    47. </project>



    六、在src/main/java 文件夹中新建类 WebMVCTController

    spring mvc环境搭建 spring mvc项目搭建_xml_12

    七、在WEB-INF目录下新建文件夹jsp ,并在jsp文件夹中新建login.jsp


    [html]  view plain  copy

     
        
     
    1. <!DOCTYPE html>  
    2. <html>  
    3. <head>  
    4. <title>webmvct简单例子</title>  
    5. <meta charset="utf-8">      
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    7. <!-- Bootstrap -->  
    8. <link rel="stylesheet" href="http://cdn.bootcss.com/twitter-bootstrap/3.0.3/css/bootstrap.min.css">  
    9. <link rel="stylesheet" href="http://v3.bootcss.com/examples/signin/signin.css"/>  
    10. <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->  
    11. <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->  
    12. >  
    13. <script src="http://cdn.bootcss.com/html5shiv/3.7.0/html5shiv.min.js"></script>  
    14. <script src="http://cdn.bootcss.com/respond.js/1.3.0/respond.min.js"></script>  
    15. >  
    16. <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->  
    17. <script src="http://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>  
    18. <!-- Include all compiled plugins (below), or include individual files as needed -->  
    19. <script src="http://cdn.bootcss.com/twitter-bootstrap/3.0.3/js/bootstrap.min.js"></script>   
    20. </head>  
    21. <body>  
    22. <div  class="container">  
    23. <form class="form-signin" role="form" action="login">  
    24. <h2 class="form-signin-heading">登陆</h2>  
    25. <input type="text" name="username" class="form-control" placeholder="请输入用户名" required autofocus>  
    26.     
    27. <button class="btn btn-lg btn-primary btn-block" type="submit">登陆</button>  
    28. </form>  
    29.      
    30. </body>  
    31. </html>


    八、同时新建文件:business.jsp


    [html]  view plain  copy


     
        
     
    1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    2. <html>  
    3. <head>  
    4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
    5. <title>欢迎</title>  
    6. </head>  
    7. <body>  
    8.     ${userName}  
    9. </body>  
    10. </html>


    九、在WEB-INF目录下创建log4j配置文件:

    log4j.properties


    [java]  view plain  copy


     

    1. # For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!  
    2. # For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.  
    3. log4j.rootLogger=DEBUG, stdout  
    4.   
    5. log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
    6. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
    7. log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n


    十、在WEB-INF文件夹中增加文件applicationContext.xml


    [html]  view plain  copy


     

    1. <?xml version="1.0" encoding="UTF-8"?>  
    2.   
    3. <beans xmlns="http://www.springframework.org/schema/beans"  
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    5. xmlns:aop="http://www.springframework.org/schema/aop"  
    6. xmlns:tx="http://www.springframework.org/schema/tx"  
    7. xmlns:context="http://www.springframework.org/schema/context"  
    8. xsi:schemaLocation="http://www.springframework.org/schema/beans   
    9.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
    10.            http://www.springframework.org/schema/aop   
    11.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
    12.            http://www.springframework.org/schema/tx   
    13.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  
    14.            http://www.springframework.org/schema/context   
    15.            http://www.springframework.org/schema/context/spring-context-2.5.xsd    
    16. >  
    17.   
    18. <!-- the parent application context definition for the springapp application -->  
    19.   
    20. <!--     <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>  
    21.  -->  
    22. </beans>


    十一、修改index.jsp文件


    [html]  view plain  copy


     

    1. <%  
    2.    request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);  
    3. >


    十二、发布,启动工程

    在浏览器中输入:http://localhost:8080/webmvct/

    spring mvc环境搭建 spring mvc项目搭建_spring mvc环境搭建_13

    spring mvc环境搭建 spring mvc项目搭建_xml_14