本篇文章来介绍如何在Eclipse中使用Maven构建SpringMVC项目。
点击Eclipse菜单File->New->Other->Maven->Maven Project
在选择maven-archetype的界面选择maven-archetype-webapp:
点击Next弹出填写Maven坐标信息的窗口
在弹出的窗口填写好相应的groupId,artifactId,version等信息,点击Finish完成。
进入项目界面后,这个项目结构还是不完整的,我们需要在Java Resources下手动添加三个源文件夹:src/main/Java(用来存放核心源码)、src/test/java(用来存放测试代码:测试单元)、src/test/resources(用来存放测试代码的配置文件)。其中已经有的src/main/resources用来项目的配置文件。(如果在Project Explorer视图下无法创建,那么转到Navigator视图下创建)
构建好的Maven web项目结构如上图所示。
右击项目,点击Properties->Project Facets
如图所示可以看到项目为web2.3,java1.5,我们可以改成我们所需要的版本。
也可以在Navigator视图下打开配置文件.settings/org.eclipse.wst.common.project.facet.core.xml修改:
修改发布时的部署文件,将test类型的文件remove掉:
maven已经准备完毕,下面开始搭建SpringMVC项目。
首先在pom.xml中添加SpringMVC必须的一些依赖:
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
完整的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.0modelVersion>
<groupId>com.test.springactiongroupId>
<artifactId>springactionartifactId>
<packaging>warpackaging>
<version>0.0.1-SNAPSHOTversion>
<name>springaction Maven Webappname>
<url>http://maven.apache.orgurl>
<properties>
<spring.version>4.1.4.RELEASEspring.version>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>3.8.1version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
<scope>providedscope>
dependency>
dependencies>
<build>
<finalName>springmvcfinalName>
build>
project>
下面重点来了!!!
配置web.xml,让这个项目具有SpringMVC的特性,主要配置两处,一处是ContextLoaderListener,一处是DispatcherServlet:
<web-app 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"
version="3.0">
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath*:applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>/WEB-INF/spring-mvc.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
web-app>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
beans>
属性为该DispatcherServlet指定了配置文件,该项目启动时会在/WEB-INF目录下加载该配置文件,所以需要在WEB-INF下创建spring-mvc.xml:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.my" />
beans>
中我配置的
为全路径”/”。到这整个SpringMVC项目已经搭建完毕!
下面使用一个小demo测试SpringMVC是否搭建成功:这个demo功能是模拟登录,如果登录成功,跳转到成功界面;如果账号密码出错,那么在登录页面出现提示信息。
1.首先在WEB-INF的jsp文件夹下创建两个jsp文件:login.jsp和success.jsp
login.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录title>
head>
<body>
<form action="login" method="post">
用户名:<input type="text" name="username"><br/>
密 码:<input type="password" name="password"><br/>
<input type="submit" value="登录">
form>
<font color="red">${error}font>
body>
html>
success.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录成功title>
head>
<body>
<h2>登录成功!h2>
body>
html>
2.在src/main/java下分别创建两个类用来执行业务逻辑:LoginController.java和LoginService.java
LoginController.java:
@Controller
public class LoginController {
@Autowired
LoginService service;
@Autowired
HttpServletRequest request;
@RequestMapping("login")
public ModelAndView doLogin() {
String loginUrl = "WEB-INF/jsp/login.jsp";
String successUrl = "WEB-INF/jsp/success.jsp";
String username = request.getParameter("username");
String password = request.getParameter("password");
return service.doLogin(loginUrl, successUrl, username, password);
}
}
LoginService.java:
Service
public class LoginService {
public ModelAndView doLogin(String loginUrl, String successUrl, String username, String password) {
if (username == null||"".equals(username)) {
return new ModelAndView(loginUrl,"error","用户名不能为空!");
}
if (password == null||"".equals(username)) {
return new ModelAndView(loginUrl,"error","密码不能为空!");
}
if (username.equals("admin")&&password.equals("password")) {
return new ModelAndView(successUrl);
}
return new ModelAndView(loginUrl,"error","用户名或密码错误!");
}
}
在浏览器中输入地址http://localhost:8080/springmvc/login进行测试:
如果账号或密码输入错误,则出现错误提示 :
项目可以正常运行,说明我们的项目搭建成功!