@RequestParam @RequestBody @PathVariable @RequestHeader @CookieValue @ModelAttribute注解使用

 

package com.itheima.domain;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 一个用户实体类,它可以包含多个账户
 * 
 * @author ChonZ
 *
 */
public class User implements Serializable {
	private String username;
	private String passwoed;
	private String loginname;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPasswoed() {
		return passwoed;
	}
	public void setPasswoed(String passwoed) {
		this.passwoed = passwoed;
	}
	public String getLoginname() {
		return loginname;
	}
	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}
}

User用户类 

 

package com.itheima.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.itheima.domain.User;

@Controller("springmvc")
@RequestMapping("account")
public class annotationTestController {
		

	/**RequestParam注解的使用
	 * value属性的设置参数,表示url参数可以使用该参数名传入参数:<a href="springmvc/testRequestParam?id=1">
	 * required属性为false:表示无须带有id参数<a href="springmvc/testRequestParam">
	 * @param accountId
	 * @return
	 */
	@RequestMapping("/testRequestParam")
	public String testRequestParam(@RequestParam(value="id",required=false) Integer accountId) {
		System.out.println("测试RequestParam注解的使用" + accountId);
		return "success";
	}
	
	
	/**RequestBody注解的使用
	 * 表单的name和value会一同传给body变量
	 * @param accountId
	 * @return
	 */
	@RequestMapping("/testRequestBody")
	public String testRequestBody(@RequestBody String body) {
		System.out.println("测试RequestBody注解的使用" + body);
		return "success";
	}

	
	/**PathVariable注解的使用
	 * <a href="springmvc/testPathVariable/1">
	 * /testPathVariable/{id}的{id}表示url的占位符,这里表示1
	 * value属性的设置参数,表示占位符的名称,这里是id,并把占位符的值赋给方法形参的id
	 * required属性为true:表示必须带有id参数
	 * @param accountId
	 * @return
	 */
	@RequestMapping("/testPathVariable/{id}")
	public String testPathVariable(@PathVariable(value="id",required=true) Integer id) {
		System.out.println("测试PathVariable注解的使用" + id);
		return "success";
	}
	
	
	/**RequestHeader注解的使用
	 * 获取请求头的相关参数
	 * @param accountId
	 * @return
	 */
	@RequestMapping("/testRequestHeader")
	public String testRequestHeader(@RequestHeader("Accept-Language") String header) {
		System.out.println("测试RequestHeader注解的使用" + header);
		return "success";
	}
	
	
	/**CookieValue注解的使用
	 * 获取请求头的JSESSIONID
	 * @param accountId
	 * @return
	 */
	@RequestMapping("/testCookieValue")
	public String testCookieValue(@CookieValue("JSESSIONID") String value) {
		System.out.println("测试CookieValue注解的使用" + value);
		return "success";
	}
	

	
	/*------------------ModelAttribute注解的使用--------------------------*/	
	
	/**
	 * 从表单中获取username password的值封装到User类中
	 * 由于没有loginname,所以写入到持久层,loginname的值null
	 * 避免这种情况发生,要需要用到ModelAttribute注解,先查询数据库该类信息,用于回显,再通过表单更新数据再次存入持久层
	 * @param accountId
	 * @return
	 */
	@RequestMapping("/testModelAttribute")
	public String testModelAttribute(User user) {
		System.out.println("测试ModelAttribute注解的使用" + user);
		return "success";
	}
	
	/**ModelAttribute注解的使用
	 * 模拟先去数据库先查了数据库用户的信息
	 * 返回一个User类
	 * @param accountId
	 * @return
	 */
	@ModelAttribute
	public User testModelAttributeBeforeController() {
		User user = new User();
		user.setUsername("test");
		user.setPasswoed("1234");
		user.setLoginname("login");
		System.out.println("数据库用户的信息" + user);
		return user;
	}
}


注解使用案例

 

<%@ 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>springmvc的入门案例</title>
</head>
<body>

	<!-- 1.RequestParam注解的使用 -->
	<a href="springmvc/testRequestParam?id=1">测试RequestParam</a>
	
	<!-- 2.RequestBody注解的使用 -->
	<form action="spring/testRequestBody" method="post">
		用户名:<input type="text" name="name"/><br/>
		密码:<input type="password" name="password"><br/>
		<input type="submit" value="提交"/>
	</form>
	
	<!-- 3.PathVariable注解的使用 -->
	<a href="springmvc/testPathVariable/1">删除</a>\
	
	<!-- 4.RequestHeader注解的使用 -->
	<a href="springmvc/testRequestHeader">测试RequestHeader</a>

	<!-- 5.CookieValue注解的使用 -->
	<a href="springmvc/testCookieValue">测试CookieValue</a>
	
	<!-- 6.ModelAttributr注解的使用 -->
	<form action="spring/testModelAttribute" method="post">
		登录名:<input type="text" name="loginname"/><br/>
		昵称:<input type="text" name="username"/>
		密码:<input type="password" name="password"><br/>
		<input type="submit" value="更新"/>
	</form>

</body>
</html>

index.jsp测试页面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值