Maven新建一个Spring MVC项目
新建一个Maven项目,选择archetypes为maven-archetype-webapp,
相关的名称按个人习惯取,我这里取
Group Id:moonlit-group
Artifact Id:moonlit-artifact
然后一个新的maven webapp项目就见成了,你可以在Eclipse左侧的Project Explorer中看到一个项目名为moonlit-artifact,这个即是我们新间的项目的名称。
新建完项目之后会发现项目有个红叉,一直追溯过去会发现是webapp目录下的index.jsp下面有一个红叉,解决办法是:把tomcat的运行时环境添加进来。Project --> Properties --> Libraries --> add Library --> Server Runtime,把对应的tomcat添加进来就可以了。
此时虽然红叉没有了,但是感叹号还存在。这是因为项目引用的Jre版本和本季上装载的Java版本不一致造成的。
修改方式如下:
Project-->Properties-->Java Build Path-->Libraries->JRE System Library的版本改成本机搭载的Java版本。(这一步我没有改动)
把Project Facets中Java的版本改成本机Java的版本(我就该了这里感叹号就没有了)。
在我写这篇博客的时候,spring相关的最新版本是4.3.0.RELEASE,junit的最新版本是4.1.2,我们这里以最新版为例。
首先在pom.xml目录下添加一些相关的dependency,然后右键运行maven install,第一次可能需要一点时间下载dependencies里面的jar包。
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>moonlit-group</groupId>
<artifactId>moonlit-artifact</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>moonlit-artifact Maven Webapp</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springversion>4.3.0.RELEASE</springversion>
<junitversion>4.12</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>3.1.4.RELEASE</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>moonlit-artifact</finalName>
</build>
</project>
applicationContext.xml(放在WEB-INF/目录下):
<?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.moonlit.*" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean> </beans>
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>
model:book.java
package com.moonlit.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;
}
}
Dao:bookDao.java
package com.moonlit.dao; import org.springframework.stereotype.Component; import com.moonlit.model.Book; @Component
public class BookDao { //模拟数据库操作
public void add(Book book){
System.out.print("Add");
}
public void update(Book book){
System.out.print("Update");
}
}
Service:BookService.java
package com.moonlit.service; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.moonlit.dao.BookDao;
import com.moonlit.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);
} }
controller:BookController.java
package com.moonlit.controller; import javax.annotation.Resource; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import com.moonlit.model.Book;
import com.moonlit.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;
} }
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>
运行后可在Console中查看效果,比如,当我填写书名为harry potter,作者为moonlit之后,Console中会显示:
bookname:harry potter
author:moonlit
Add
Maven新建一个Spring MVC项目的更多相关文章
- 使用IntelliJ IDEA新建一个spring boot项目
好家伙, 使用IntelliJ IDEA新建一个spring boot项目 目的很简单,就是网页上出现一个"hello world" 别的暂时不管 首先关于工具IntelliJ I ...
- 第一个使用Spring Tool Suite(STS)和Maven建立的Spring mvc 项目
一.目标 在这篇文章中.我将要向您展示怎样使用Spring Frameworks 和 Maven build创建您的第一个J2ee 应用程序. 二.信息 Maven是一个java项目的构建工具(或者自 ...
- 在Mac系统下用STS搭建一个Spring MVC项目
[本文出自天外归云的博客园] 从STS的下载到空项目的搭建 1. 下载STS,下载解压缩后点击sts-bundle文件夹中的STS文件启动ide: 2. 创建Spring MVC项目:File-> ...
- 使用Maven创建一个Spring MVC Web 项目
使用Maven创建java web 项目(Spring MVC)用到如下工具: 1.Maven 3.2 2.IntelliJ IDEA 13 3.JDK 1.7 4.Spring 4.1.1 rele ...
- eclipse 使用maven 创建纯spring mvc项目
接着eclipse 使用maven 创建web3.1项目 创建完成后, 讲spring mvc加入到项目中 先修改pom.xml文件 注意红色字部分 <project xmlns="h ...
- IDEA新建一个Spring Boot项目
Maven构建项目模板 maven构建的是maven风格的纯净模板,要转变成spring boot项目需要自己添加依赖等配置. mvn archetype:generate: Maven插件原型是一个 ...
- 利用maven构建一个spring mvc的helloworld实例
刚开始学习maven和spring mvc,学的云里雾里的 这里提供一个hello world实例,记录自己的学习之路 首先看maven官网的介绍 Apache Maven is a software ...
- 用intellj 建一个spring mvc 项目DEMO
spring的起初可能经常碰壁,因为网上的资料都是混乱的xml堆成的,混乱难以理解,我这个也是,阿哈哈哈哈! 新建一个Maven->create from archetype->org.j ...
- 【spring boot】【idea】100.idea新建一个spring boot项目
1.idea新创建一个项目 2.setting进入,选择自己的Maven 3.简单补充一下pom.xml <?xml version="1.0" encoding=" ...
随机推荐
- Android画图之抗锯齿
在画图的时候,图片如果旋转或缩放之后,总是会出现那些华丽的锯齿.其实Android自带了解决方式. 方法一:给Paint加上抗锯齿标志.然后将Paint对象作为参数传给canvas的绘制方法. ...
- 转:Mosquitto用户认证配置
转自:https://blog.csdn.net/u012377333/article/details/69397124?utm_source=blogxgwz1 前言:基于Mosquitto服务器已 ...
- C#中Out和Ref參数修饰符
在编程过程中对于函数之间的參数的传递一般分为两种:传值和传地址. 以下为大家分析一下. 传值 比方你又一份文档,假设採用传值的话.相当于我复制了一份,因此我对我这份文档的改动都不会影响到你的那份.假设 ...
- [CentOS] CentOS for vsftpd with MySQL Virtual user
從ubuntu 12.04的安裝手法拿到CentOS來真的有些很大的不同 絕大部分的語法.概念都是差不多的,只是指令上有些差別,跟ubuntu 有不一樣的地方特別拿出來另外說明 要讓vsftpd與my ...
- Spring Cloud(七):使用SVN存储分布式配置中心文件和实现refresh
国内很多公司都使用的svn来做代码的版本控制,我们先介绍以下如何使用svn+Spring Cloud Config来做配置中心. svn版本 同样先示例server端的代码,基本步骤一样. 1.添加依 ...
- atitit。win7 win8 win9 win10 win11 新特性总结与战略规划
atitit.win7 win8 win9 win10 win11 新特性总结与战略规划 1. win7 1 1.1. 发布时间 2009年10月22日 1 1.2. 稳定性大幅提升,很少蓝屏死机 ...
- atitit.新增编辑功能 跟orm的实现 attilax p31
atitit.新增编辑功能 跟orm的实现 attilax p31 1. 流程的实现 1 2. view的实现(dwr) 1 3. 获取表结构 1 4. grep filt req params 2 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- iPhone应用程序的启动过程
Phone的入口函数main,这之后它有是怎样启动应用程序,初始化的呢,这些都是通过 UIApplicationMain 来实现的. 其启动的流程图大致如下图所示: 1 int retVal = UI ...
- Win7-U盘安装出现"We were unable to copy your files. "
使用Windows 7 USB/DVD Download Tool时,提示We were unable to copy your files. Please check your USB device ...