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

RequestParam注解

package org.springframework.web.bind.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Map; import org.springframework.core.annotation.AliasFor; @Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam { /**
* name属性的别名
*/
@AliasFor("name")
String value() default ""; /**
* 指定请求头绑定的名称
*/
@AliasFor("value")
String name() default ""; /**
* 指定参数是否为必须绑定
*/
boolean required() default true; /**
* 如果没有传递参数而使用的默认值
*/
String defaultValue() default ValueConstants.DEFAULT_NONE; }

UserController.java

package com.rookie.bigdata.controller;

import com.rookie.bigdata.domain.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import java.util.ArrayList;
import java.util.List; @Controller
@RequestMapping(value = "/user")
public class UserController { private static List<User> userList; public UserController() {
super();
userList = new ArrayList<User>();
} private static final Log logger = LogFactory
.getLog(UserController.class); @RequestMapping(value = "/register", method = RequestMethod.GET)
public String registerForm() {
logger.info("register GET方法被调用...");
// 跳转到注册页面
return "registerForm";
} @RequestMapping(value = "/register", method = RequestMethod.POST)
// 将请求中的loginname参数的值赋给loginname变量,password和username同样处理
public String register(
@RequestParam("loginname") String loginname,
@RequestParam("password") String password,
@RequestParam("username") String username) {
logger.info("register POST方法被调用...");
// 创建User对象
User user = new User();
user.setLoginname(loginname);
user.setPassword(password);
user.setUsername(username);
// 模拟数据库存储User信息
userList.add(user);
// 跳转到登录页面
return "loginForm";
} @RequestMapping("/login")
public String login(
// 将请求中的loginname参数的值赋给loginname变量,password同样处理
@RequestParam("loginname") String loginname,
@RequestParam("password") String password,
Model model) {
logger.info("登录名:" + loginname + " 密码:" + password);
// 到集合中查找用户是否存在,此处用来模拟数据库验证
for (User user : userList) {
if (user.getLoginname().equals(loginname)
&& user.getPassword().equals(password)) {
model.addAttribute("user", user);
return "welcome";
}
}
return "loginForm";
} }

浏览器访问如下:



registerForm.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>注册页面</title>
</head>
<body>
<h3>注册页面</h3>
<form action="register" method="post">
<table>
<tr>
<td><label>登录名: </label></td>
<td><input type="text" id="loginname" name="loginname" ></td>
</tr>
<tr>
<td><label>密码: </label></td>
<td><input type="password" id="password" name="password"></td>
</tr>
<tr>
<td><label>真实姓名: </label></td>
<td><input type="text" id="username" name="username" ></td>
</tr>
<tr>
<td><input id="submit" type="submit" value="注册"></td>
</tr>
</table>
</form>
</body>
</html>

loginForm.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>登录页面</title>
</head>
<body>
<h3>登录页面</h3>
<form action="login" method="post">
<table>
<tr>
<td><label>登录名: </label></td>
<td><input type="text" id="loginname" name="loginname" ></td>
</tr>
<tr>
<td><label>密码: </label></td>
<td><input type="password" id="password" name="password"></td>
</tr>
<tr>
<td><input id="submit" type="submit" value="登录"></td>
</tr>
</table>
</form>
</body>
</html>

PathVariable注解

org.springframework.web.bind.annotation.PathVariable注解类型可以非常方便地获得请求URL中的动态参数,@PathVariable注解只支持一个属性value,类型String,表示绑定的名称,如果省略默认绑定同名参数。

@RequestHeader注解

org.springframework.web.bind.annotation.RequestHeader注解类型用于将请求的头信息区书记映射到功能处理方法的参数上

package org.springframework.web.bind.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import org.springframework.core.annotation.AliasFor; @Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestHeader { /**
* name属性的别名
*/
@AliasFor("name")
String value() default ""; /**
* 指定请求头绑定的名称
*/
@AliasFor("value")
String name() default ""; /**
* 指定参数是否必须绑定
*/
boolean required() default true; /**
* 如果没有传递参数而使用默认值
*/
String defaultValue() default ValueConstants.DEFAULT_NONE; }

@CookieValue注解

org.springframework.core.annotation.CookieValue用于将请求的Cookie书记映射到功能处理方法的参数上

package org.springframework.web.bind.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import org.springframework.core.annotation.AliasFor; @Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CookieValue { /**
* name属性的别名
*/
@AliasFor("name")
String value() default ""; /**
* 指定请求头绑定的名称
*/
@AliasFor("value")
String name() default ""; /**
* 只是参数是否必须绑定
*/
boolean required() default true; /**
* 如果没有传递参数而使用的默认值
*/
String defaultValue() default ValueConstants.DEFAULT_NONE; }

DataBindingController.java

package com.rookie.bigdata;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping; /**
* Created by dell on 2019/6/15.
*/
@Controller
public class DataBindingController { private static final Log logger = LogFactory
.getLog(DataBindingController.class); // 测试@PathVariable注解
@RequestMapping(value = "/pathVariableTest/{userId}")
public void pathVariableTest(
@PathVariable Integer userId) {
logger.info("通过@PathVariable获得数据: " + userId);
} // 测试@RequestHeader注解
@RequestMapping(value = "/requestHeaderTest")
public void requestHeaderTest(
@RequestHeader("User-Agent") String userAgent,
@RequestHeader(value = "Accept") String[] accepts) {
logger.info("通过@requestHeaderTest获得数据: " + userAgent);
for (String accept : accepts) {
logger.info(accept);
}
} // 测试@CookieValue注解
@RequestMapping(value = "/cookieValueTest")
public void cookieValueTest(
@CookieValue(value = "JSESSIONID", defaultValue = "") String sessionId) {
logger.info("通过@requestHeaderTest获得数据: " + sessionId);
} }

index.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>数据绑定测试</title>
</head>
<body>
<h2>数据绑定测试</h2>
<a href="pathVariableTest/1">测试@PathVariable注解</a><br><br>
<a href="requestHeaderTest">测试@RequestHeader注解</a><br><br>
<a href="cookieValueTest">测试@CookieValue注解</a><br><br>
</body>
</html>

springmvc注解一的更多相关文章

  1. springMVC注解初步

    一.(补充)视图解析器---XmlViewResolver 作用:分离配置信息. 在视图解析器---BeanNameViewResolver的基础之上进行扩充,新建一个myView.xml分离信息 在 ...

  2. SpringMVC注解开发初步

    一.(补充)视图解析器---XmlViewResolver 作用:分离配置信息. 在视图解析器---BeanNameViewResolver的基础之上进行扩充,新建一个myView.xml分离信息 在 ...

  3. SpringMVC注解汇总(二)-请求映射规则

    接上一节SpringMVC注解汇总-定义 讲到Httpy请求信息 URL路径映射 1)普通URL路径映射 @RequestMapping(value={"/test1", &quo ...

  4. springMVC注解启用及优化

    使用注解的原因 最方便的还是启用注解 注解方便,而且项目中很流行. 配置文件尽量减少,主要使用注解方式. Springmvc的注解是在2.5版本后有了注解,如何开启注解配置文件 Web.xml文件中不 ...

  5. 6.SpringMVC注解启用

    SpringMVC注解可以帮助我们快速地注入 属性和参数 提高开发效率. 由于 有相当一部分人讨厌xml配置方式 注解可以覆盖 xml则不能 使用注解比xml规范化,因为很多注解都是java的规范的范 ...

  6. springMVC(注解版笔记)

    springMVC(注解版) 较之于非注解版本,发生一下变化: 1.配置文件需要配置的标签有: <!-- 包的扫描,此包下面的所有包都启用注解 --> <context:compon ...

  7. springMVC 注解版

    http://blog.csdn.net/liuxiit/article/details/5756115 http://blog.csdn.net/hantiannan/article/categor ...

  8. 腾讯公司数据分析岗位的hadoop工作 线性回归 k-means算法 朴素贝叶斯算法 SpringMVC组件 某公司的广告投放系统 KNN算法 社交网络模型 SpringMVC注解方式

    腾讯公司数据分析岗位的hadoop工作 线性回归 k-means算法 朴素贝叶斯算法 SpringMVC组件 某公司的广告投放系统 KNN算法 社交网络模型 SpringMVC注解方式 某移动公司实时 ...

  9. Spring+SpringMVC+MyBatis深入学习及搭建(十六)——SpringMVC注解开发(高级篇)

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7085268.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十五)——S ...

  10. SpringMVC注解HelloWorld

    今天整理一下SpringMVC注解 欢迎拍砖 @RequestMapping RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是 ...

随机推荐

  1. HTTP协议之chunk,单页应用这样的动态页面,怎么获取Content-Length的办法

    当客户端向服务器请求一个静态页面或者一张图片时,服务器可以很清楚的知道内容大小,然后通过Content-Length消息首部字段告诉客户端需要接收多少数据.但是如果是动态页面等时,服务器是不可能预先知 ...

  2. LeetCode 885. Spiral Matrix III

    原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and ...

  3. Windows WoW64浅析

    WOW64(Windows-On-Windows 64bit)是X64 Windows操作系统的一个子系统,为32位应用程序提供运行环境.类似的还有WOW32子系统,负责在32位Windows系统上运 ...

  4. rpm 子包创建学习

    rpm 在打包的时候,可以创建子包,这样可以清晰的进行软件包的拆分,以下是结合官方文档学习 的一个实践 预备条件 需要安装rpmdevtools spec 文件 内容 Name: foo Versio ...

  5. 使Jackson和Mybatis支持JSR310标准

    1.首先要确保Jackson和Mybatis正确地整合进项目了 2.添加额外的依赖 <dependency> <groupId>org.mybatis</groupId& ...

  6. vue.config.js 配置 scss,less,sass全局配置 vuecli3

    module.exports = { /* 部署生产环境和开发环境下的URL:可对当前环境进行区分,baseUrl 从 Vue CLI 3.3 起已弃用,要使用publicPath */ public ...

  7. vue-echarts在vue中的使用

    安装依赖: [win]npm install echarts vue-echarts [mac]sudo npm install echarts vue-echarts Vue-ECharts 默认在 ...

  8. Apache的安装部署 2(加密认证 ,网页重写 ,搭建论坛)

    一.http和https的基本理论知识1. 关于https: HTTPS(全称:Hypertext Transfer Protocol Secure,超文本传输安全协议),是以安全为目标的HTTP通道 ...

  9. 几句话总结一个算法之Policy Gradients

    强化学习与监督学习的区别在于,监督学习的每条样本都有一个独立的label,而强化学习的奖励(label)是有延后性,往往需要等这个回合结束才知道输赢 Policy Gradients(PG)计算某个状 ...

  10. kafka(四) 网络通讯

    参考文章 http://www.jianshu.com/p/eab8f15880b5 kafka nio通信机制:http://ifeve.com/kafka-nio/ 通讯协议:tcp 网络模型: