zoukankan      html  css  js  c++  java
  • 006 请求处理方法签名

    一:请求处理方法签名介绍

    1.介绍

      

    二:@RequestParam

    1.使用@RequestParam绑定请求参数

      注意点:required。

      

    2.index.jsp

     1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
     2     pageEncoding="ISO-8859-1"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     <a href="helloworld3?username=cj&age=13">Test Rest Get</a>
    11     <br>
    12 </body>
    13 </html>

    3.RequestParamController.java

     1 package com.spring.it;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 import org.springframework.web.bind.annotation.RequestParam;
     6 
     7 @Controller
     8 public class RequestParamControl {
     9     @RequestMapping("/helloworld3")
    10     public String hello(@RequestParam(value="username") String username,
    11             @RequestParam(value="age") int age) 
    12     {
    13         System.out.println("username="+username+" ,age="+age);
    14         return "success";
    15     }
    16 }

    4.效果

      

    5.衍生

      如果required该参数是否必须,如果是false时,下面还可能会用到参数,这个时候可以使用下面的方式处理。

      在里面可以添加一个默认属性的参数。

      @RequestParam(value=“age”,required=“false”,defaultValue=“0”)

    三:@RequestHeader

    1.@RequestHeader绑定请求报头

      

    2.寻找请求头

      

    3.以Accept-Language为例

      

    4.index.jsp

     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     测试请求头
    11     <br>
    12     <a href="helloworld4/">Accept-Language</a>
    13     <br><br>
    14     
    15     测试请求参数
    16     <br>
    17     <a href="helloworld3?username=cj&age=13">Test Rest Get</a>
    18     <br>
    19 </body>
    20 </html>

    5.java

     1 package com.spring.it;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.RequestHeader;
     5 import org.springframework.web.bind.annotation.RequestMapping;
     6 
     7 @Controller
     8 public class RequestParamControl {
     9     @RequestMapping("/helloworld4")
    10     public String hello(@RequestHeader(value="Accept-Language") String al) 
    11     {
    12         System.out.println("Accept-Language="+al);
    13         return "success";
    14     }
    15 }

    6.效果

      

      

  • 相关阅读:
    ruby 二进制转十进制 Integer("0b101") = 5
    开始菜单和我的文档的我的图片及我的音乐变成 my pictrues 正常图标了
    ruby watir 莫名其妙的错误
    Excel SaveAS是去掉提示框
    apache && jboss安装
    ruby require include的区别
    ruby控制鼠标
    This error is raised because the column 'type' is reserved for storing the class in case of inheritance
    用正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等各类代码
    ASP.NET 如何动态修改 Header 属性如添加 Meta 标签 keywords description!
  • 原文地址:https://www.cnblogs.com/juncaoit/p/8419705.html
Copyright © 2011-2022 走看看