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

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

使用Maven构建Spring MVC项目的简单示例_第1张图片

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

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

下面是配置文件及代码:

2、pom.xml


	4.0.0
	MavenTest
	tan.maven.springmvc
	war
	0.0.1-SNAPSHOT
	tan.maven.springmvc Maven Webapp
	http://maven.apache.org
	
		UTF-8
		3.1.1.RELEASE
		3.8.1
	

	
		
			junit
			junit
			${junitversion}
			test
		
		
			org.springframework
			spring-aop
			${springversion}
			jar
			compile
		
		
			org.springframework
			spring-asm
			${springversion}
			jar
			compile
		
		
			org.springframework
			spring-aspects
			${springversion}
			jar
			compile
		
		
			org.springframework
			spring-beans
			${springversion}
			jar
			compile
		
		
			org.springframework
			spring-context
			${springversion}
			jar
			compile
		
		
			org.springframework
			spring-context-support
			${springversion}
			jar
			compile
		
		
			org.springframework
			spring-core
			${springversion}
			jar
			compile
		
		
			org.springframework
			spring-expression
			${springversion}
			jar
			compile
		
		
			org.springframework
			spring-jdbc
			${springversion}
			jar
			compile
		
		
			org.springframework
			spring-jms
			${springversion}
			jar
			compile
		
		
			org.springframework
			spring-orm
			${springversion}
			jar
			compile
		
		
			org.springframework
			spring-oxm
			${springversion}
			jar
			compile
		
		
			org.springframework
			spring-tx
			${springversion}
			jar
			compile
		
		
			org.springframework
			spring-web
			${springversion}
			jar
			compile
		
		
			org.springframework
			spring-webmvc
			${springversion}
			jar
			compile
		
		
			org.springframework
			spring-test
			${springversion}
			jar
			compile
		

		
			javax.servlet
			jstl
			1.2
			jar
			compile
		

		
			commons-collections
			commons-collections
			3.1
		

		
			commons-logging
			commons-logging
			1.1
		
	

	
		tan-springmvc-book
	


3、applicationContext.xml配置




	
	

	
		
		
	


4、web.xml配置



	
	
		index.jsp
	
	
		
	
		org.springframework.web.util.Log4jConfigListener
	
	
	
	
		book
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			/WEB-INF/applicationContext.xml
		
		1
	
	
	
		org.springframework.web.context.ContextLoaderListener
	
	
		book
		/
	



5、model:book.java

package com.tan.model;

public class Book {
	private int id;
	private String name;
	private String author;
	public Book(){}
	public Book(int id, String name, String author) {
		super();
		this.id = id;
		this.name = name;
		this.author = author;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	

}

6、Dao:bookDao.java

package com.tan.dao;

import org.springframework.stereotype.Component;

import com.tan.model.Book;
@Component
public class BookDao {

	//模拟数据库操作
	public void add(Book book){
		System.out.print("Add");
	}
	public void update(Book book){
		System.out.print("Update");
	}
}

7、Service:BookService.java

package com.tan.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.tan.dao.BookDao;
import com.tan.model.Book;

@Component
public class BookService {

	private BookDao bookDao;

	public BookDao getBookDao() {
		return bookDao;
	}

	@Resource
	public void setBookDao(BookDao bookDao) {
		this.bookDao = bookDao;
	}
	
	public void add(Book book){
		bookDao.add(book);
	}
	public void update(Book book){
		bookDao.update(book);
	}
	
}

8、controller:BookController.java

package com.tan.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tan.model.Book;
import com.tan.service.BookService;

@Controller
@RequestMapping("/book.do")
public class BookController {

	private BookService bookService;
	@RequestMapping(params = "method=add")
	public String add(Book book){
		System.out.println("bookname:"+book.getName());
		System.out.println("author:"+book.getAuthor());
		bookService.add(book);
		return "success";
	}
	@RequestMapping(params = "method=update")
	public String update(Book book) {
		bookService.update(book);
		return "success";
	}
	public BookService getBookService() {
		return bookService;
	}
	@Resource
	public void setBookService(BookService bookService) {
		this.bookService = bookService;
	}
	
}

9、index.jsp



Add Book

bookname: author:


你可能感兴趣的:(使用Maven构建Spring MVC项目的简单示例)