springmvc and maven
版权声明:本文为博主原创文章,未经博主允许不得转载。
1、首先看一下项目结构:
总结:主要是将配置文件配置好之后就不会有什么问题了。在阅读《Maven实战》这本书的时候可以知道有一章是讲解关于继承和聚合的知识,这里主要的是Maven构建SpringMVC项目,所以DAO等这些都写到一起了。因为我也没有只用Maven进行过一个完整项目的实践,所以独立模块开发+聚合可以参考《Maven实战》这本书上面的示例讲解。作为初学者,有很多的东西要学。
附源代码地址:点击打开链接
下面是配置文件及代码:
2、pom.xml
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>MavenTest</groupId>
- <artifactId>tan.maven.springmvc</artifactId>
- <packaging>war</packaging>
- <version>0.0.1-SNAPSHOT</version>
- <name>tan.maven.springmvc Maven Webapp</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <springversion>3.1.1.RELEASE</springversion>
- <junitversion>3.8.1</junitversion>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>${junitversion}</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- <version>${springversion}</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-asm</artifactId>
- <version>${springversion}</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aspects</artifactId>
- <version>${springversion}</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-beans</artifactId>
- <version>${springversion}</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${springversion}</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>${springversion}</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${springversion}</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-expression</artifactId>
- <version>${springversion}</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>${springversion}</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jms</artifactId>
- <version>${springversion}</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-orm</artifactId>
- <version>${springversion}</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-oxm</artifactId>
- <version>${springversion}</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>${springversion}</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>${springversion}</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${springversion}</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>${springversion}</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jstl</artifactId>
- <version>1.2</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>commons-collections</groupId>
- <artifactId>commons-collections</artifactId>
- <version>3.1</version>
- </dependency>
- <dependency>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- <version>1.1</version>
- </dependency>
- </dependencies>
- <build>
- <finalName>tan-springmvc-book</finalName>
- </build>
- </project>
3、applicationContext.xml配置
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
- <mvc:annotation-driven />
- <context:component-scan base-package="com.tan.*" />
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/" />
- <property name="suffix" value=".jsp" />
- </bean>
- </beans>
4、web.xml配置
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
- <display-name></display-name>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <!-- Spring的log4j监听器 -->
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
- <!-- 核心控制器 -->
- <servlet>
- <servlet-name>book</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <servlet-mapping>
- <servlet-name>book</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </web-app>
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
- <html>
- <body>
- <h2>Add Book</h2>
- <form method="post" action="<%=request.getContextPath() %>/book.do?method=add">
- bookname:<input type="text" name="name" id="name">
- author:<input type="text" name="author" id="author">
- <input type="submit" value="Add">
- </form>
- </body>
- </html>
springmvc and maven的更多相关文章
- SSM Spring+SpringMVC+mybatis+maven+mysql环境搭建
SSM Spring+SpringMVC+mybatis+maven环境搭建 1.首先右键点击项目区空白处,选择new->other..在弹出框中输入maven,选择Maven Project. ...
- Spring+SpringMVC+MyBatis+Maven框架整合
本文记录了Spring+SpringMVC+MyBatis+Maven框架整合的记录,主要记录以下几点 一.Maven需要引入的jar包 二.Spring与SpringMVC的配置分离 三.Sprin ...
- CXF WebService整合SpringMVC的maven项目
首先推荐博客:http://www.cnblogs.com/xdp-gacl/p/4259481.html http://blog.csdn.net/hu_shengyang/article/de ...
- Spring+SpringMVC+MyBatis+Maven 服务端XML配置
项目目录结构 spring-mybatis.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...
- Spring+SpringMVC+Mybatis+MAVEN+Eclipse+项目完整环境搭建
1.新建一个Maven项目,创建父项目. 2.创建子项目模块 3.创建javaWeb项目 4.创建后的项目目录结构 5.Maven文件配置 parent父项目pom.xml文件配置 <?xml ...
- springmvc+mongodb+maven 项目搭建配置
操作步骤我就不再细化了 项目能运行,测试过了,先上配置,另一篇文章上代码,点击下载源码 项目结构 pom.xml <project xmlns="http://maven.apache ...
- mybatis分页+springmvc+jsp+maven使用步骤
作者注:本文主要用于个人学习.复习.同时欢迎指导讨论 1,添加maven依赖<dependency> <groupId>com.github.miemiedev ...
- sts 创建springMVC项目---- maven和tomcat 错误处理
今天学习spring的时候,学到了springMVC, 因为springMVC 就是beginning spring 书籍的第三章,为了更深入或更简单的起步学习springMVC, 我又找了另外一本书 ...
- spring springmvc mybatis maven 项目整合示例-导航页面
spring原理 实践解析-简单的helloworld spring原理案例-基本项目搭建 01 spring framework 下载 官网下载spring jar包 spring原理案例-基本项目 ...
- IntelliJ IDEA 创建Spring+SpringMVC+hibernate+maven项目
第一步: 新建maven管理的web项目, 具体步骤参考:http://www.cnblogs.com/gczmn/p/8693734.html 第二步: 创建项目结构, 完整项目结构如下: 第三步: ...
随机推荐
- DotNetCasClient加载失败问题分析
最近公司在接入整理单点登录方案的时候,选择了CAS方案,实际版本采用了4.0.当我们把服务端附属完毕,基于.NET平台Web版的客户端DotNetCasClient进行定制化修改后,在测试环境通过.然 ...
- HTML基础练习
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- pgsql 远程机器无法连接数据库报错处理方法
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本地localhost访问正常,在相同网段的远程机器访问报如下错误 “server closed the connection unexpe ...
- create-react-app 引入ant design 及 使用 less
全局引入: 第一步:全局安装 create-react-app npm install create-react-app -g 第二步:安装 yarn npm install -g yarn 第三步: ...
- 【PostgreSQL-9.6.3】函数(1)--数值型函数
函数表示对输入参数返回一个特殊计算结果的值.PostgreSQL中的函数种类比较丰富,主要分为以下几类:数值型函数.字符型函数.日期和时间函数.条件判断函数.系统函数.加密函数以及其他函数.这篇文章只 ...
- 【Linux】七种文件类型
Linux中的七种文件类型 d 目录文件. l 符号链接(指向另一个文件). s 套接字文件. b 块设备文件,二进制文件. c 字符设备文件. p 命名管道文件. - 普通文件
- 【sqli-labs】 less36 GET- Bypass MYSQL_real_escape_string (GET型绕过MYSQL_real_escape_string的注入)
看一下mysql_real_escape_string()函数 \x00 \x1a 注入的关键还是在于闭合引号,同样使用宽字节注入 http://192.168.136.128/sqli-labs-m ...
- React Native - 使用Geolocation进行定位(获取当前位置、监听位置变化)
1,getCurrentPosition()方法介绍 static getCurrentPosition(geo_success, geo_error?, geo_options? 该方法用于获取当前 ...
- python基础1 格式化输出
转载自:https://www.cnblogs.com/fat39/p/7159881.html %用法 1.整数输出 %o —— oct 八进制%d —— dec 十进制%x —— hex 十六进制 ...
- appium滑动
在app应用日常使用过程中,会经常用到在屏幕滑动操作.如刷朋友圈上下滑操作.浏览图片左右滑动操作等.在自动化脚本该如何实现这些操作呢? 在Appium中模拟用户滑动操作需要使用swipe方法,该方法定 ...