• springmvc and maven


     

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

    标签: mavenspringmvcspring
     分类:
     

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

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

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

    下面是配置文件及代码:

    2、pom.xml

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

    3、applicationContext.xml配置
    [html] view plain copy
     
    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 plain copy
     
    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.       
    11.         <!-- Spring的log4j监听器 -->  
    12.     <listener>  
    13.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
    14.     </listener>  
    15.       
    16.     <!-- 核心控制器 -->  
    17.     <servlet>  
    18.         <servlet-name>book</servlet-name>  
    19.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    20.         <init-param>  
    21.             <param-name>contextConfigLocation</param-name>  
    22.             <param-value>/WEB-INF/applicationContext.xml</param-value>  
    23.         </init-param>  
    24.         <load-on-startup>1</load-on-startup>  
    25.     </servlet>  
    26.       
    27.     <listener>  
    28.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    29.     </listener>  
    30.     <servlet-mapping>  
    31.         <servlet-name>book</servlet-name>  
    32.         <url-pattern>/</url-pattern>  
    33.     </servlet-mapping>  
    34.   
    35. </web-app>  

    5、model:book.java
    [java] view plain copy
     
    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 plain copy
     
    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 plain copy
     
    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 plain copy
     
    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 plain copy
     
    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>  
  • 相关阅读:
    linux实践之ELF文件分析
    《linux内核设计与实现》实践之模块及深入
    《linux内核设计与实现》读书笔记第十七章
    读书笔记——《沉思录》(4/4)
    读书笔记——《沉思录》(3/4)
    《Linux内核分析》期末总结
    20135337——linux第四次实践:字符集总结与分析
    20135337——Linux实践三:程序破解
    20135337——Linux实践三:ELF文件格式(64位系统,简单分析)
    20135337——linux实践三:ELF文件格式分析(32位系统)
  • 原文地址:https://www.cnblogs.com/developer-ios/p/5346515.html
Copyright © 2020-2023  润新知