一.背景介绍

  对于初学者,用maven构建项目并不是一件容易的事,springmvc并不是MVC中的主流,但是本人觉得springmvc比struts要好用,真正做到了零配置。一次使用,就放不下了。

二.准备工作

  1.Eclipse 3.7

  2.maven

  3.Eclipse 需要安装maven插件。url:maven - http://download.eclipse.org/technology/m2e/releases 。安装maven-3.0.4。并且选择本地的maven,如下图:

  

 

三.构建工程

  1.用maven插件构建项目框架

  maven具有强大构建功能,使用maven可以构建多种不同类型的工程。这里我们构建maven-archetype-webapp类型的项目。在Eclipse->New中选择other,找到maven Project型。如下图:

  

在选完路径之后,我们选择构建类型,如下图:

  

接下来,填写工程的Group Id,Artifact Id,如下图:

  

这里的Group Id就是大项目的id,Arifact Id就是该项目的Id。就像一个大项目中有许多小项目组成一样。此时,我们的项目已经成型了,样子如下图:

  

接下来,我们要完善项目的目录,配置。

 

  2.完善项目

  首先,完善目录,增加重要的source Folder,这个不是简单的Floder,这些文件夹是会参与编译的。增加src/main/java,src/test/resources,src/test/java目录。让目录变成标准的maven结构。如下图:

  

接下来,改变一些配置:

让工程的JDK用本地的jdk;

让工程的字符集为UTF-8;

改变工程的目录顺序;

  这些都完成之后,工程目录应该是如下的样子:

  

  

  3.将工程变成web工程

  此时,我们的工程还不是标准的web工程,可以在eclipse中增加web工程的特性,选择工程的Properties,选Project Facets,如下图:

  

这里,我们选择Dynamic Web Module,版本选择2.4,这个版本比较通用。如下图:

  

此时,我们看到目录中多了一个WebContent目录,由于使用maven构建,web目录是src/main/webapp,所以我们删除WebContent目录。接下来,要配置web项目的发布目录,就是Deployment Assembly,如图:

  

test目录不要发布,WebContent目录没有了,所以这三项都删掉。并且增加src/main/webapp目录,和Maven Dependenices,完成之后如下图:

  

于是,我们的工程就完全是一个web工程了。

 

  4.赋予工程的springmvc特性

  配置web.xml,使其具有springmvc特性,主要配置两处,一个是ContextLoaderListener,一个是DispatcherServlet。代码如下:  

复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
 5     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 6 
 7     <listener>
 8         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 9     </listener>
10 
11     <servlet>
12         <servlet-name>exam</servlet-name>
13         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
14     </servlet>
15 
16     <servlet-mapping>
17         <servlet-name>exam</servlet-name>
18         <url-pattern>/</url-pattern>
19     </servlet-mapping>
20 
21     <welcome-file-list>
22         <welcome-file>index.jsp</welcome-file>
23     </welcome-file-list>
24 </web-app>
复制代码

  配置ContextLoaderListener表示,该工程要以spring的方式启动。启动时会默认在/WEB-INF目录下查找applicationContext.xml作为spring容器的配置文件,这里可以初始化一些bean,如DataSource。我们这里什么也不做。代码如下:

1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
3 <beans>
4 </beans>

  配置DispatcherServlet表示,该工程将采用springmvc的方式。启动时也会默认在/WEB-INF目录下查找XXX-servlet.xml作为配置文件,XXX就是DispatcherServlet的名字,该文件中将配置两项重要的mvc特性:

HandlerMapping,负责为DispatcherServlet这个前端控制器的请求查找Controller;

ViewResolver,负责为DispatcherServlet查找ModelAndView的视图解析器。

代码如下:

复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!-- Bean头部 -->
 3 <beans xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:util="http://www.springframework.org/schema/util"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
 8             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
 9             http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd              
10             http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
11     
12     <!-- 激活@Controller模式 -->
13     <mvc:annotation-driven />
14     <!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 需要更改 -->
15     <context:component-scan base-package="cc.monggo.web.controller" />
16 
17     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
18 
19     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
20         <property name="prefix">
21             <value>/WEB-INF/jsp/</value>
22         </property>
23         <property name="suffix">
24             <value>.jsp</value>
25         </property>
26     </bean>
27 </beans>  
复制代码

 

  5.让maven自动配置jar包

  在用maven生成框架时,就生成了pop.xml,这就是maven的配置文件。我们要引入spring-web,servlet等特性的包。代码如下:

复制代码
 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>exam</groupId>
 5   <artifactId>exam_3</artifactId>
 6   <packaging>war</packaging>
 7   <version>0.0.1-SNAPSHOT</version>
 8   <name>exam_3 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     
18     <dependency>
19         <groupId>org.springframework</groupId>
20         <artifactId>spring-web</artifactId>
21         <version>3.0.5.RELEASE</version>
22     </dependency>
23     
24     <dependency>
25         <groupId>org.springframework</groupId>
26         <artifactId>spring-webmvc</artifactId>
27         <version>3.0.5.RELEASE</version>
28     </dependency>
29     
30     <dependency>
31         <groupId>org.apache.geronimo.specs</groupId>
32         <artifactId>geronimo-servlet_2.5_spec</artifactId>
33         <version>1.2</version>
34     </dependency>
35             
36   </dependencies>
37   <build>
38     <finalName>exam_3</finalName>
39   </build>
40 </project>
复制代码

  maven就是这么简单,一旦保存,maven就会自动下载pop.xml的jar包。此时可以看到目录中Maven Dependencies下生成了jar包。

  更多的jar包可以在maven中心库下载:http://mvnrepository.com

  

  6.做个测试

  说了一大堆,只有运行起来才有意思,下面写个简单的测试。先写Controller。编写两个类,LoginControler.java,LoginForm.java。代码如下:

复制代码
 1 package cc.monggo.web.controller;
 2 
 3 
 4 import javax.servlet.http.HttpServletRequest;
 5 import javax.servlet.http.HttpServletResponse;
 6 
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 import org.springframework.web.servlet.ModelAndView;
10 
11 import cc.monggo.domain.LoginForm;
12 
13 @Controller
14 public class LoginController {
15     @RequestMapping(value="login")
16     public ModelAndView login(HttpServletRequest request,HttpServletResponse response,LoginForm command ){
17         String username = command.getUsername();
18         ModelAndView mv = new ModelAndView("/index/index","command","LOGIN SUCCESS, " + username);
19         return mv;
20     }
21 }
复制代码
复制代码
 1 package cc.monggo.domain;
 2 
 3 
 4 public class LoginForm {
 5     private String username;
 6     private String password;
 7     public String getUsername() {
 8         return username;
 9     }
10     public void setUsername(String username) {
11         this.username = username;
12     }
13     public String getPassword() {
14         return password;
15     }
16     public void setPassword(String password) {
17         this.password = password;
18     }
19 }
复制代码

  再增加一些jsp,首页的index.jsp,主要是做跳转,代码如下:

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

   还有两个jsp,做些简单的功能,一个表单login.jsp,一个表单提交的返回index.jsp,代码如下:

复制代码
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <div>
11         <form action="login" methed="get">
12             <input type="text" name="username">
13             <input type="submit" value="SUBMIT">
14         </form>
15     </div>
16 </body>
17 </html>
复制代码
复制代码
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     ${command}
11 </body>
12 </html>
复制代码

  整个项目的目录结构如下:

  

  在tomcat中运行,即可。不要使用Tomcat插件,可能有问题。就用普通的方式。运行效果如下图:

  

GitHub 加速计划 / ma / maven
36
2
下载
Maven: Apache Maven是一个开源的项目管理和构建工具,主要用于Java项目。适合需要自动化项目构建和依赖管理的开发者。特点包括约定优于配置、易于使用和社区驱动。
最近提交(Master分支:1 个月前 )
23d38a2d They lacked in some cases the `.mvn` directory, and it caused Maven to "bubble up" all way to Maven Project own `.mvn`. Now the UT properly confine the test within CWD by creating `.mvn` in it. 1 天前
727d80f6 When a POM contains ${project.url} in the <url> field and also defines a property named 'project.url', Maven was incorrectly attempting to resolve the expression via model reflection first, which would return the same ${project.url} value, causing a recursive variable reference error. This fix changes the interpolation resolution order to check model properties before prefixed model reflection. This allows properties like 'project.url' to be resolved from the <properties> section before attempting to use reflection on the model object. The new resolution order is: 1. basedir 2. build.timestamp 3. user properties 4. model properties (moved up from position 5) 5. prefixed model reflection (moved down from position 3) 6. system properties 7. environment variables 8. unprefixed model reflection This change is consistent with the approach used for MNG-8469, which established that explicit property definitions should take precedence over model field reflection. Fixes #11384 1 天前
Logo

新一代开源开发者平台 GitCode,通过集成代码托管服务、代码仓库以及可信赖的开源组件库,让开发者可以在云端进行代码托管和开发。旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐