使用Maven构建Spring MVC项目的简单示例

1、首先看一下项目结构:

总结:主要是将配置文件配置好之后就不会有什么问题了。在阅读《Maven实战》这本书的时候可以知道有一章是讲解关于继承和聚合的知识,这里主要的是Maven构建SpringMVC项目,所以DAO等这些都写到一起了。因为我也没有只用Maven进行过一个完整项目的实践,所以独立模块开发+聚合可以参考《Maven实战》这本书上面的示例讲解。作为初学者,有很多的东西要学。

附源代码地址:点击打开链接

下面是配置文件及代码:

2、pom.xml

 

[html][/html] view plaincopy

  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>MavenTest</groupId>
  5.     <artifactId>tan.maven.springmvc</artifactId>
  6.     <packaging>war</packaging>
  7.     <version>0.0.1-SNAPSHOT</version>
  8.     <name>tan.maven.springmvc Maven Webapp</name>
  9.     <url>http://maven.apache.org</url>
  10.     <properties>
  11.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12.         <springversion>3.1.1.RELEASE</springversion>
  13.         <junitversion>3.8.1</junitversion>
  14.     </properties>
  15.     <dependencies>
  16.         <dependency>
  17.             <groupId>junit</groupId>
  18.             <artifactId>junit</artifactId>
  19.             <version>${junitversion}</version>
  20.             <scope>test</scope>
  21.         </dependency>
  22.         <dependency>
  23.             <groupId>org.springframework</groupId>
  24.             <artifactId>spring-aop</artifactId>
  25.             <version>${springversion}</version>
  26.             <type>jar</type>
  27.             <scope>compile</scope>
  28.         </dependency>
  29.         <dependency>
  30.             <groupId>org.springframework</groupId>
  31.             <artifactId>spring-asm</artifactId>
  32.             <version>${springversion}</version>
  33.             <type>jar</type>
  34.             <scope>compile</scope>
  35.         </dependency>
  36.         <dependency>
  37.             <groupId>org.springframework</groupId>
  38.             <artifactId>spring-aspects</artifactId>
  39.             <version>${springversion}</version>
  40.             <type>jar</type>
  41.             <scope>compile</scope>
  42.         </dependency>
  43.         <dependency>
  44.             <groupId>org.springframework</groupId>
  45.             <artifactId>spring-beans</artifactId>
  46.             <version>${springversion}</version>
  47.             <type>jar</type>
  48.             <scope>compile</scope>
  49.         </dependency>
  50.         <dependency>
  51.             <groupId>org.springframework</groupId>
  52.             <artifactId>spring-context</artifactId>
  53.             <version>${springversion}</version>
  54.             <type>jar</type>
  55.             <scope>compile</scope>
  56.         </dependency>
  57.         <dependency>
  58.             <groupId>org.springframework</groupId>
  59.             <artifactId>spring-context-support</artifactId>
  60.             <version>${springversion}</version>
  61.             <type>jar</type>
  62.             <scope>compile</scope>
  63.         </dependency>
  64.         <dependency>
  65.             <groupId>org.springframework</groupId>
  66.             <artifactId>spring-core</artifactId>
  67.             <version>${springversion}</version>
  68.             <type>jar</type>
  69.             <scope>compile</scope>
  70.         </dependency>
  71.         <dependency>
  72.             <groupId>org.springframework</groupId>
  73.             <artifactId>spring-expression</artifactId>
  74.             <version>${springversion}</version>
  75.             <type>jar</type>
  76.             <scope>compile</scope>
  77.         </dependency>
  78.         <dependency>
  79.             <groupId>org.springframework</groupId>
  80.             <artifactId>spring-jdbc</artifactId>
  81.             <version>${springversion}</version>
  82.             <type>jar</type>
  83.             <scope>compile</scope>
  84.         </dependency>
  85.         <dependency>
  86.             <groupId>org.springframework</groupId>
  87.             <artifactId>spring-jms</artifactId>
  88.             <version>${springversion}</version>
  89.             <type>jar</type>
  90.             <scope>compile</scope>
  91.         </dependency>
  92.         <dependency>
  93.             <groupId>org.springframework</groupId>
  94.             <artifactId>spring-orm</artifactId>
  95.             <version>${springversion}</version>
  96.             <type>jar</type>
  97.             <scope>compile</scope>
  98.         </dependency>
  99.         <dependency>
  100.             <groupId>org.springframework</groupId>
  101.             <artifactId>spring-oxm</artifactId>
  102.             <version>${springversion}</version>
  103.             <type>jar</type>
  104.             <scope>compile</scope>
  105.         </dependency>
  106.         <dependency>
  107.             <groupId>org.springframework</groupId>
  108.             <artifactId>spring-tx</artifactId>
  109.             <version>${springversion}</version>
  110.             <type>jar</type>
  111.             <scope>compile</scope>
  112.         </dependency>
  113.         <dependency>
  114.             <groupId>org.springframework</groupId>
  115.             <artifactId>spring-web</artifactId>
  116.             <version>${springversion}</version>
  117.             <type>jar</type>
  118.             <scope>compile</scope>
  119.         </dependency>
  120.         <dependency>
  121.             <groupId>org.springframework</groupId>
  122.             <artifactId>spring-webmvc</artifactId>
  123.             <version>${springversion}</version>
  124.             <type>jar</type>
  125.             <scope>compile</scope>
  126.         </dependency>
  127.         <dependency>
  128.             <groupId>org.springframework</groupId>
  129.             <artifactId>spring-test</artifactId>
  130.             <version>${springversion}</version>
  131.             <type>jar</type>
  132.             <scope>compile</scope>
  133.         </dependency>
  134.         <dependency>
  135.             <groupId>javax.servlet</groupId>
  136.             <artifactId>jstl</artifactId>
  137.             <version>1.2</version>
  138.             <type>jar</type>
  139.             <scope>compile</scope>
  140.         </dependency>
  141.         <dependency>
  142.             <groupId>commons-collections</groupId>
  143.             <artifactId>commons-collections</artifactId>
  144.             <version>3.1</version>
  145.         </dependency>
  146.         <dependency>
  147.             <groupId>commons-logging</groupId>
  148.             <artifactId>commons-logging</artifactId>
  149.             <version>1.1</version>
  150.         </dependency>
  151.     </dependencies>
  152.     <build>
  153.         <finalName>tan-springmvc-book</finalName>
  154.     </build>
  155. </project>

3、applicationContext.xml配置

 

 

[html][/html] view plaincopy

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <beans xmlns=”http://www.springframework.org/schema/beans”
  3.        xmlns:aop=”http://www.springframework.org/schema/aop”
  4.        xmlns:context=”http://www.springframework.org/schema/context”
  5.        xmlns:mvc=”http://www.springframework.org/schema/mvc”
  6.        xmlns:tx=”http://www.springframework.org/schema/tx”
  7.        xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
  8.        xsi:schemaLocation=”http://www.springframework.org/schema/aop
  9.         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  10.         http://www.springframework.org/schema/beans
  11.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  12.         http://www.springframework.org/schema/context
  13.         http://www.springframework.org/schema/context/spring-context-3.0.xsd
  14.         http://www.springframework.org/schema/mvc
  15.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  16.         http://www.springframework.org/schema/tx
  17.         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd”>
  18.     <mvc:annotation-driven />
  19.     <context:component-scan base-package=”com.tan.*” />
  20.     <bean class=”org.springframework.web.servlet.view.InternalResourceViewResolver”>
  21.         <property name=”prefix” value=”/” />
  22.         <property name=”suffix” value=”.jsp” />
  23.     </bean>
  24. </beans>

4、web.xml配置

 

 

[html][/html] view plaincopy

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <web-app version=”3.0″ xmlns=”http://java.sun.com/xml/ns/javaee”
  3.     xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
  4.     xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee
  5.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd”>
  6.     <display-name></display-name>
  7.     <welcome-file-list>
  8.         <welcome-file>index.jsp</welcome-file>
  9.     </welcome-file-list>
  10.         <!– Spring的log4j监听器 –>
  11.     <listener>
  12.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  13.     </listener>
  14.     <!– 核心控制器 –>
  15.     <servlet>
  16.         <servlet-name>book</servlet-name>
  17.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  18.         <init-param>
  19.             <param-name>contextConfigLocation</param-name>
  20.             <param-value>/WEB-INF/applicationContext.xml</param-value>
  21.         </init-param>
  22.         <load-on-startup>1</load-on-startup>
  23.     </servlet>
  24.     <listener>
  25.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  26.     </listener>
  27.     <servlet-mapping>
  28.         <servlet-name>book</servlet-name>
  29.         <url-pattern>/</url-pattern>
  30.     </servlet-mapping>
  31. </web-app>

5、model:book.java

 

 

[java][/java] view plaincopy

  1. package com.tan.model;
  2. public class Book {
  3.     private int id;
  4.     private String name;
  5.     private String author;
  6.     public Book(){}
  7.     public Book(int id, String name, String author) {
  8.         super();
  9.         this.id = id;
  10.         this.name = name;
  11.         this.author = author;
  12.     }
  13.     public int getId() {
  14.         return id;
  15.     }
  16.     public void setId(int id) {
  17.         this.id = id;
  18.     }
  19.     public String getName() {
  20.         return name;
  21.     }
  22.     public void setName(String name) {
  23.         this.name = name;
  24.     }
  25.     public String getAuthor() {
  26.         return author;
  27.     }
  28.     public void setAuthor(String author) {
  29.         this.author = author;
  30.     }
  31. }

6、Dao:bookDao.java

 

 

[java][/java] view plaincopy

  1. package com.tan.dao;
  2. import org.springframework.stereotype.Component;
  3. import com.tan.model.Book;
  4. @Component
  5. public class BookDao {
  6.     //模拟数据库操作
  7.     public void add(Book book){
  8.         System.out.print(“Add”);
  9.     }
  10.     public void update(Book book){
  11.         System.out.print(“Update”);
  12.     }
  13. }

7、Service:BookService.java

 

 

[java][/java] view plaincopy

  1. package com.tan.service;
  2. import javax.annotation.Resource;
  3. import org.springframework.stereotype.Component;
  4. import com.tan.dao.BookDao;
  5. import com.tan.model.Book;
  6. @Component
  7. public class BookService {
  8.     private BookDao bookDao;
  9.     public BookDao getBookDao() {
  10.         return bookDao;
  11.     }
  12.     @Resource
  13.     public void setBookDao(BookDao bookDao) {
  14.         this.bookDao = bookDao;
  15.     }
  16.     public void add(Book book){
  17.         bookDao.add(book);
  18.     }
  19.     public void update(Book book){
  20.         bookDao.update(book);
  21.     }
  22. }

8、controller:BookController.java

 

 

[java][/java] view plaincopy

  1. package com.tan.controller;
  2. import javax.annotation.Resource;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import com.tan.model.Book;
  6. import com.tan.service.BookService;
  7. @Controller
  8. @RequestMapping(“/book.do”)
  9. public class BookController {
  10.     private BookService bookService;
  11.     @RequestMapping(params = “method=add”)
  12.     public String add(Book book){
  13.         System.out.println(“bookname:”+book.getName());
  14.         System.out.println(“author:”+book.getAuthor());
  15.         bookService.add(book);
  16.         return “success”;
  17.     }
  18.     @RequestMapping(params = “method=update”)
  19.     public String update(Book book) {
  20.         bookService.update(book);
  21.         return “success”;
  22.     }
  23.     public BookService getBookService() {
  24.         return bookService;
  25.     }
  26.     @Resource
  27.     public void setBookService(BookService bookService) {
  28.         this.bookService = bookService;
  29.     }
  30. }

9、index.jsp

 

 

[html][/html] view plaincopy

  1. <html>
  2. <body>
  3. <h2>Add Book</h2>
  4. <form method=”post” action=”<%=request.getContextPath() %>/book.do?method=add”>
  5. bookname:<input type=”text” name=”name” id=”name”>
  6. author:<input type=”text” name=”author” id=”author”>
  7. <input type=”submit” value=”Add”>
  8. </form>
  9. </body>
  10. </html>

标签