前:
最近在学习springMVC,也搜索了点资料。对其学习并总结,转发来写一篇自己的学习笔记。
参考:
context:component-scan 的使用说明
SpringMVC中的注解
SpringMVC中的参数绑定
SpringMVC入门
一、SpringMVC框架理解
SpringMVC核心架构由前端控制器DispatcherServlet、处理器映射器HandlerMapping、处理器适配器HandlerAdapter、处理器Handler、视图解析器ViewResolver、视图View组成。
处理请求的具体流程:
请求->DispatcherServlet->HandlerMapping->HandlerAdapter->生成ModelAndView->View
先接受到了浏览器请求,再通过前端控制器委托给处理映射器和处理适配器,映射器将请求映射为HandlerExecutionChain对象,适配器进行调用对应的处理方法,返回一个Modelandview对象给前端控制器,前端控制器再通过视图解析器来解析返回View,前端控制器再对View进行渲染显示。在流程中充当全局的流程控制。
二、SpringMVC注解的作用
该文会使用到以下三种注解
@Controller
定义一个控制器
@RequestMapping
URL映射方法
@RequestParam
绑定 HttpServletRequest 请求参数到控制器方法参数
三、参数绑定
1、简单绑定
在通过@RequestMapping注解调用方法时,方法的参数可以与request中的参数绑定,方法形参中的参数名要和前台传进来的名一样才能完成参数的绑定。若想不一样的名字进行绑定参数,则需要使用到@RequestParam注解。
2、pojo类绑定
由简单类型的参数绑定转为类似封装类bean的绑定,要求页面中input标签的name属性值和controller的pojo形参中的属性名称一致,即可将页面中数据绑定到pojo。也就是说前台页面传进来的name要和要封装的pojo属性名一模一样。
两种绑定在实现简单程序中都会出现到。
四、简单的springMVC程序(Hello Spring MVC)
实现前端表单上传到控制器并在另一个页面输出
1、新建project

新建后会自动下载所需要的依赖,需要等一会。
2、配置本地tomcat




3、编写index.jsp和HelloController
前端表单index.jsp
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*" isELIgnored="false"%>
<html>
<head>
<meta charset="UTF-8">
<title>Spring MVC 传参方式</title>
</head>
<body>
<form action="param" role="form" method="post">
用户名:<input type="text" name="userName"><br/>
密码:<input type="text" name="password"><br/>
<input type="submit" value="提 交">
</form>
</body>
</html>
处理与转发HelloController.java
有三种方式接受参数
1)通过servlet原生api,request.geParameter()
@RequestMapping("/param")
public ModelAndView getParam(HttpServletRequest request,
HttpServletResponse response) {
String userName = request.getParameter("userName");
String password = request.getParameter("password");
System.out.println(userName);
System.out.println(password);
return null;
}
2)通过springMVC的@RequestParam注解的简单参数绑定传参
@RequestMapping("/param")
public ModelAndView getParam(@RequestParam("userName") String user,
String password) {
System.out.println(user);
System.out.println(password);
return null;
}
3)通过User类的pojo参数绑定传参
public class User {
private String userName;
private String password;
………………
//getterandsetter
}
@RequestMapping("/param")
public ModelAndView getParam(User user) {
ModelAndView mv = new ModelAndView("test.jsp");
mv.addObject("userName",user.getUserName());
mv.addObject("password",user.getPassword());
return mv;
}
测试回显账号密码test.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>test</title>
</head>
<body>
<h1>${userName}<h1>
<h1>${password}<h1>
</body>
</html>
4、编写配置dispatcher-servlet.xml
添加扫描xmlns:context字段和更改xsi:schemaLocation,可参考最上方的context:component-scan使用说明。
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描controller下的组件 -->
<context:component-scan base-package="controller"/>
5、程序回显测试


网友评论