数据绑定的本质,是将客户端的数据传递给后台,后台将处理结果传递回去。本文介绍默认数据绑定的实现。
常用的默认参数类型如下:
- HttpServletRequest,通过request对象获取请求信息
- HttpServletResponse,通过response处理响应信息
- HttpSession,通过session对象得到session中存储的对象
- Model/ModelMap,Model是一个接口,ModelMap是一个接口实现,作用是将model数据填充到request域
一、导入Spring MVC所需Jar包,以及Spring AOP包
参考:Spring MVC入门,以及:Spring MVC,基于注解的Spring MVC应用
二、在Web.xml中,配置Spring MVC的前端控制器,包括配置编码过滤器
<?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_3_0.xsd" id="WebApp_ID" version="3.0"> <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:springmvc-config.xml</param-value> </init-param> <!-- 表示容器在启动时立即加载Servlet --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置编码过滤器 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
前段控制器的解读,参考:Spring MVC入门;编码过滤器的作用,是解决前后端数据传递的中文乱码问题,即所有的请求信息内容都会以UTF-8的编码格式进行解析。
三、配置springmvc-config.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 指定需要扫描的包 --> <context:component-scan base-package="com.itheima.controller" /> <!-- 定义视图解析器 --> <bean id="viewResolver" class= "org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 设置前缀 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- 设置后缀 --> <property name="suffix" value=".jsp" /> </bean> </beans>
对于它的解读,参考:Spring MVC,基于注解的Spring MVC应用
四、创建控制器
package com.itheima.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping(value = "/hello") public class DefalutParamController { @RequestMapping("/defaultParameter") public ModelAndView defaultParameter(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, ModelMap modelMap) throws Exception { // 加入读取前台参数的代码 String inputParam = request.getParameter("name"); // 回传参数到前台 request.setAttribute("requestParameter", "您的输入是" + inputParam); session.setAttribute("sessionParameter", "这是session参数"); // ModelMap是Model接口的一个实现类,作用是将Model数据填充到request域 // 即便使用Model接口,其内部绑定还是由ModelMap来实现 model.addAttribute("modelParameter", "这是model参数"); modelMap.addAttribute("modelMapParameter", "这是modelMap参数"); ModelAndView mv = new ModelAndView(); mv.setViewName("success"); return mv; } }
对于它的解读,参考:Spring MVC,基于注解的Spring MVC应用
五、在WEB-INF/jsp目录下,新建success.jsp
内容如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>数据绑定</title> </head> <body> request: ${requestParameter} <br/> session:${sessionParameter} <br/> model:${modelParameter} <br/> modelMap:${modelMapParameter} </body> </html>
六、启动项目,测试
打开网页:http://localhost:8080/ssm/hello/defaultParameter?name=张三