工作中项目开发使用Maven管理项目的构建、打包、编译,框架采用的是Spring MVC框架,并且实现了多模块、多项目的管理。自己也简单的参与了架构的设计。对于初学者来说,使用Maven构建项目并不是一件容易的事情,本文的目的就是引导新手使用maven构建springmvc项目。
准本工作
1、Eclipse
尽量选用较高版本的Eclispse,因为eclipse对于maven的支持比较晚。
2、Maven
安装maven,至于其安装方式这里也就不再多提了,请自行google。
3、Eclipse选择本地Maven,如下图所:
选择本地库的原因是你可以自行指定仓库的位置,也可以指定远程仓库,方便管理。
构建工程
1、新建Maven项目maven具有强大构建功能,使用maven可以构建多种不同类型的工程。这里我们构建maven-archhetype-webapp类型的项目。Eclipse->New中选择maven project,具体如下图:
之后选择构建类型:
接下来填写完Group id 和Artifact id 之后即可新建一个空的Maven项目了。
一个空的示例项目目录结构如下:
似乎和完整的Maven项目还略有差距,不急,一步一步完善。
2、完善项目
上述项目结构离完整的maven项目结构还有一定的距离,我们需要添加三个源文件夹src/main/java(核心源码),src/test/java(测试代码:单元测试),src/test/resources(测试代码的配置文件)。其中已经有的src/main/resources为项目的配置文件放置路径。
不过在通过Eclipse新建三个源文件时,会出现一个奇怪的问题,如下所示:
一个解决办法是我们直接定位到项目文件中(磁盘中),手动的新建该三个文件,之后刷新项目即可。
3、添加web特性
对于版本较高的Eclipse来说,到现在项目基本就是一个maven项目了,但是对于较老版本来说还要进行一些操作。而且基于后续项目打包、发布的考虑这里也需要做相关操作。
右键项目->Properties->Project Facets->动态web特性
由于项目打包、发布的时候不需要测试代码、测试的配置文件,以及运行时产生的额外文件(target),这里我们要进行下web目录的配置也就是Deployment Assembly,具体如下图:
删除蓝色框区域(老版本可能需删除webContent目录,添加webapp目录)。
至此,一个完整的maven项目就构建好了,项目的结构如下:
Spring MVC配置
目前为止只是构建了maven项目,接下来一步一步来配置Spring MVC特性。 1、Spring MVC配置
配置web.xml,采用Spring MVC框架,主要配置的是ContextLoaderListener和DispacherServlet。web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>LCore</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 加载所有的配置文件
这里我将配置文件置于源码包中
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-*.xml</param-value>
</context-param>
<!-- 配置Spring-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置SpringMVC -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置字符集 -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置Session -->
<filter>
<filter-name>openSession</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
配置ContextLoaderListener表示,该工程要以spring的方式启动。启动时会默认在/WEB-INF目录下查找 spring-common.xml作为spring容器的配置文件,这里可以初始化一些bean,如DataSource。代码如下:
<?xml version="1.0" encoding="UTF-8"?>至于jdbc.properties文件jdbc的配置这里就不给出了。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" 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.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<context:property-placeholder location="classpath:jdbc.properties" />
<context:component-scan base-package="com.ceis.core" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
p:driverClassName="${jdbc.driver}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.ceis.core.po</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven proxy-target-class="true" />
</beans>
配置DispatcherServlet表示,该工程将采用springmvc的方式。启动时也会默认在classPath目录下查找spring-mvc.xml作为配置文件,该文件中将配置两项重要的mvc特性:
HandlerMapping,负责为DispatcherServlet这个前端控制器的请求查找Controller;
ViewResolver,负责为DispatcherServlet查找ModelAndView的视图解析器这里我额外配置了velocity引擎处理请求和视图解析器(页面通过velocity模板引擎构建)。代码如下:
<?xml version="1.0" encoding="UTF-8"?>2、Maven配置jar包
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 注解扫描包 -->
<context:component-scan base-package="com.ceis.core.controller" />
<!-- 启动spring mvc的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<!-- 配置信息转换,将用@responsebody注解的返回值转换为json返回前台,编码为utf-8-->
<property name="messageConverters">
<list>
</list>
</property>
</bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*" />
<!-- 需排除拦截的地址 -->
<mvc:exclude-mapping path="/login" />
<bean class="com.ceis.core.Interceptor.SecurityInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
<mvc:annotation-driven />
<!-- 静态资源(js/image)的访问 -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- 定义视图解析器 -->
<!-- 配置velocity引擎处理请求 -->
<bean id="velocityConfigurer"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<!-- <property name="configLocation"> <value>/WEB-INF/toolbox.xml</value>
</property> -->
<property name="resourceLoaderPath">
<value>/WEB-INF/views/</value>
</property>
<property name="velocityProperties">
<props>
<prop key="input.encoding">UTF-8</prop>
<prop key="output.encoding">UTF-8</prop>
</props>
</property>
</bean>
<!-- 配置velocity视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="suffix">
<value>.vm</value>
</property>
<property name="contentType" value="text/html;charset=UTF-8"></property>
</bean>
</beans>
传统引入jar的方式是将其放入web-inf->lib目录里面,无形中增大了项目,而且jar不能统一进行管理。使用Maven的好处之一就是通过配置POM.XML文件自动下载jar包,并且通过中心库统一进行管理、版本的控制等。
这里我们需要引入spring-mvc、servlet特性相关的包,以及tomcat插件(运行)
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>debug</groupId>
<artifactId>debug</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.6.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>3.6.0.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>3.6.10.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>2.0.8</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.1_3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.4.GA</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.5.6-Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.8</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.8</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
</pluginRepository>
</pluginRepositories>
<build>
<sourceDirectory>C:\Users\LCore\git\debug\debug\src</sourceDirectory>
<scriptSourceDirectory>C:\Users\LCore\git\debug\debug\src\main\scripts</scriptSourceDirectory>
<testSourceDirectory>C:\Users\LCore\git\debug\debug\src\test\java</testSourceDirectory>
<outputDirectory>C:\Users\LCore\git\debug\debug\target\classes</outputDirectory>
<testOutputDirectory>C:\Users\LCore\git\debug\debug\target\test-classes</testOutputDirectory>
<resources>
<resource>
<directory>C:\Users\LCore\git\debug\debug\src\main\resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>C:\Users\LCore\git\debug\debug\src\test\resources</directory>
</testResource>
</testResources>
<directory>C:\Users\LCore\git\debug\debug\target</directory>
<finalName>debug-0.0.1-SNAPSHOT</finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.3.2</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<url>http://127.0.0.1:8080/manager</url>
<port>8080</port>
<server>TomcatServer</server>
<path>/debug</path>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</execution>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</execution>
</executions>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>default-clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default-install</id>
<phase>install</phase>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>default-resources</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
<execution>
<id>default-testResources</id>
<phase>process-test-resources</phase>
<goals>
<goal>testResources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>default-war</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
<executions>
<execution>
<id>default-site</id>
<phase>site</phase>
<goals>
<goal>site</goal>
</goals>
<configuration>
<outputDirectory>C:\Users\LCore\git\debug\debug\target\site</outputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
</reportPlugin>
</reportPlugins>
</configuration>
</execution>
<execution>
<id>default-deploy</id>
<phase>site-deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
<configuration>
<outputDirectory>C:\Users\LCore\git\debug\debug\target\site</outputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
</reportPlugin>
</reportPlugins>
</configuration>
</execution>
</executions>
<configuration>
<outputDirectory>C:\Users\LCore\git\debug\debug\target\site</outputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
</reportPlugin>
</reportPlugins>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<outputDirectory>C:\Users\LCore\git\debug\debug\target\site</outputDirectory>
</reporting>
</project
上述配置中有关于git的都可以删掉。
测试
经过一些努力终于配置好了springmvc,下面进行一个简单的登录测试。
LoginController:
package com.ceis.core.controller.login;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.ceis.core.controller.BaseController;
import com.ceis.core.po.User;
import com.ceis.core.service.UserService;
@Controller
public class LoginController extends BaseController{
@Resource(name="userService")
private UserService userService;
@RequestMapping("/login")
public ModelAndView login(User user,HttpServletRequest request,HttpServletResponse response)
{
ModelAndView view = null;
user.setPassword(request.getParameter("password"));
user.setName(request.getParameter("username"));
if(userService.login(user.getName(),user.getPassword())){
view = createLayoutView("content");
request.getSession().setAttribute("user", user);
return view;
}
else
return new ModelAndView("login");
}
/**
* 退出登录必须清空session
* @param user
* @param request
* @return the login.jsp
*/
@RequestMapping("/loginOut")
public String loginOut(User user,HttpServletRequest request)
{
HttpSession httpSession = request.getSession();
httpSession.removeAttribute("user");
httpSession.invalidate();//session不可用
return "redirect:login.jsp";//请求重定向到首页
}
}
至于service的代码这里就不给出了。
登录页面的核心代码:
<!-- Login Screen -->form表单的action的值就是LoginController的login方法的requestMapping路径。
<div class="login-wrapper">
<div class="login-container">
<img width="100" height="30" src="resources/images/logo-login-big.png" />
<form action="login" method="post">
<div class="form-group">
<input class="form-control" placeholder="用户名"
type="text" name="username">
</div>
<div class="form-group">
<input class="form-control" placeholder="密码" type="password" name="password"><input
type="submit" value="">
</div>
<div class="form-options clearfix">
<a class="pull-right" href="#">忘记密码?</a>
<div class="text-left">
<label class="checkbox"><input type="checkbox"><span>记住密码</span></label>
</div>
</div>
</form>
<div class="social-login clearfix">
<a class="btn btn-primary pull-left facebook" href=""><i
class="icon-facebook"></i>Facebook login</a><a
class="btn btn-primary pull-right twitter" href=""><i
class="icon-twitter"></i>Twitter login</a>
</div>
<p class="signup">
没有账户? <a href="signup">立即注册</a>
</p>
</div>
</div>
点击登录之后顺利跳转至主页: