文章目录
Spring MVC简介
- Spring MVC框架是一个开源的Java平台,基于Java的Web应用程序提供全面的基础架构支持
- Spring web MVC框架提供了MVC(模型 - 视图 - 控制器)架构和用于开发灵活和松散耦合的Web应用程序的组件。
- MVC模式导致应用程序的不同方面(输入逻辑,业务逻辑和UI逻辑)分离,同时提供这些元素之间的松散耦合。
Spring MVC重要概念
- 模型(Model)封装了应用程序数据,通常它们将由POJO类组成
- 视图(View)负责渲染模型数据,一般来说它生成客户端浏览器可以解释HTML输出。
- 控制器(Controller)负责处理用户请求并构建适当的模型,并将其传递给视图进行渲染
Spring MVC核心
- Spring MVC核心的核心是
DispatcherServlet,DispatcherServlet处理所有的HTTP请求和响应 - Spring Web MVC DispatcherServlet的请求处理工作流如下图所示

Spring Web MVC DispatcherServlet的传入HTTP请求的事件顺序
- 在接收到HTTP请求后,DispatcherServlet会查询HandlerMapping以调用相应的Controller。
- Controller接受请求并根据使用的GET或POST方法调用相应的服务方法,服务方法将基于定义的业务逻辑设置模型数据,并将视图名称返回给DispatcherServlet。
- DispatcherServlet将从ViewResolver获取请求的定义视图。
- 当视图完成,DispatcherServlet将模型数据传递到最终的视图,并在浏览器上呈现。
- 所有上述组件,即: HandlerMapping,Controller和ViewResolver是WebApplicationContext的一部分
- WebApplicationContext是ApplicationContext的扩展,带有Web应用程序所需要的一些额外功能
Spring MVC - Hello World示例
- 使用Spring MVC框架编写一个简单的基于Web的Hello World应用程序
- 第一步:创建一个名为HelloWeb的动态Web项目,并在创建的项目中的src文件夹下创建一个包com.yiibai.springmvc。
- 将下面提到的Spring和其他库拖放到文件夹WebContent/WEB-INF/lib中
- 在com.yiibai.springmvc包下创建一个Java类HelloController
- 在WebContent/WEB-INF文件夹下创建Spring配置文件web.xml和HelloWeb-servlet.xml
- 在WebContent/WEB-INF文件夹下创建一个名为jsp的子文件夹。在此子文件夹下创建视图文件hello.jsp
- 是创建所有源和配置文件的内容并导出应用程序或直接在Eclipse中运行
HelloController.java
package com.yiibai.springmvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
@Controller
@RequestMapping("/hello")
public class HelloController{
@RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
HelloWeb-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.yiibai" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
hello.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>Hello, ${message}</h2>
</body>
</html>
SpringMVC静态页面
- 使用Spring MVC Framework编写一个简单的基于Web的应用程序
- 可以使用
<mvc:resources>标记访问静态页面和动态页面。 - 首先使用Eclipse IDE创建一个动态WEB项目并使用Spring Web Framework开发基于动态表单的Web应用程序
- 第一步:创建一个简单的动态Web项目:StaticPages,并在 src 目录下创建一个 com.yiibai.springmvc 包。
- 第二步:在com.yiibai.springmvc包下创建一个Java类WebController
- 第三步:在jsp子文件夹下创建一个静态文件final.html
- 在WebContent/WEB-INF文件夹下创建一个Spring配置文件 StaticPages-servlet.xml,
WebController.java
package com.yiibai.springmvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class WebController {
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index() {
return "index";
}
@RequestMapping(value = "/staticPage", method = RequestMethod.GET)
public String redirect() {
return "redirect:/pages/final.html";
}
}
StaticPages-servlet.xml
<?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:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="springmvc"/>
<mvc:resources mapping="/jsp/**" location="/WEB-INF/jsp/"/>
<mvc:annotation-driven/>
</beans>
使用<mvc:resources ..../>标记来映射静态页面- 映射属性必须是指定http请求的URL模式的Ant模式
- location属性必须指定一个或多个有效的资源目录位置,其中包含静态页面,包括图片,样式表,JavaScript和其他静态内容。
- 可以使用逗号分隔的值列表指定多个资源位置。
WEB-INF/jsp/index.jsp 是一个登录页面,将会发送一个请求访问staticPage服务方法,该方法将此请求重定向到WEB-INF/pages文件夹中的静态页面。
index.jsp 页面的代码如下
<%@ page contentType="text/html; charset=UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring Landing Page</title>
</head>
<body>
<h2>Spring Landing Page</h2>
<p>点击下面的按钮获得一个简单的HTML页面</p>
<form:form method="GET" action="/StaticPages/staticPage">
<table>
<tr>
<td>
<input type="submit" value="获取HTML页面"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
final.html 的完整代码如下 -
<html>
<head>
<title>Spring Static Page</title>
</head>
<body>
<h2>A simple HTML page</h2>
</body>
</html>
- 导出应用程序。右键单击应用程序,并使用导出> WAR文件选项,并将文件保存为HelloWeb.war 在Tomcat的webapps文件夹中。
- 单击“获取HTML页面”按钮访问staticPage服务方法中提到的静态页面。
Spring MVC页面重定向
- 创建一个名称为 PageRedirection 项目。
- 在 com.yiibai.springmvc 包下创建一个Java类WebController。
- 在jsp子文件夹下创建一个视图文件index.jsp,final.jsp。
- 创建所有源和配置文件的内容并导出应用程序
WebController.java
package com.yiibai.springmvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class WebController {
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index() {
return "index";
}
@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public String redirect() {
return "redirect:finalPage";
}
@RequestMapping(value = "/finalPage", method = RequestMethod.GET)
public String finalPage() {
return "final";
}
}
Spring视图文件index.jsp的内容
- index.jsp是登录页面,这个页面将发送访问重定向方法的请求,将重定向这个请求到final.jsp页面中去
index.jsp 文件
<%@ page contentType="text/html; charset=UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring MVC页面重定向</title>
</head>
<body>
<h2>Spring MVC页面重定向</h2>
<p>点击下面的按钮将结果重定向到新页面</p>
<form:form method="GET" action="/PageRedirection/redirect">
<table>
<tr>
<td><input type="submit" value="页面重定向" /></td>
</tr>
</table>
</form:form>
</body>
<html>
**final.jsp 文件 **
<%@ page contentType="text/html; charset=UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring重定向页面</title>
</head>
<body>
<h2>重定向页面...</h2>
</body>
</html>
Spring MVC文本框
- 在 com.yiibai.springmvc 包下创建Java类Student,StudentController
- 在jsp子文件夹下创建一个视图文件student.jsp,result.jsp
Student.java
package com.yiibai.springmvc;
public class Student {
private Integer age;
private String name;
private Integer id;
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
}
StudentController.java
package com.yiibai.springmvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.ModelMap;
@Controller
public class StudentController {
@RequestMapping(value = "/student", method = RequestMethod.GET)
public ModelAndView student() {
return new ModelAndView("student", "command", new Student());
}
@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
public String addStudent(@ModelAttribute("SpringWeb")Student student,
ModelMap model) {
model.addAttribute("name", student.getName());
model.addAttribute("age", student.getAge());
model.addAttribute("id", student.getId());
return "result";
}
}
- 第一个服务方法是 student(),在ModelAndView对象中传递了一个名称为“command”的空对象, 因为如果要在JSP中使用form:form标签,spring框架需要一个名称为“command”的对象文件,当调用student()方法时,它返回student.jsp视图。
- 第二个服务方法addStudent()将在URL HelloWeb/addStudent 上的POST方法调用,将根据提交的信息准备模型对象。 最后,将从服务方法返回“result”视图,这将渲染result.jsp 页面。
student.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring MVC表单之-输入框处理</title>
</head>
<body>
<h2>学生信息</h2>
<form:form method="POST" action="/TextBox/addStudent">
<table>
<tr>
<td><form:label path="name">姓名:</form:label></td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td><form:label path="age">年龄:</form:label></td>
<td><form:input path="age" /></td>
</tr>
<tr>
<td><form:label path="id">编号:</form:label></td>
<td><form:input path="id" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交学生信息" /></td>
</tr>
</table>
</form:form>
</body>
</html>
result.jsp-
<%@ page contentType="text/html; charset=UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring MVC表单之-输入框处理</title>
</head>
<body>
<h2>提交的学生信息如下 - </h2>
<table>
<tr>
<td>姓名:</td>
<td>${name}</td>
</tr>
<tr>
<td>年龄:</td>
<td>${age}</td>
</tr>
<tr>
<td>编号:</td>
<td>${id}</td>
</tr>
</table>
</body>
</html>
Spring MVC密码处理
- 创建一个名称为 Password 的动态WEB项目。
- 在 com.yiibai.springmvc 包下创建两个Java类User,UserController。
- 在jsp子文件夹下创建两个视图文件:user.jsp,userlist.jsp
User.java
package com.yiibai.springmvc;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserController.java
package com.yiibai.springmvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.ModelMap;
@Controller
public class UserController {
@RequestMapping(value = "/user", method = RequestMethod.GET)
public ModelAndView user() {
return new ModelAndView("user", "command", new User());
}
@RequestMapping(value = "/addUser", method = RequestMethod.POST)
public String addUser(@ModelAttribute("SpringWeb")User user,
ModelMap model) {
model.addAttribute("username", user.getUsername());
model.addAttribute("password", user.getPassword());
return "userlist";
}
}
- 第一个服务方法user(),在ModelAndView对象中传递了一个名称为“command”的空User对象,因为如果在JSP文件中使用form:form标签,spring框架需要一个名称为“command”的对象。所以当调用user()方法时,它返回user.jsp视图。
- 第二个服务方法addUser()将根据URL => HelloWeb/addUser 上的POST方法请求时调用。根据提交的信息准备模型对象。 最后从服务方法返回“userlist”视图,这将呈现userlist.jsp视图
user.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring MVC表单处理之-密码</title>
</head>
<body>
<h2>用户信息</h2>
<form:form method="POST" action="/Password/addUser">
<table>
<tr>
<td><form:label path="username">用户名:</form:label></td>
<td><form:input path="username" /></td>
</tr>
<tr>
<td><form:label path="password">密码:</form:label></td>
<td><form:password path="password" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
userlist.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring MVC表单处理之-密码</title>
</head>
<body>
<h2>提交的用户信息</h2>
<table>
<tr>
<td>用户名:</td>
<td>${username}</td>
</tr>
<tr>
<td>密码:</td>
<td>${password}</td>
</tr>
</table>
</body>
</html>
版权声明:本文为qq_43408367原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。