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.效果

      

      

  • 相关阅读:
    OCP 062【中文】考试题库(cuug内部资料)第29题
    413. 等差数列划分 力扣(中等) 找规律,细节
    264. 丑数 II 力扣(中等) 动态规划,不会
    313. 超级丑数 力扣(中等) 动态规划,不会做
    5840. 使字符串平衡的最小交换次数 力扣(中等) 第255场oppo周赛 猜出来的
    手写一个仿微信登录的Nodejs程序
    你不知道的CSS国际化
    React实现类似淘宝tab居中切换效果
    原来 CSS 这样写是会让 App 崩溃的
    css中class和id之间有什么区别?
  • 原文地址:https://www.cnblogs.com/juncaoit/p/8419705.html
Copyright © 2011-2022 走看看