看过之前的执行流程,相信你对springMVC有了一定的认识。今天我们继续来学习springMVC相关的核心知识,帮助你快速起飞,完成蜕变。
猿蜕变同样是一个原创系列文章,帮助你从一个普通的小白,开始掌握一些行业内通用的框架技术知识以及锻炼你对系统设计能力的提升,完成属于你的蜕变,更多精彩内容,敬请大家关注公主号猿人工厂,点击猿人养成获取!
RequestMapping是用于指定程序访问入口的annotation,代表了程序被访问的URI,掌握好它的使用,你就掌握了springMVC的程序入口。你甚至可以使用通配符的方式来指定url,比如:
@RequestMapping("/*mapping.do")
这样就表示url中只要是以mapping.do结尾的就可以被当前方法处理。实际的开发中,这种方式不算常见,大家了解就好。
在@RequestMapping中有一个method属性,可以指定这个方法只处理get或者是post请求:
@RequestMapping(value="/handlePost.do", method = RequestMethod.POST)
public ModelAndView handlePost(HttpServletRequestreq, HttpServletResponse res) throws Exception{
ModelAndView mv = new ModelAndView();
mv.addObject("hello", "hello spring web mvc by handlePost");
mv.setViewName("helloByAnnotation");
return mv;
}
我们看上面这个例子,只有使用post方法提交请求,handlePost才会被执行,因为method属性指定了方法只能被以post的方式访问。如果不使用method属性,那么无论是get还是post的方式都可以访问。
例子:只能get方式访问
@RequestMapping(value="/handleGet.do", method = RequestMethod.GET)
public ModelAndView handleGet(HttpServletRequestreq, HttpServletResponse res) throws Exception{
ModelAndView mv = new ModelAndView();
mv.addObject("hello", "hello spring web mvc by handleGet");
mv.setViewName("helloByAnnotation");
return mv;
}
RequestMapping还提供了params属性来限制请求中的参数。
@RequestMapping(value="/requireParama.do", params={"hello" , "name"})
public ModelAndViewrequireParama(HttpServletRequest req, HttpServletResponse res) throws Exception {
ModelAndView mv = new ModelAndView();
mv.addObject("hello", "hello spring web mvc by helloAnnotation");
mv.setViewName("helloByAnnotation");
return mv;
}
要求请求中必须有参数名为hello和name的参数。
@RequestMapping(value="/requireParama2.do", params={"!hello" , "name=pz"})
public ModelAndView requireParama2(HttpServletRequestreq, HttpServletResponse res) throws Exception{
ModelAndView mv = new ModelAndView();
mv.addObject("hello", "hello spring web mvc by helloAnnotation" + req.getParameter("name"));
mv.setViewName("helloByAnnotation");
return mv;
}
要求请求中不能出现参数名为hello,但是必须出现参数名为name,并且参数name的值还必须为”pz”.
使用基本类型接收表单参数
我们在开发中会遇到很多表单提交的请求,最常见的就是用户注册页面,我们通过下面这个例子,学习下怎样在Spring MVC中获取表单提交的参数。
示例:
我们首先编写一个jsp页面:form.jsp(注意:直接放在webapp目录下,因为我们需要直接访问,这样处理比较简单)
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"isELIgnored="false"%>
<html>
<head>
<title>spring form表单样例</title>
</head>
<body>
<form action="/submitForm.do"method=post>
<lable>姓名:</lable>
<input type="text"name="name" id="name"/><br />
<lable>电话:</lable>
<input type="text"name="phone" id="phone"/><br />
<lable>年龄:</lable>
<input type="text"name="age" id="age"/><br />
<input type="submit"value="提交"id="submit" /><br />
</form>
</body>
</html>
然后,编写一个controller来接收表单提交的参数:
package com.pz.web.study.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
publicclass FormControllerDemo {
@RequestMapping("/submitForm.do")
public ModelAndView submitForm(String name, Stringphone) throws Exception {
ModelAndView mv = new ModelAndView();
mv.addObject("name", name);
mv.addObject("phone", phone);
mv.setViewName("showForm");
return mv;
}
}
再编写一个jsp页面(showForm.jsp)用于展示从表单获取的数据:
<%@ page language="java" contentType="text/html;charset=utf-8"
pageEncoding="utf-8"isELIgnored="false"%>
<html>
<head>
<title>spring form表单样例</title>
</head>
<body>
<lable>姓名:</lable>
${name} <br />
<lable>电话:</lable>
${phone}<br /></