zoukankan      html  css  js  c++  java
  • SpingMVC常用注解之@RequestParam

    (在Spring MVC 后台控制层获取前台参数的方式主要有两种,一种是requset.getParameter(“name”),另一种是用注解@ResquestParam获取。)

    org.springframework.web.bind.annotation.RequestParam 注解类型用于将指定的请求参数赋值给方法中的形参。

    使用@RequestParam注解,可指定@RequestParam支持的属性

    @RequestParam注解支持的属性
    属性 类型 是否必要 说明
    name String 指定请求头绑定的名称
    value String 前台name属性的别名
    required boolean 指示参数是否必须绑定
    defaultValue String 如果没有传递参数而使用的默认值

    例:

    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企业应用实战》 疯狂软件编著,电子工业出版社。

    本博客为博主的学习笔记,不作任何商业用途。
  • 相关阅读:
    LeetCode 623. Add One Row to Tree
    LeetCode 894. All Possible Full Binary Trees
    LeetCode 988. Smallest String Starting From Leaf
    LeetCode 979. Distribute Coins in Binary Tree
    LeetCode 814. Binary Tree Pruning
    LeetCode 951. Flip Equivalent Binary Trees
    LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List
    LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
    LeetCode 687. Longest Univalue Path
    LeetCode 428. Serialize and Deserialize N-ary Tree
  • 原文地址:https://www.cnblogs.com/guo7533/p/9018087.html
Copyright © 2011-2022 走看看