Chinaunix首页 | 论坛 | 博客
  • 博客访问: 460708
  • 博文数量: 101
  • 博客积分: 578
  • 博客等级: 中士
  • 技术积分: 872
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-12 22:24
文章分类

全部博文(101)

文章存档

2015年(1)

2014年(83)

2012年(17)

我的朋友

分类: 其他平台

2014-04-06 14:46:46



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

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

附源代码地址:

下面是配置文件及代码:

2、pom.xml


[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.0modelVersion>  
  4.     <groupId>MavenTestgroupId>  
  5.     <artifactId>tan.maven.springmvcartifactId>  
  6.     <packaging>warpackaging>  
  7.     <version>0.0.1-SNAPSHOTversion>  
  8.     <name>tan.maven.springmvc Maven Webappname>  
  9.     <url>http://maven.apache.orgurl>  
  10.     <properties>  
  11.         <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>  
  12.         <springversion>3.1.1.RELEASEspringversion>  
  13.         <junitversion>3.8.1junitversion>  
  14.     properties>  
  15.   
  16.     <dependencies>  
  17.         <dependency>  
  18.             <groupId>junitgroupId>  
  19.             <artifactId>junitartifactId>  
  20.             <version>${junitversion}version>  
  21.             <scope>testscope>  
  22.         dependency>  
  23.         <dependency>  
  24.             <groupId>org.springframeworkgroupId>  
  25.             <artifactId>spring-aopartifactId>  
  26.             <version>${springversion}version>  
  27.             <type>jartype>  
  28.             <scope>compilescope>  
  29.         dependency>  
  30.         <dependency>  
  31.             <groupId>org.springframeworkgroupId>  
  32.             <artifactId>spring-asmartifactId>  
  33.             <version>${springversion}version>  
  34.             <type>jartype>  
  35.             <scope>compilescope>  
  36.         dependency>  
  37.         <dependency>  
  38.             <groupId>org.springframeworkgroupId>  
  39.             <artifactId>spring-aspectsartifactId>  
  40.             <version>${springversion}version>  
  41.             <type>jartype>  
  42.             <scope>compilescope>  
  43.         dependency>  
  44.         <dependency>  
  45.             <groupId>org.springframeworkgroupId>  
  46.             <artifactId>spring-beansartifactId>  
  47.             <version>${springversion}version>  
  48.             <type>jartype>  
  49.             <scope>compilescope>  
  50.         dependency>  
  51.         <dependency>  
  52.             <groupId>org.springframeworkgroupId>  
  53.             <artifactId>spring-contextartifactId>  
  54.             <version>${springversion}version>  
  55.             <type>jartype>  
  56.             <scope>compilescope>  
  57.         dependency>  
  58.         <dependency>  
  59.             <groupId>org.springframeworkgroupId>  
  60.             <artifactId>spring-context-supportartifactId>  
  61.             <version>${springversion}version>  
  62.             <type>jartype>  
  63.             <scope>compilescope>  
  64.         dependency>  
  65.         <dependency>  
  66.             <groupId>org.springframeworkgroupId>  
  67.             <artifactId>spring-coreartifactId>  
  68.             <version>${springversion}version>  
  69.             <type>jartype>  
  70.             <scope>compilescope>  
  71.         dependency>  
  72.         <dependency>  
  73.             <groupId>org.springframeworkgroupId>  
  74.             <artifactId>spring-expressionartifactId>  
  75.             <version>${springversion}version>  
  76.             <type>jartype>  
  77.             <scope>compilescope>  
  78.         dependency>  
  79.         <dependency>  
  80.             <groupId>org.springframeworkgroupId>  
  81.             <artifactId>spring-jdbcartifactId>  
  82.             <version>${springversion}version>  
  83.             <type>jartype>  
  84.             <scope>compilescope>  
  85.         dependency>  
  86.         <dependency>  
  87.             <groupId>org.springframeworkgroupId>  
  88.             <artifactId>spring-jmsartifactId>  
  89.             <version>${springversion}version>  
  90.             <type>jartype>  
  91.             <scope>compilescope>  
  92.         dependency>  
  93.         <dependency>  
  94.             <groupId>org.springframeworkgroupId>  
  95.             <artifactId>spring-ormartifactId>  
  96.             <version>${springversion}version>  
  97.             <type>jartype>  
  98.             <scope>compilescope>  
  99.         dependency>  
  100.         <dependency>  
  101.             <groupId>org.springframeworkgroupId>  
  102.             <artifactId>spring-oxmartifactId>  
  103.             <version>${springversion}version>  
  104.             <type>jartype>  
  105.             <scope>compilescope>  
  106.         dependency>  
  107.         <dependency>  
  108.             <groupId>org.springframeworkgroupId>  
  109.             <artifactId>spring-txartifactId>  
  110.             <version>${springversion}version>  
  111.             <type>jartype>  
  112.             <scope>compilescope>  
  113.         dependency>  
  114.         <dependency>  
  115.             <groupId>org.springframeworkgroupId>  
  116.             <artifactId>spring-webartifactId>  
  117.             <version>${springversion}version>  
  118.             <type>jartype>  
  119.             <scope>compilescope>  
  120.         dependency>  
  121.         <dependency>  
  122.             <groupId>org.springframeworkgroupId>  
  123.             <artifactId>spring-webmvcartifactId>  
  124.             <version>${springversion}version>  
  125.             <type>jartype>  
  126.             <scope>compilescope>  
  127.         dependency>  
  128.         <dependency>  
  129.             <groupId>org.springframeworkgroupId>  
  130.             <artifactId>spring-testartifactId>  
  131.             <version>${springversion}version>  
  132.             <type>jartype>  
  133.             <scope>compilescope>  
  134.         dependency>  
  135.   
  136.         <dependency>  
  137.             <groupId>javax.servletgroupId>  
  138.             <artifactId>jstlartifactId>  
  139.             <version>1.2version>  
  140.             <type>jartype>  
  141.             <scope>compilescope>  
  142.         dependency>  
  143.   
  144.         <dependency>  
  145.             <groupId>commons-collectionsgroupId>  
  146.             <artifactId>commons-collectionsartifactId>  
  147.             <version>3.1version>  
  148.         dependency>  
  149.   
  150.         <dependency>  
  151.             <groupId>commons-logginggroupId>  
  152.             <artifactId>commons-loggingartifactId>  
  153.             <version>1.1version>  
  154.         dependency>  
  155.     dependencies>  
  156.   
  157.     <build>  
  158.         <finalName>tan-springmvc-bookfinalName>  
  159.     build>  
  160. project>  

3、applicationContext.xml配置



[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.   
  19.     <mvc:annotation-driven />  
  20.     <context:component-scan base-package="com.tan.*" />  
  21.   
  22.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  23.         <property name="prefix" value="/" />  
  24.         <property name="suffix" value=".jsp" />  
  25.     bean>  
  26.   
  27. beans>  

4、web.xml配置



[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.jspwelcome-file>  
  9.     welcome-file-list>  
  10.       
  11.           
  12.     <listener>  
  13.         <listener-class>org.springframework.web.util.Log4jConfigListenerlistener-class>  
  14.     listener>  
  15.       
  16.       
  17.     <servlet>  
  18.         <servlet-name>bookservlet-name>  
  19.         <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>  
  20.         <init-param>  
  21.             <param-name>contextConfigLocationparam-name>  
  22.             <param-value>/WEB-INF/applicationContext.xmlparam-value>  
  23.         init-param>  
  24.         <load-on-startup>1load-on-startup>  
  25.     servlet>  
  26.       
  27.     <listener>  
  28.         <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>  
  29.     listener>  
  30.     <servlet-mapping>  
  31.         <servlet-name>bookservlet-name>  
  32.         <url-pattern>/url-pattern>  
  33.     servlet-mapping>  
  34.   
  35. web-app>  

5、model:book.java



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

6、Dao:bookDao.java



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

7、Service:BookService.java



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

8、controller:BookController.java



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

9、index.jsp



[html] view plaincopy
  1. <html>  
  2. <body>  
  3. <h2>Add Bookh2>  
  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>  
阅读(1882) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~