@SessionAttributes

@sessionattributes注解应用到Controller上面,可以将Model中的属性同步到session作用域当中。

SessionAttributesController.java

package com.rookie.bigdata;

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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes; @Controller
// 将Model中的属性名为user的放入HttpSession对象当中
@SessionAttributes("user")
public class SessionAttributesController{
private static final Log logger = LogFactory
.getLog(SessionAttributesController.class); @RequestMapping(value="/{formName}")
public String loginForm(@PathVariable String formName){
// 动态跳转页面
return formName;
} @RequestMapping(value="/login")
public String login(
@RequestParam("loginname") String loginname,
@RequestParam("password") String password,
Model model ) {
// 创建User对象,装载用户信息
User user = new User();
user.setLoginname(loginname);
user.setPassword(password);
user.setUsername("admin");
// 将user对象添加到Model当中
model.addAttribute("user",user);
return "welcome";
} }

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>测试@SessionAttributes注解</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>



输入用户名和面,点击登录按钮,请求会被提交到SessionAttributesController中的login方法,该方法将会创建User对象来保存数据,并将其设置到HttpSession作用域当中。

@ModelAttribute注解

org.springframework.web.bind.annotation.ModelAttribute注解类型将请求参数绑定到Model对象中。被@ModelAttribute注解的方法会在Controller每个方法之执行前被执行。

@ModelAttribute注解只支持一个属性value,类型String,表示绑定的属性名称。

最新文章

  1. Python virtualenv with Sublime Text 3
  2. 从一个复杂的json格式的String内获取某key的值
  3. day--6_python常用模块
  4. HTML5中lineCap端点样式遇到closePath()
  5. oracle 自动关闭 数据库连接
  6. Linux 线程与进程,以及通信
  7. GitHub使用教程for Eclipse
  8. C的快速趋向实验 --&gt;
  9. win7/win8 64位系统注册TeeChart8.ocx 控件---以及dllregisterserver调用失败问题解决办法
  10. Photoshop颜色出现比较大的偏差,偏色严重,显示器配置文件2351似乎有问题
  11. Python编程从入门到实践笔记——类
  12. Day 5内存管理,定义变量
  13. Java关键字(三)——static
  14. Day12-navicat for sqlite 11.1.12 patch 永久使用版
  15. ORACLE 执行计划
  16. Awk使用及站点日志分析
  17. CSS 实现单边阴影
  18. python中常用的内建模块
  19. rac库grid目录权限(6751)导致数据库宕机案例 此方法仅用于紧急救助
  20. Centos下使用压缩包安装MySQL5.7

热门文章

  1. Spring Cloud Ribbon 源码分析---负载均衡算法
  2. 更改THttpClientSocket连接超时时间
  3. SpringBoot(十八):SpringBoot2.1.1引入SwaggerUI工具
  4. js中[object Object]与object.prototype.toString.call()
  5. SpringBoot 为API添加统一的异常处理(一)
  6. JMH java基准测试
  7. HTML a标签链接 设置点击下载文件
  8. kotlin基础 尾递归
  9. Golang常见小细节总结(1)
  10. 008-linux shell vim使用