SpringMVC之@SessionAttribute和@ModelAttribute
1.Controller
package com.tz.controller; import java.util.Map; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping; import com.tz.domain.Course; @Controller
@RequestMapping("/ModelAttribute")
public class ModelAttributeController { @RequestMapping("/handle")
public String handle(Course c){
System.out.println("handle......"); System.out.println(c);
return "success";
} // //有返回值类型
// @ModelAttribute//优先执行
// public Course show(String cname){
// /**
// * 当form表单中提交的数据不是完整的实体类型的数据时,需要保证,
// * 没有提交数据的字段还是使用
// * 数据库对象原来的数据
// *
// */
// System.out.println("show.....");
// //通过课程名称去数据库查询数据
// Course c = new Course();
// //模拟数据库中的值
// c.setCname(cname);
// c.setNumber(123);
// c.setPrice(11);
// c.setStock(123);
// c.setTeacher("coco");
// return c;
// } //无返回值类型
@ModelAttribute//优先执行
public void show(String cname,Map<String,Course> map){
/**
* 当form表单中提交的数据不是完整的实体类型的数据时,需要保证,
* 没有提交数据的字段还是使用
* 数据库对象原来的数据
*/
System.out.println("show.....");
//通过课程名称去数据库查询数据
Course c = new Course();
//模拟数据库中的值
c.setCname(cname);
c.setNumber(123);
c.setPrice(11);
c.setStock(123);
c.setTeacher("coco"); map.put("c",c);
// return c;
} }
2.request.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>Insert title here</title>
</head>
<body>
<a href="handle?user=coco">访问</a><br/> <form action="ModelAttribute/handle" method="post">
课程名称:
<input type="text" name="cname"><br/>
主讲老师:
<input type="text" name="teacher"><br/>
<hr/>
<input type="submit" value="提交">
</form> </body> </html>

SpringMVC之@SessionAttribute和@ModelAttribute的更多相关文章
- SpringMVC(十七-二十) ModelAttribute 注解
有点难理解. 修饰方法是表示在该控制器的所有目标方法执行前都执行该modelattribute注解的方法. 修饰参数是表示什么?修饰参数时@modelattributes(value="xx ...
- springMVC源码分析--@ModelAttribute使用及运行原理
这一篇博客我们简单的介绍一下ModelAttribute的使用和运行原理. 1.首先@ModelAttribute是使用在方法或者上的,当使用在方法上时其作用于本身所在的Controller,在访问C ...
- springMVC注解用法:@modelattribute的用法
在Spring MVC里,@ModelAttribute通常使用在Controller方法的参数注解中,用于解释model entity,但同时,也可以放在方法注解里. 如果把@ModelAttrib ...
- 阶段3 3.SpringMVC·_03.SpringMVC常用注解_7 ModelAttribute注解
这个注解可以作用在方法上,也可以作用在参数上 演示 user里面有三个属性, 表单只提交了两个属性.缺少了date属性 date没有获取到值因为也没提交这个值. 下面返回的user对象.上面就会拿到 ...
- SpringMVC(二)--处理数据模型、ModelAndView、Model、Map、重定向、@ModelAttribute、
1.处理模型数据 Spring MVC 提供了以下几种途径输出模型数据: – ModelAndView:处理方法返回值类型为 ModelAndView 时, 方法体即可通过该对象添加模型数据 ...
- springMVC源码分析--@SessionAttribute用法及原理解析SessionAttributesHandler和SessionAttributeStore
@SessionAttribute作用于处理器类上,用于在多个请求之间传递参数,类似于Session的Attribute,但不完全一样,一般来说@SessionAttribute设置的参数只用于暂时的 ...
- SpringMvc @ModelAttribute 的用法
参考:Spring 3.x 企业应用开发实战 第15章:SpringMvc 页码:532 ModelAttribute 从字面上解释就是模型的属性. 对于MVC框架来说是模型数据是最重要的,因为 ...
- SpringMVC核心——返回值问题
一.SpringMVC 使用 ModelAndView 来处理返回值问题. 1.ModelAndView 官方描述: Holder for both Model and View in the web ...
- 一篇文章学会springMVC(转)
说在前面 本文只是入门 为什么用springMVC?springMVC有什么有缺点?springMVC和Struts有什么区别?等等这些问题可以参考网路上资源,本文的重点是快速带入,让大家了解熟悉sp ...
随机推荐
- 2019年java后端年终总结(六年开发经验),送给正在努力的你
长大之后,时间总是过得飞快,转眼之间,今年已经只剩下1天了.小时候总感觉遥不可及.只在科幻小说里面出现的2020年,已经开始进入蓄力期了. 这篇文章主要和大家聊一聊分析2019年java技术的更新给大 ...
- gff文件提取cds
#!/usr/bin/perl use strict; use warnings; ########input######## ];my $cut = &cut($gff);my %cut = ...
- centos快速安装mysql
1. wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 2. rpm -ivh mysql-community-r ...
- linux中awk的应用
1.awk的基本认识和使用方法,参考下面链接 https://www.cnblogs.com/timxgb/p/4658631.html 2.awk中关于条件判断的用法,如 https://blog. ...
- 通过javascri实现输入框只能输入数字
输入框只能输入数字 <input type="text" onkeyup="value=value.replace(/[^\d]/g,'');"> ...
- vue项目打包,生成dist文件夹,如何修改文件夹的名字
vue项目打包之后的dist文件目录是如下 如果想要修改dist或则static文件名称,需要打开config / index.js build: { // Template for index.ht ...
- shell_跳板机推送公钥
#!/bin/bash#push publickey to aap-servers#将局域网内可以ping通的主机ip保存到一个文件> ip_up.txtfor i in {2..10}do { ...
- HDU-6708 Windows Of CCPC(打表,递归)
http://acm.hdu.edu.cn/showproblem.php?pid=6708 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- space sniffer
space sniffer 一款检测本地磁盘文件占用情况的工具,高效,快速.
- GSON转换成Long型变为科学计数法及时间格式转换异常的解决方案
直接上工具类了,简单实用 public class GsonUtils { private static Gson gson = null; static { if (gson == null) { ...