打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
SpingMVC常用注解之@RequestParam
(在Spring MVC 后台控制层获取前台参数的方式主要有两种,一种是requset.getParameter(“name”),另一种是用注解@ResquestParam获取。)
org.springframework.web.bind.annotation.RequestParam 注解类型用于将指定的请求参数赋值给方法中的形参。
使用@RequestParam注解,可指定@RequestParam支持的属性
@RequestParam注解支持的属性属性类型是否必要说明
nameString否指定请求头绑定的名称
valueString否前台name属性的别名
requiredboolean否指示参数是否必须绑定
defaultValueString否如果没有传递参数而使用的默认值
例:
1、前台代码
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title></head><body><form action="show.do" method="post">姓名<input type="text" name="name" value="name"/><br/>年龄<input type="text" name="age" value="age"/><br/><input type="submit" value="确认"/></form></body></html>
两个前台参数name 和 age
2、控制层代码
import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.servlet.ModelAndView;@Controller public class StudentController { @RequestMapping("/show") public ModelAndView show(@RequestParam(value="name",required=false) String name1,@RequestParam(value="age",required=false) String age1,HttpServletResponse response)throws Exception{ ModelAndView mav=new ModelAndView(); mav.addObject("name", name1); mav.addObject("age", age1); mav.setViewName("show"); return mav; } }
@RequestParam(value="name" 中的name 是前台参数name ,将它赋给形参name1,然后ModelAndView对象调用addObject方法,将形参数据传递给showname
然后前台用EL表达式获取showname,就可以将用户输入到输入框中的数据显示到显示页面上。
3、显示页面show.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>Insert title here</title></head><body>名字:${showname }年龄:${showage }</body></html>
4、结果显示
运行  在index.html 输入框中输入 数据
点确认后,后台控制层处理,将数据传到show.jsp显示。
参考书籍:《Spring MyBatis企业应用实战》 疯狂软件编著,电子工业出版社。
来源:https://www.icode9.com/content-4-814851.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Spring MVC中RequestMapping的各种用法
springmvc 中的 ModelAndView 遍历其中额的数据
SpringMVC 文件上传配置,多文件上传,使用的MultipartFile
Spring MVC异常处理
Spring MVC 接收请求参数所有方式总结!
SpringMVC学习系列(3) 之 URL请求到Action的映射规则
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服