引言

最近需要使用SpringMVC做一个小项目,之前对SpringMVC没什么了解,所以先写一个SpringMVC的小Demo练习一下。

使用Maven构建项目

IDE = Eclipse

首先创建一个Maven的webapp项目,Jar包的管理就交给Maven来完成;POM内容如下:

<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>com.tuo.example</groupId>
<artifactId>SpringMVC</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringMVC Maven Webapp</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springversion>4.1.2.RELEASE</springversion>
<junitversion>3.8.1</junitversion>
</properties> <dependencies> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- Spring -->
<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-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> <!-- servlet & jstl-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency> <!-- common -->
<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>SpringMVC</finalName>
</build>
</project>

修改Web.xml配置

web.xml作为前端的配置文件,需要在其中指定以SpringMVC方式处理HTTP请求,即配置servlet标签为SpringMVC,并且指明使用哪个文件作为SpringMVC的配置文件。

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringMVC01</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list> <!-- configure springmvc dispatcher for Servlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

SpringMVC 配置文件

如上面web.xml中所示,我这里使用spring-mvc.xml最为SpringMVC的配置文件,并将其放在Maven项目默认的resoucres目录下:

其内容为:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 使用注解的包,包括子集 -->
<context:component-scan base-package="com.example.tuo"/> <!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"></property>
</bean> </beans>

到此,SpringMVC的配置部分就完成了;下面需要写 controller 类 以及 HTML和JSP 页面。

实现HTML引导页面

基于我们在web.xml里面里面关于 “servlet-mapping” 的定义,我们需要截获的HTTP request 为 *.do(例如:http://localhost:8080/SpringMVC/HelloSpringMVC.do)

在这里,我们不直接输入xxx.do,通过web.xml里面定义的index.html中的超链接跳转到目标地址(HelloSpringMVC.do)。HTML如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SpringMVC start page</title>
</head>
<body>
<a href="sayHello.do">Hello springMVC!</a>
</body>
</html>

实现controller类

讲 controller 类放在controller包下,其内容很简单,就是利用MVC中的Model返回一个message到JSP,代码如下

package com.example.tuo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class HelloSpringMVC { @RequestMapping("sayHello")
public void sayHello(Model model){
model.addAttribute("message", "Hello SpringMVC, I am coming!");
}
}

实现 JSP 页面

还是基于我们在web.xml里面的视图解析器定义:

<!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"></property>
</bean>

我们在项目里面建立一个sayHello.jsp, 请注意这里的前后缀以及在controller类里面定义的@RequestMapping的内容, 把它们连接在一起应指向了JSP页面:prefix+RequestMapping("sayHello")+suffix = /WEB-INF/jsp/sayHello.jsp, 如图所示:

JSP页面本身的内容就很简单了,只是简单的输出在controller类里面定义的message:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>SpringMVC page</title>
</head>
<body>
${message}
</body>
</html>

OK,我们的所有编码工作到这里就全部完成。启动Tomcat加载并运行项目,在浏览器中输入导航页面的路径:http://localhost:8080/SpringMVC/后,我们便来到了HTML页面:

在页面上点击超链接,即可跳转到sayHello.jsp;我们可以看到在view中输出了我们在controller里面定义的内容“Hello SpringMVC, I am coming!”;

Well done!我们的SpringMVC项目运行正确啦 :)

使用 maven 构建 SpringMVC的更多相关文章

  1. 使用Eclipse maven构建springmvc项目

    Eclipse maven构建springmvc项目 Listener 监听器 架构 使用Log4J监控系统日志邮件警报 2014-12-16 13:09:16 控制器在完成逻辑处理后,通常会产生一些 ...

  2. Eclipse maven构建springmvc项目

    原文地址: http://www.cnblogs.com/fangjins/archive/2012/05/06/2485459.html 一.背景介绍 对于初学者,用maven构建项目并不是一件容易 ...

  3. maven构建springmvc项目

    1.Eclipse中 NEW ->OTHER->Maven->maven project 2.选择项目路径 3.选择项目类型->next->输入groupid和artif ...

  4. JavaWeb之Eclipse中使用Maven构建SpringMVC项目

    为了学习spring和maven我这也是拼了老命了,光使用maven配置springmvc我花了上周一周的时间,下班回来就搞,一直有bug,一个bug接着一个,昨天一整天都在解决配置的问题,让大学同学 ...

  5. Maven构建SpringMVC+Mybatis项目

    1.创建Maven项目时,起始是没有src/main/java.src/test/java.src/test/resources,需要修改一些配置之后,自动就会创建出来: 2.开始引入Spring+M ...

  6. SpringMVC拓展——利用maven构建springMVC项目

    一.构建项目结构 首先需要构建一个符合目录结构的maven项目 file->new->maven project,勾选 create a simple project->next / ...

  7. 在Eclipse中使用Maven构建SpringMVC项目

    环境搭建 安装JDK, Eclipse, Tomcat等 – 请参考网上常见攻略. 安装Maven: 下载需要的Maven 版本( http://maven.apache.org/download.c ...

  8. Maven构建 SpringMVC+Spring+MyBatis 环境整合

    目录 1. Maven 项目搭建 2. Maven 插件生成 MyBatis 代码 3. 待续 ... 开发环境 开发环境请尽量保持一致,不一致的情况可能存在问题. JDK 1.7 MyEclipse ...

  9. maven构建springmvc过程中遇到的一些问题

    1.spring的配置文件,xsd不要配版本号 2.spring配置datasource的三种方式 3.springmvc与spring扫描注解的冲突 4.NoClassDefFoundError: ...

随机推荐

  1. Windows 64位系统下安装JAVA环境

    文件名称:jdk-7u51-windows-x64 下载Windows 64位平台的JDK安装文件. 安装 1.双击打开下载好的文件,点击“下一步”开始安装. 2. ①默认选择安装全部功能,不做调整. ...

  2. 5天突击GRE(155+170+4.0)

    个人认为最靠谱GRE经验,没有之一 虽然分数并不高(V 155 + Q 170 + AW 4.0),但是自认为有很多很多可以拿来给短期突击同学的宝贵经验. 首先是自己的背景,交大英语教改实验班(交大同 ...

  3. Apache配置php redis扩展

    1.根据phpinfo,下载redis 下载地址:注意php版本号 http://windows.php.net/downloads/pecl/snaps/redis/2.2.5/ http://wi ...

  4. Photon——Feature Overview 功能概述

    Photon——Feature Overview 功能概述   Feature Overview 功能概述        Photon is a real-time socket server and ...

  5. 人生苦短之我用Python篇(列表list、字典dict、元组tuple、字符串str)

    列表 创建列表 sample_list = ['a',1,('a','b')] Python 列表操作 sample_list = ['a','b',0,1,3] 得到列表中的某一个值 value_s ...

  6. windows 10 安装 sql 2005 安装失败

    windows 10 安装 sql 2005 安装失败 网上的方法记录: 安装中无法启动需要先用sp4的补丁文件sqlos.dll,sqlservr.exe 替换D:\Program Files (x ...

  7. [转载][QT][SQL]sql学习记录3_sqlite之update delete like

    转载自:详见 : http://www.runoob.com/sqlite/sqlite-create-table.html 约束表实例 下面是一个实例,它创建了一个 COMPANY 表,ID 作为主 ...

  8. java 实现共享锁和排它锁

    一直对多线程有恐惧,在实现共享锁和排它锁之后,感觉好了很多. 共享锁    就是查询的时候,如果没有修改,可以支持多线程查询: 排它锁    就是修改的时候,锁定共享锁,停止查询,同时,锁定排它锁,只 ...

  9. 新Eclipse安装与配置 【来源网络根据实际情况自己补充】

    [第一次更新:20161108:http://blog.csdn.net/vvanity/article/details/51036678] Eclipse的官网地址:http://www.eclip ...

  10. 命令“"xxx.exe" xxx”已退出,代码为 n。这些错误是什么意思?(3 / 123 / 9009)

    命令“”xxx.exe” xxx”已退出,代码为 n. 我们在 cmd 中输入常见命令的时候,如果命令输入错误或使用错误,cmd 中会提示错误原因,帮助我们定位并解决问题.然而如果相同的命令放到了 V ...