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. Ubuntu 出现access denied by server while mounting

    3516cv500板端nfst调试时如此配置 虚拟机: #vi /etc/exports  添加 /home/"待分享文件路径"   *(rw,sync,no_root_squas ...

  2. Java 为什么需要包装类,如何使用包装类?

    出处:https://cloud.tencent.com/developer/article/1362754

  3. Java 字符流读写文件

    据说,java读写文件要写很多,贼麻烦,不像c艹,几行代码就搞定.只能抄抄模板拿来用了. 输入输出流分字节流和字符流.先看看字符流的操作,字节转化为字符也可读写. 一.写入文件 1.FileWrite ...

  4. HTML JAVASCRIPT CSS 大小写敏感问题

    html: 大小写不敏感 css: 大小写不敏感 javascript: 大小写敏感 但是 但是 但是 这三者是相互联系的, 所以合在一起使用的时候就产生了变化 ----  TagName, Clas ...

  5. day 17

    Our life is frittered away by detail, simplify it, simplify it. 我们的生活都被琐事浪费掉了,简单点,简单点.

  6. ERROR:Simulator861-Failed to link the design解决办法

    在安装目录下找到collect2.exe文件,删除就可以解决了.D:\install_dir\ISE2\14.7\ISE_DS\ISE\gnu\MinGW\5.0.0\nt\libexec\gcc\m ...

  7. The import junit cannot be resolved解决问题

    第一次安装Junit,配置环境之后发现添加语句import junit.framework.TestCase; 编译错误 解决:项目右键Properties->Java Build Path-& ...

  8. 常用Linux软件安装

    JDK 先从Oracle官网下载JDK Linux版本的安装包,上传到服务器,这里推荐在服务器中创建一个目录/software,可以将所有软件的安装包放在这个目录下(或者是/opt目录下),将软件包解 ...

  9. CF 494E Sharti

    CF 494E Sharti 题意:一个\(n \times n\)的棋盘,共有m个矩形中的格子为白色.两个人需要博弈,每次操作选择一个边长不超过k的正方形并翻转颜色,每次翻转需要正方形的右下角为白色 ...

  10. Linux笔记本合上屏幕不待机

    Linux笔记本合上屏幕不待机[]# vim /etc/systemd/logind.conf# This file is part of systemd.## systemd is free sof ...