转自:http://blog.csdn.net/wananlaopo/article/details/41889705
一.背景介绍
对于初学者,用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。代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>exam</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>exam</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
配置ContextLoaderListener表示,该工程要以spring的方式启动。启动时会默认在/WEB-INF目录下查找applicationContext.xml作为spring容器的配置文件,这里可以初始化一些bean,如DataSource和iBATIS的集成。代码如下:
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName">
<!-- DataSource定义 -->
<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<propertyname="driverClassName"value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<propertyname="url"
value="jdbc:sqlserver://172.16.40.5:1433;DatabaseName=dev_his"/>
<propertyname="username"value="sa"/>
<propertyname="password"value="bsoft"/>
<propertyname="maxActive"value="50"/>
<propertyname="maxIdle"value="5"/>
<propertyname="maxWait"value="5000"/>
<propertyname="removeAbandoned"value="true"/>
<propertyname="removeAbandonedTimeout"value="5"/>
</bean>
<!-- TransactionManager定义 -->
<beanid="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource"ref="dataSource"/>
</bean>
<beanid="transactionTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
<propertyname="transactionManager"ref="transactionManager"/>
</bean>
<!-- iBatis SQL map定义 -->
<beanid="sqlMapClient"class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<propertyname="dataSource"ref="dataSource"/>
<propertyname="configLocation"value="classpath:conf/sql-map-config.xml"/>
</bean>
</beans>
配置DispatcherServlet表示,该工程将采用springmvc的方式。启动时也会默认在/WEB-INF目录下查找XXX-servlet.xml作为配置文件,XXX就是DispatcherServlet的名字,该文件中将配置两项重要的mvc特性:
HandlerMapping,负责为DispatcherServlet这个前端控制器的请求查找Controller;
ViewResolver,负责为DispatcherServlet查找ModelAndView的视图解析器。
代码如下:
exam-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Bean头部 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- 防止jsp等静态资源被拦截 -->
<mvc:default-servlet-handler/>
<!-- 激活@Controller模式 -->
<mvc:annotation-driven />
<!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 需要更改 -->
<context:component-scan base-package="cc.monggo.web.controller" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
5.让maven自动配置jar包
在用maven生成框架时,就生成了pop.xml,这就是maven的配置文件。我们要引入spring-web,servlet等特性的包。代码如下:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>exam</groupId>
<artifactId>exam_3</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>exam_3 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_2.5_spec</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>exam_3</finalName>
</build>
</project>
更多的jar包可以在maven中心库下载:http://mvnrepository.com。
run-jetty-run插件下载地址:http://run-jetty-run.googlecode.com/svn/trunk/updatesite/
jetty配置:
<build>
<finalName>WebProject_SMM</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopKey>foo</stopKey>
<stopPort>9999</stopPort>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
6.做个测试
说了一大堆,只有运行起来才有意思,下面写个简单的测试。先写Controller。编写两个类,LoginControler.java,LoginForm.java。代码如下:
LoginController.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 //第一种注解方式(不需要在上下文中配置bean) 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 }
相关推荐
maven+ibatis+spring mvc源码
Maven整合的spring-mvc3.x和iBatis3.x
springmvc+ibatis +maven多项目整合
最新Java通用后台管理系统 bootstrap,hibernate,spring mvc,spring,maven,mysql
弹簧该项目在Eclipse Maven中使用springmvc和ibatis 春天和肌纤维瘤spring.version 4.2.0。发布mybatis.version 3.2.8
* 包括jsp,servlet,html,javascript,css等,以及springmvc+jdbcTemplate和 struts2+hibernate+spring, struts2+ibatis+spring 实现的登陆,CRUD,jfreechart图表显示等例子,用来演示两个mvc框架的基本使用,...
2、开发环境搭建以及创建Maven Web项目 参看之前的博文:http://www.cnblogs.com/zyw-205520/p/4767633.html 3、SSM整合 下面主要介绍三大框架的整合,至于环境的搭建以及项目的创建,参看上面的博文。...
MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。MyBatis是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data ...
SpringMVC Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring MVC 分离了控制器、模型对象、分派器以及...MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache softwar
1.webx是阿里巴巴开发的mvc框架(就是spring mvc又封了一层) 2.此项目是个完整的项目,修改数据库配置后可直接运行(搜索data-source.xml) 3.webx自带的持久层是ibatis,我改成了hibernate 3.此为maven项目,jar包在项目...
A.2 把Spring添加为一个Maven 2依赖项 A.3 Spring与Ant A.4 Spring与Log4j 附录B 用(和不用)Spring进行测试 B.1 测试简介 B.1.1 理解不同类型的测试 B.1.2 使用JUnit B.1.3 Spring在测试中的角色 B.2 单元...
A.2 把Spring添加为一个Maven 2依赖项 A.3 Spring与Ant A.4 Spring与Log4j 附录B 用(和不用)Spring进行测试 B.1 测试简介 B.1.1 理解不同类型的测试 B.1.2 使用JUnit B.1.3 Spring在测试中的角色 B.2 单元...
jar包 和 eclipse插件(内含spring mvc,upload,jquery,ibatis,urlrewritefilter,mysql驱动包和svn,maven,easy explore,tomcat插件)
Appfuse是Matt Raible 开发的一个指导性的入门级J2EE框架,它对如何集成流行的Spring、Hibernate、iBatis、Struts、xDcolet、Junit、Taperstry、JSF等基础框架给出了示范。在持久层,AppFuse采用了Hibernate O/R映射...
Appfuse是Matt Raible 开发的一个指导性的入门级J2EE框架,它对如何集成流行的Spring、Hibernate、iBatis、Struts、xDcolet、Junit、Taperstry、JSF等基础框架给出了示范。在持久层,AppFuse采用了Hibernate O/R映射...
Spring MVC Spring WebFlow spring tx aop ioc Struts ibatis Mybatis CAS Dubbo 工作能力 软实力 应急能力 创新能力 管理能力 分享能力 学习能力 沟通能力 解决问题能力 经历 技术攻关案例 ...