工作中项目开发使用Maven管理项目的构建、打包、编译,框架採用的是Spring MVC框架,而且实现了多模块、多项目的管理。自己也简单的參与了架构的设计。对于刚開始学习的人来说,使用Maven构建项目并非一件easy的事情,本文的目的就是引导新手使用maven构建springmvc项目。

准本工作

1、Eclipse

尽量选用较高版本号的Eclispse,由于eclipse对于maven的支持比較晚。

2、Maven

安装maven,至于其安装方式这里也就不再多提了,请自行google。

3、Eclipse选择本地Maven,例如以下图所:

选择本地库的原因是你能够自行指定仓库的位置,也能够指定远程仓库,方便管理。

构建project

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新建三个源文件时,会出现一个奇怪的问题,例如以下所看到的:

一个解决的方法是我们直接定位到项目文件里(磁盘中),手动的新建该三个文件,之后刷新项目就可以。

当中src/test/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配置例如以下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <display-name>LCore</display-name>
  4. <welcome-file-list>
  5. <welcome-file>index.jsp</welcome-file>
  6. </welcome-file-list>
  7.  
  8. <!-- 载入全部的配置文件
  9. 这里我将配置文件置于源代码包中
  10. -->
  11. <context-param>
  12. <param-name>contextConfigLocation</param-name>
  13. <param-value>classpath*:spring-*.xml</param-value>
  14. </context-param>
  15.  
  16. <!-- 配置Spring-->
  17. <listener>
  18. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  19. </listener>
  20.  
  21. <!-- 配置SpringMVC -->
  22. <servlet>
  23. <servlet-name>springMVC</servlet-name>
  24. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  25. <init-param>
  26. <param-name>contextConfigLocation</param-name>
  27. <param-value>classpath*:spring-mvc.xml</param-value>
  28. </init-param>
  29. <load-on-startup>1</load-on-startup>
  30. </servlet>
  31. <servlet-mapping>
  32. <servlet-name>springMVC</servlet-name>
  33. <url-pattern>/</url-pattern>
  34. </servlet-mapping>
  35.  
  36. <!-- 配置字符集 -->
  37. <filter>
  38. <filter-name>encodingFilter</filter-name>
  39. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  40. <init-param>
  41. <param-name>encoding</param-name>
  42. <param-value>UTF-8</param-value>
  43. </init-param>
  44. <init-param>
  45. <param-name>forceEncoding</param-name>
  46. <param-value>true</param-value>
  47. </init-param>
  48. </filter>
  49. <filter-mapping>
  50. <filter-name>encodingFilter</filter-name>
  51. <url-pattern>/*</url-pattern>
  52. </filter-mapping>
  53.  
  54. <!-- 配置Session -->
  55. <filter>
  56. <filter-name>openSession</filter-name>
  57. <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  58. </filter>
  59. <filter-mapping>
  60. <filter-name>openSession</filter-name>
  61. <url-pattern>/*</url-pattern>
  62. </filter-mapping>
  63. </web-app>

配置ContextLoaderListener表示,该project要以spring的方式启动。启动时会默认在/WEB-INF文件夹下查找 spring-common.xml作为spring容器的配置文件,这里能够初始化一些bean,如DataSource。代码例如以下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:p="http://www.springframework.org/schema/p" 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-3.1.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
  7. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
  8.  
  9. <context:property-placeholder location="classpath:jdbc.properties" />
  10. <context:component-scan base-package="com.ceis.core" />
  11.  
  12. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  13. p:driverClassName="${jdbc.driver}"
  14. p:url="${jdbc.url}"
  15. p:username="${jdbc.username}"
  16. p:password="${jdbc.password}" />
  17.  
  18. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  19. <property name="dataSource" ref="dataSource" />
  20. <property name="hibernateProperties">
  21. <props>
  22. <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
  23. <prop key="hibernate.current_session_context_class">thread</prop>
  24. <prop key="hibernate.hbm2ddl.auto">update</prop>
  25. </props>
  26. </property>
  27. <property name="packagesToScan">
  28. <list>
  29. <value>com.ceis.core.po</value>
  30. </list>
  31. </property>
  32. </bean>
  33.  
  34. <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  35. <property name="sessionFactory" ref="sessionFactory" />
  36. </bean>
  37.  
  38. <tx:annotation-driven proxy-target-class="true" />
  39.  
  40. </beans>
          至于jdbc.properties文件jdbc的配置这里就不给出了。

配置DispatcherServlet表示,该project将採用springmvc的方式。启动时也会默认在classPath文件夹下查找spring-mvc.xml作为配置文件,该文件里将配置两项重要的mvc特性:

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

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

这里我额外配置了velocity引擎处理请求和视图解析器(页面通过velocity模板引擎构建)。代码例如以下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.2.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
  12.  
  13. <!-- 注解扫描包 -->
  14. <context:component-scan base-package="com.ceis.core.controller" />
  15. <!-- 启动spring mvc的注解功能,完毕请求和注解POJO的映射 -->
  16. <bean
  17. class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  18. <!-- 配置信息转换,将用@responsebody注解的返回值转换为json返回前台,编码为utf-8-->
  19. <property name="messageConverters">
  20. <list>
  21. </list>
  22. </property>
  23. </bean>
  24. <mvc:interceptors>
  25. <mvc:interceptor>
  26. <mvc:mapping path="/*" />
  27. <!-- 需排除拦截的地址 -->
  28. <mvc:exclude-mapping path="/login" />
  29.  
  30. <bean class="com.ceis.core.Interceptor.SecurityInterceptor" />
  31. </mvc:interceptor>
  32. </mvc:interceptors>
  33. <mvc:annotation-driven />
  34. <!-- 静态资源(js/image)的訪问 -->
  35. <mvc:resources mapping="/resources/**" location="/resources/" />
  36. <!-- 定义视图解析器 -->
  37. <!-- 配置velocity引擎处理请求 -->
  38. <bean id="velocityConfigurer"
  39. class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
  40. <!-- <property name="configLocation"> <value>/WEB-INF/toolbox.xml</value>
  41. </property> -->
  42. <property name="resourceLoaderPath">
  43. <value>/WEB-INF/views/</value>
  44. </property>
  45. <property name="velocityProperties">
  46. <props>
  47. <prop key="input.encoding">UTF-8</prop>
  48. <prop key="output.encoding">UTF-8</prop>
  49. </props>
  50. </property>
  51. </bean>
  52.  
  53. <!-- 配置velocity视图解析器 -->
  54. <bean id="viewResolver"
  55. class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
  56. <property name="suffix">
  57. <value>.vm</value>
  58. </property>
  59. <property name="contentType" value="text/html;charset=UTF-8"></property>
  60. </bean>
  61. </beans>

       
2、Maven配置jar包
         

传统引入jar的方式是将其放入web-inf->lib文件夹里面,无形中增大了项目,并且jar不能统一进行管理。使用Maven的优点之中的一个就是通过配置POM.XML文件自己主动下载jar包,并且通过中心库统一进行管理、版本号的控制等。

这里我们须要引入spring-mvc、servlet特性相关的包,以及tomcat插件(执行)

  1. <?xml version="1.0"?>
  2. <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"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>debug</groupId>
  6. <artifactId>debug</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>war</packaging>
  9. <dependencies>
  10. <dependency>
  11. <groupId>org.springframework</groupId>
  12. <artifactId>spring-webmvc</artifactId>
  13. <version>3.1.2.RELEASE</version>
  14. <scope>compile</scope>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework</groupId>
  18. <artifactId>spring-orm</artifactId>
  19. <version>3.1.2.RELEASE</version>
  20. <scope>compile</scope>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework</groupId>
  24. <artifactId>spring-test</artifactId>
  25. <version>3.1.2.RELEASE</version>
  26. <scope>compile</scope>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.slf4j</groupId>
  30. <artifactId>jcl-over-slf4j</artifactId>
  31. <version>1.6.6</version>
  32. <scope>compile</scope>
  33. </dependency>
  34. <dependency>
  35. <groupId>mysql</groupId>
  36. <artifactId>mysql-connector-java</artifactId>
  37. <version>5.1.21</version>
  38. <scope>compile</scope>
  39. </dependency>
  40. <dependency>
  41. <groupId>jstl</groupId>
  42. <artifactId>jstl</artifactId>
  43. <version>1.2</version>
  44. <scope>compile</scope>
  45. </dependency>
  46. <dependency>
  47. <groupId>taglibs</groupId>
  48. <artifactId>standard</artifactId>
  49. <version>1.1.2</version>
  50. <scope>compile</scope>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.hibernate</groupId>
  54. <artifactId>hibernate-c3p0</artifactId>
  55. <version>3.6.0.Final</version>
  56. <scope>compile</scope>
  57. </dependency>
  58. <dependency>
  59. <groupId>org.hibernate</groupId>
  60. <artifactId>hibernate-ehcache</artifactId>
  61. <version>3.6.10.Final</version>
  62. <scope>compile</scope>
  63. </dependency>
  64. <dependency>
  65. <groupId>org.springframework</groupId>
  66. <artifactId>spring-context</artifactId>
  67. <version>3.1.2.RELEASE</version>
  68. <scope>compile</scope>
  69. </dependency>
  70. <dependency>
  71. <groupId>org.springframework</groupId>
  72. <artifactId>spring-tx</artifactId>
  73. <version>3.1.2.RELEASE</version>
  74. <scope>compile</scope>
  75. </dependency>
  76. <dependency>
  77. <groupId>org.springframework</groupId>
  78. <artifactId>spring-mock</artifactId>
  79. <version>2.0.8</version>
  80. <scope>compile</scope>
  81. </dependency>
  82. <dependency>
  83. <groupId>commons-lang</groupId>
  84. <artifactId>commons-lang</artifactId>
  85. <version>2.5</version>
  86. <scope>compile</scope>
  87. </dependency>
  88. <dependency>
  89. <groupId>cglib</groupId>
  90. <artifactId>cglib</artifactId>
  91. <version>2.1_3</version>
  92. <scope>compile</scope>
  93. </dependency>
  94. <dependency>
  95. <groupId>javassist</groupId>
  96. <artifactId>javassist</artifactId>
  97. <version>3.4.GA</version>
  98. <scope>compile</scope>
  99. </dependency>
  100. <dependency>
  101. <groupId>org.hibernate</groupId>
  102. <artifactId>hibernate-core</artifactId>
  103. <version>3.5.6-Final</version>
  104. <scope>compile</scope>
  105. </dependency>
  106. <dependency>
  107. <groupId>org.hibernate</groupId>
  108. <artifactId>hibernate-annotations</artifactId>
  109. <version>3.5.6-Final</version>
  110. <scope>compile</scope>
  111. </dependency>
  112. <dependency>
  113. <groupId>org.codehaus.jackson</groupId>
  114. <artifactId>jackson-core-asl</artifactId>
  115. <version>1.9.8</version>
  116. <scope>compile</scope>
  117. </dependency>
  118. <dependency>
  119. <groupId>org.codehaus.jackson</groupId>
  120. <artifactId>jackson-mapper-asl</artifactId>
  121. <version>1.9.8</version>
  122. <scope>compile</scope>
  123. </dependency>
  124. <dependency>
  125. <groupId>javax.servlet</groupId>
  126. <artifactId>servlet-api</artifactId>
  127. <version>2.4</version>
  128. <scope>provided</scope>
  129. </dependency>
  130. <dependency>
  131. <groupId>commons-dbcp</groupId>
  132. <artifactId>commons-dbcp</artifactId>
  133. <version>1.2.2</version>
  134. <scope>compile</scope>
  135. </dependency>
  136. <dependency>
  137. <groupId>org.apache.velocity</groupId>
  138. <artifactId>velocity</artifactId>
  139. <version>1.7</version>
  140. <scope>compile</scope>
  141. </dependency>
  142. <dependency>
  143. <groupId>org.apache.velocity</groupId>
  144. <artifactId>velocity-tools</artifactId>
  145. <version>2.0</version>
  146. <scope>compile</scope>
  147. </dependency>
  148. </dependencies>
  149. <repositories>
  150. <repository>
  151. <snapshots>
  152. <enabled>false</enabled>
  153. </snapshots>
  154. <id>central</id>
  155. <name>Central Repository</name>
  156. <url>http://repo.maven.apache.org/maven2</url>
  157. </repository>
  158. </repositories>
  159. <pluginRepositories>
  160. <pluginRepository>
  161. <releases>
  162. <updatePolicy>never</updatePolicy>
  163. </releases>
  164. <snapshots>
  165. <enabled>false</enabled>
  166. </snapshots>
  167. <id>central</id>
  168. <name>Central Repository</name>
  169. <url>http://repo.maven.apache.org/maven2</url>
  170. </pluginRepository>
  171. </pluginRepositories>
  172. <build>
  173. <sourceDirectory>C:\Users\LCore\git\debug\debug\src</sourceDirectory>
  174. <scriptSourceDirectory>C:\Users\LCore\git\debug\debug\src\main\scripts</scriptSourceDirectory>
  175. <testSourceDirectory>C:\Users\LCore\git\debug\debug\src\test\java</testSourceDirectory>
  176. <outputDirectory>C:\Users\LCore\git\debug\debug\target\classes</outputDirectory>
  177. <testOutputDirectory>C:\Users\LCore\git\debug\debug\target\test-classes</testOutputDirectory>
  178. <resources>
  179. <resource>
  180. <directory>C:\Users\LCore\git\debug\debug\src\main\resources</directory>
  181. </resource>
  182. </resources>
  183. <testResources>
  184. <testResource>
  185. <directory>C:\Users\LCore\git\debug\debug\src\test\resources</directory>
  186. </testResource>
  187. </testResources>
  188. <directory>C:\Users\LCore\git\debug\debug\target</directory>
  189. <finalName>debug-0.0.1-SNAPSHOT</finalName>
  190. <pluginManagement>
  191. <plugins>
  192. <plugin>
  193. <artifactId>maven-antrun-plugin</artifactId>
  194. <version>1.3</version>
  195. </plugin>
  196. <plugin>
  197. <artifactId>maven-assembly-plugin</artifactId>
  198. <version>2.2-beta-5</version>
  199. </plugin>
  200. <plugin>
  201. <artifactId>maven-dependency-plugin</artifactId>
  202. <version>2.8</version>
  203. </plugin>
  204. <plugin>
  205. <artifactId>maven-release-plugin</artifactId>
  206. <version>2.3.2</version>
  207. </plugin>
  208. </plugins>
  209. </pluginManagement>
  210. <plugins>
  211. <plugin>
  212. <groupId>org.codehaus.mojo</groupId>
  213. <artifactId>tomcat-maven-plugin</artifactId>
  214. <version>1.1</version>
  215. <configuration>
  216. <url>http://127.0.0.1:8080/manager</url>
  217. <port>8080</port>
  218. <server>TomcatServer</server>
  219. <path>/debug</path>
  220. <uriEncoding>UTF-8</uriEncoding>
  221. </configuration>
  222. </plugin>
  223. <plugin>
  224. <artifactId>maven-compiler-plugin</artifactId>
  225. <version>2.5.1</version>
  226. <executions>
  227. <execution>
  228. <id>default-testCompile</id>
  229. <phase>test-compile</phase>
  230. <goals>
  231. <goal>testCompile</goal>
  232. </goals>
  233. <configuration>
  234. <source>1.7</source>
  235. <target>1.7</target>
  236. <encoding>UTF-8</encoding>
  237. </configuration>
  238. </execution>
  239. <execution>
  240. <id>default-compile</id>
  241. <phase>compile</phase>
  242. <goals>
  243. <goal>compile</goal>
  244. </goals>
  245. <configuration>
  246. <source>1.7</source>
  247. <target>1.7</target>
  248. <encoding>UTF-8</encoding>
  249. </configuration>
  250. </execution>
  251. </executions>
  252. <configuration>
  253. <source>1.7</source>
  254. <target>1.7</target>
  255. <encoding>UTF-8</encoding>
  256. </configuration>
  257. </plugin>
  258. <plugin>
  259. <artifactId>maven-clean-plugin</artifactId>
  260. <version>2.5</version>
  261. <executions>
  262. <execution>
  263. <id>default-clean</id>
  264. <phase>clean</phase>
  265. <goals>
  266. <goal>clean</goal>
  267. </goals>
  268. </execution>
  269. </executions>
  270. </plugin>
  271. <plugin>
  272. <artifactId>maven-install-plugin</artifactId>
  273. <version>2.4</version>
  274. <executions>
  275. <execution>
  276. <id>default-install</id>
  277. <phase>install</phase>
  278. <goals>
  279. <goal>install</goal>
  280. </goals>
  281. </execution>
  282. </executions>
  283. </plugin>
  284. <plugin>
  285. <artifactId>maven-resources-plugin</artifactId>
  286. <version>2.6</version>
  287. <executions>
  288. <execution>
  289. <id>default-resources</id>
  290. <phase>process-resources</phase>
  291. <goals>
  292. <goal>resources</goal>
  293. </goals>
  294. </execution>
  295. <execution>
  296. <id>default-testResources</id>
  297. <phase>process-test-resources</phase>
  298. <goals>
  299. <goal>testResources</goal>
  300. </goals>
  301. </execution>
  302. </executions>
  303. </plugin>
  304. <plugin>
  305. <artifactId>maven-surefire-plugin</artifactId>
  306. <version>2.12.4</version>
  307. <executions>
  308. <execution>
  309. <id>default-test</id>
  310. <phase>test</phase>
  311. <goals>
  312. <goal>test</goal>
  313. </goals>
  314. </execution>
  315. </executions>
  316. </plugin>
  317. <plugin>
  318. <artifactId>maven-war-plugin</artifactId>
  319. <version>2.2</version>
  320. <executions>
  321. <execution>
  322. <id>default-war</id>
  323. <phase>package</phase>
  324. <goals>
  325. <goal>war</goal>
  326. </goals>
  327. </execution>
  328. </executions>
  329. </plugin>
  330. <plugin>
  331. <artifactId>maven-deploy-plugin</artifactId>
  332. <version>2.7</version>
  333. <executions>
  334. <execution>
  335. <id>default-deploy</id>
  336. <phase>deploy</phase>
  337. <goals>
  338. <goal>deploy</goal>
  339. </goals>
  340. </execution>
  341. </executions>
  342. </plugin>
  343. <plugin>
  344. <artifactId>maven-site-plugin</artifactId>
  345. <version>3.3</version>
  346. <executions>
  347. <execution>
  348. <id>default-site</id>
  349. <phase>site</phase>
  350. <goals>
  351. <goal>site</goal>
  352. </goals>
  353. <configuration>
  354. <outputDirectory>C:\Users\LCore\git\debug\debug\target\site</outputDirectory>
  355. <reportPlugins>
  356. <reportPlugin>
  357. <groupId>org.apache.maven.plugins</groupId>
  358. <artifactId>maven-project-info-reports-plugin</artifactId>
  359. </reportPlugin>
  360. </reportPlugins>
  361. </configuration>
  362. </execution>
  363. <execution>
  364. <id>default-deploy</id>
  365. <phase>site-deploy</phase>
  366. <goals>
  367. <goal>deploy</goal>
  368. </goals>
  369. <configuration>
  370. <outputDirectory>C:\Users\LCore\git\debug\debug\target\site</outputDirectory>
  371. <reportPlugins>
  372. <reportPlugin>
  373. <groupId>org.apache.maven.plugins</groupId>
  374. <artifactId>maven-project-info-reports-plugin</artifactId>
  375. </reportPlugin>
  376. </reportPlugins>
  377. </configuration>
  378. </execution>
  379. </executions>
  380. <configuration>
  381. <outputDirectory>C:\Users\LCore\git\debug\debug\target\site</outputDirectory>
  382. <reportPlugins>
  383. <reportPlugin>
  384. <groupId>org.apache.maven.plugins</groupId>
  385. <artifactId>maven-project-info-reports-plugin</artifactId>
  386. </reportPlugin>
  387. </reportPlugins>
  388. </configuration>
  389. </plugin>
  390. </plugins>
  391. </build>
  392. <reporting>
  393. <outputDirectory>C:\Users\LCore\git\debug\debug\target\site</outputDirectory>
  394. </reporting>
  395. </project

          上述配置中有关于git的都能够删掉。

測试

经过一些努力最终配置好了springmvc,以下进行一个简单的登录測试。

LoginController:

  1. package com.ceis.core.controller.login;
  2.  
  3. import javax.annotation.Resource;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import javax.servlet.http.HttpSession;
  7.  
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.servlet.ModelAndView;
  11.  
  12. import com.ceis.core.controller.BaseController;
  13. import com.ceis.core.po.User;
  14. import com.ceis.core.service.UserService;
  15.  
  16. @Controller
  17. public class LoginController extends BaseController{
  18.  
  19. @Resource(name="userService")
  20. private UserService userService;
  21. @RequestMapping("/login")
  22. public ModelAndView login(User user,HttpServletRequest request,HttpServletResponse response)
  23. {
  24. ModelAndView view = null;
  25. user.setPassword(request.getParameter("password"));
  26. user.setName(request.getParameter("username"));
  27. if(userService.login(user.getName(),user.getPassword())){
  28. view = createLayoutView("content");
  29. request.getSession().setAttribute("user", user);
  30. return view;
  31. }
  32. else
  33. return new ModelAndView("login");
  34. }
  35.  
  36. /**
  37. * 退出登录必须清空session
  38. * @param user
  39. * @param request
  40. * @return the login.jsp
  41. */
  42. @RequestMapping("/loginOut")
  43. public String loginOut(User user,HttpServletRequest request)
  44. {
  45. HttpSession httpSession = request.getSession();
  46. httpSession.removeAttribute("user");
  47. httpSession.invalidate();//session不可用
  48. return "redirect:login.jsp";//请求重定向到首页
  49. }
  50. }

         至于service的代码这里就不给出了。

登录页面的核心代码:

  1. <!-- Login Screen -->
  2. <div class="login-wrapper">
  3. <div class="login-container">
  4. <img width="100" height="30" src="resources/images/logo-login-big.png" />
  5. <form action="login" method="post">
  6. <div class="form-group">
  7. <input class="form-control" placeholder="用户名"
  8. type="text" name="username">
  9. </div>
  10. <div class="form-group">
  11. <input class="form-control" placeholder="password" type="password" name="password"><input
  12. type="submit" value="">
  13. </div>
  14. <div class="form-options clearfix">
  15. <a class="pull-right" href="#">忘记password?</a>
  16. <div class="text-left">
  17. <label class="checkbox"><input type="checkbox"><span>记住password</span></label>
  18. </div>
  19. </div>
  20. </form>
  21. <div class="social-login clearfix">
  22. <a class="btn btn-primary pull-left facebook" href=""><i
  23. class="icon-facebook"></i>Facebook login</a><a
  24. class="btn btn-primary pull-right twitter" href=""><i
  25. class="icon-twitter"></i>Twitter login</a>
  26. </div>
  27. <p class="signup">
  28. 没有账户? <a href="signup">马上注冊</a>
  29. </p>
  30. </div>
  31. </div>

          form表单的action的值就是LoginController的login方法的requestMapping路径。        

点击登录之后顺利跳转至主页:

Eclipse Maven构建Spring MVC项目的更多相关文章

  1. 用maven创建Spring MVC项目

    用maven创建Spring MVC项目 mvn archetype:generate -DgroupId=fry-arthur -DartifactId=spring-mvc-study -Darc ...

  2. 使用maven, myeclipse工具构建spring mvc项目

    一.使用myeclipse 创建一个新的 maven项目. (ps:1.在filter过滤的时候输入 webapp 选择"maven-archetype-webapp". 2.在m ...

  3. IDEA 通过Maven创建Spring MVC项目搭建

    概述 本篇随笔主要记录内容如下: 1.通过Maven创建基于Spring Framework类库的MVC项目,免去了繁琐的XML配置: 2.在Idea里面配置Tomcat的测试启动项: Maven创建 ...

  4. 使用maven一步一步构建spring mvc项目

    1      使用eclipse构建maven web项目 1.1新建Maven的web项目 打开菜单File –New-MavenProject. 点击Next 选择模板类型archtype——ma ...

  5. 安装eclipse版本oxygen,及maven导入spring mvc项目并运行

    本文地址为:http://www.cnblogs.com/jying/p/7511598.html 系统环境: win10 eclipse版本:2017.09.11 官网下载版本号为 oxygen 1 ...

  6. eclipse maven 构建简单springmvc项目

    环境:eclipse Version: Oxygen.3a Release (4.7.3a) 创建maven Project项目,目录结构 修改工程的相关编译属性 修改pop.xml,引入spring ...

  7. maven web+spring mvc项目没有出现src/main/java路径

    直接在main 文件夹下创建java可以解决 https://www.cnblogs.com/zhujiabin/p/6343462.html

  8. maven 纯注解一步一步搭建Spring Mvc项目(入门)

    初次接触spring MVC项目,通过一段时间的学习,本文介绍一种以纯注解的方法去配置spring MVC环境,让那些配置的.xml文件统统见鬼吧. 什么是Spring MVC Spring MVC属 ...

  9. 第一个使用Spring Tool Suite(STS)和Maven建立的Spring mvc 项目

    一.目标 在这篇文章中.我将要向您展示怎样使用Spring Frameworks 和 Maven build创建您的第一个J2ee 应用程序. 二.信息 Maven是一个java项目的构建工具(或者自 ...

随机推荐

  1. Ubuntu12.04编译Android4.0.1源码全过程-----附wubi安装ubuntu编译android源码硬盘空间不够的问题解决

    昨晚在编译源码,make一段时间之后报错如下: # A fatal error has been detected by the Java Runtime Environment: # # SIGSE ...

  2. msyql在查询字段中的所有记录,不重复

    mysql> select * from a ;  +----+------+--------------+ | id | name | descri       | +----+------+ ...

  3. SqlServer表EXCEL数据复制的另一种方法

    一个.SqlServer表中的数据复制到excel 1.新建查询,用sql语句把表数据读出来 2.然后,选择数据,右键.复制(也能够点击连同标题复制),拷贝到记事本中(不然会乱码) 3.然后再把记事本 ...

  4. Test SRM Level Three: LargestCircle, Brute Force

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3005&rd=5858 思路: 如果直接用Brute F ...

  5. 解决编译时出错提示: 'error: array must be initialized with a brace-enclosed initializer' 的错误

    编译出现这个错误的原因非常简单编译的标准不相同.如果用stdc90,这个就可以直接编译通过了. 下面是代码例子: ...... ] = NULL;或者 :char cmd[256] = '\0'; . ...

  6. OTN&互换amp; P-OTN有效降低100G 网络成本 (两)

    OTN互换& P-OTN有效降低100G 网络成本 (两) 在全球范围内.网流量的增长速度是空前的,导致此现象的缘由包含云服务的增长.移动宽带和基于互联网的视频点播服务的增长. Cisco估计 ...

  7. ECG信号读出,检测QRS,P,T 波(小波去噪,并根据检测),基于BP辨识的神经网络

    这学期的课程选择神经网络.最后的作业处理ECG信号,并利用神经网络识别. 1  ECG引进和阅读ECG信号 1)ECG介绍  详细ECG背景应用就不介绍了,大家能够參考百度 谷歌.仅仅是简单说下ECG ...

  8. Eclipse在Jar形成和应用程序包

    最近的熟悉Java语言.在学习过程中Eclipse经常使用再熟悉它.本文简单说下Jar形成和应用程序包. Java在Jar相当于包C/C++该lib库,它是.class文件打包:经常使用Jar包有AP ...

  9. ZOJ 1203 Swordfish 旗鱼 最小生成树,Kruskal算法

    主题链接:problemId=203" target="_blank">ZOJ 1203 Swordfish 旗鱼 Swordfish Time Limit: 2 ...

  10. telnet发电子邮件

    无聊今天的工作,想想一个学生被提到最后一次telnet发电子邮件,所以我想试试.最后,成功的实践,这里做个总结. 首先,cmd进telnet打开回话: 下面红色字体为命令. 1.open smtp.1 ...