SpringMVC之@SessionAttribute和@ModelAttribute
2024-09-25 00:24:51
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>
最新文章
- Java - NIO
- think in java学习笔记
- Bandpass Signals(带通信号,或称窄带信号)
- @SpringBootApplication
- Travis-CI与Latex构建开源中文PDF
- 常用的PHP数据库操作方法(MYSQL版)
- (转)web网站架构演变
- 如何同时启动多个Tomcat服务
- 深入浅出 JavaScript 数组 v0.5
- 360开源的类Redis存储系统:Pika
- 打印object对象
- 打包程序时的证书问题(上传APP就出现Missing iOS Distribution signing indetity for)
- T4 代码生成 Demo (抽奖程序)
- master_pos_wait函数与MySQL主从切换
- Kettle根据时间戳同步数据实现
- [HNOI 2004]树的计数
- Spring Boot + Jersey发生FileNotFoundException (No such file or directory)
- ping -c 3 localhost
- Mybatis系列(三):Mybatis实现关联表查询
- vim的简单配置
热门文章
- Oracle连接Navicat Premium遇到的问题
- python 拆解包
- [原]调试实战——使用windbg调试崩溃在ole32!CStdMarshal::DisconnectSrvIPIDs
- tensorflow(三)
- Opencv笔记(十七)——轮廓性质
- centos mysql无法删除数据库
- topological space
- Windows_Management_Instrumentation
- 如何查看Linux系统下程序运行时使用的库?
- Spring Boot集成全局唯一ID生成器