springmvc注解一
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注解一的更多相关文章
- springMVC注解初步
一.(补充)视图解析器---XmlViewResolver 作用:分离配置信息. 在视图解析器---BeanNameViewResolver的基础之上进行扩充,新建一个myView.xml分离信息 在 ...
- SpringMVC注解开发初步
一.(补充)视图解析器---XmlViewResolver 作用:分离配置信息. 在视图解析器---BeanNameViewResolver的基础之上进行扩充,新建一个myView.xml分离信息 在 ...
- SpringMVC注解汇总(二)-请求映射规则
接上一节SpringMVC注解汇总-定义 讲到Httpy请求信息 URL路径映射 1)普通URL路径映射 @RequestMapping(value={"/test1", &quo ...
- springMVC注解启用及优化
使用注解的原因 最方便的还是启用注解 注解方便,而且项目中很流行. 配置文件尽量减少,主要使用注解方式. Springmvc的注解是在2.5版本后有了注解,如何开启注解配置文件 Web.xml文件中不 ...
- 6.SpringMVC注解启用
SpringMVC注解可以帮助我们快速地注入 属性和参数 提高开发效率. 由于 有相当一部分人讨厌xml配置方式 注解可以覆盖 xml则不能 使用注解比xml规范化,因为很多注解都是java的规范的范 ...
- springMVC(注解版笔记)
springMVC(注解版) 较之于非注解版本,发生一下变化: 1.配置文件需要配置的标签有: <!-- 包的扫描,此包下面的所有包都启用注解 --> <context:compon ...
- springMVC 注解版
http://blog.csdn.net/liuxiit/article/details/5756115 http://blog.csdn.net/hantiannan/article/categor ...
- 腾讯公司数据分析岗位的hadoop工作 线性回归 k-means算法 朴素贝叶斯算法 SpringMVC组件 某公司的广告投放系统 KNN算法 社交网络模型 SpringMVC注解方式
腾讯公司数据分析岗位的hadoop工作 线性回归 k-means算法 朴素贝叶斯算法 SpringMVC组件 某公司的广告投放系统 KNN算法 社交网络模型 SpringMVC注解方式 某移动公司实时 ...
- Spring+SpringMVC+MyBatis深入学习及搭建(十六)——SpringMVC注解开发(高级篇)
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7085268.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十五)——S ...
- SpringMVC注解HelloWorld
今天整理一下SpringMVC注解 欢迎拍砖 @RequestMapping RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是 ...
随机推荐
- react小项目
本章要讲述一个评价栏的制作. 首先先简单写一个ract组件来试试. index.html <!DOCTYPE html> <html> <head> <tit ...
- Springboot测试类之@RunWith注解
@runWith注解作用: --@RunWith就是一个运行器 --@RunWith(JUnit4.class)就是指用JUnit4来运行 --@RunWith(SpringJUnit4ClassRu ...
- 循环递减算法 [a,b,c] 求 ab,ac,bc
有数组 lineList=[a,b,c] 求所有不同的两两组合 ,结果:ab,ac,bc lineList.forEach((lineA,lineIndex)=>{ ==len){ return ...
- 小学四则运算口算练习app---No.4
今天主要是改了出题页中各个组件的位置以及时间的接收还有时间控制,代码如下:(但是存在一个问题 设置页面点击确定按钮进入出题界面时有时会闪退,未解决!) CalculatorActivity.clas ...
- requests-html
目录 一 介绍 二 安装 三 如何使用requests-html 四 支持JavaScript 五 自定义User-Agent 六 模拟表单提交 七 支持异步请求 一 介绍 Python上有一个非常著 ...
- .Net Core 获取项目所有程序集,排除Microsoft、Nuget下载的
https://www.cnblogs.com/yanglang/p/6866165.html public static List<Assembly> BaiqianAssemblies ...
- Windbg Command Browser(命令浏览器)窗口的使用
命令浏览器窗口显示并存储调试器命令的文本结果.此窗口创建命令引用,使您可以查看特定命令的结果,而无需重新输入该命令.命令浏览器窗口还提供了对存储的命令的导航,因此您可以比使用调试器命令窗口更快地访问命 ...
- Codeforces Round #606 (Div. 2) D - Let's Play the Words?(贪心+map)
- centos6中安装新版 Elasticsearch 7.x
es出新版了,虽然公司里还是用的老版本,但是本地还是有必要自己安装了玩玩 下载地址:https://www.elastic.co/cn/downloads/elasticsearch 那么一般来说还是 ...
- PATB1024科学计数法
代码是部分正确,只得了13分还有两个测试点没有通过,不知道原因是啥,先不深究了,赶进度. 参考代码: #include<cstdio> #include<cstring> #i ...