package com.sample.smartmap.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import com.sample.smartmap.entity.User;
import com.sample.smartmap.service.UserService; @Controller
@RequestMapping("/model")
public class ModelAndViewController { @Autowired UserService userService;
/**
* 一个beetl模板测试。因为视图扩展名字是btl
* @param userId
* @param model
* @return
*/
@GetMapping(path = "/{userId}/get.html")
public String getUser(@PathVariable Long userId,Model model) {
User userInfo = userService.getUserById(userId);
//model.addAttribute(userInfo); 与下面一行作用一样,但这会有潜在问题
model.addAttribute("user", userInfo);
return "/userInfo.html";
}
/**
* 使用freemaker模板测试,freemaker会寻找/userInfo.ftl 模板
* @param userId
* @param view
* @return
*/
@GetMapping(path = "/{userId}/get2.html")
public ModelAndView getUser2(@PathVariable Long userId,ModelAndView view) {
User userInfo = userService.getUserById(userId);
//model.addAttribute(userInfo);
view.addObject("user", userInfo);
view.setViewName("/userInfo");
return view;
} }
package com.sample.smartmap.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import com.sample.smartmap.controller.form.OrderPostForm;
import com.sample.smartmap.service.UserService; @Controller
@RequestMapping("/modelattribute")
public class ModelAttributeController {
@Autowired UserService userService;
/**
* Controller方法中的公共放啊,调用方法前先调用此方法。
* @param id
* @param model
*/
@ModelAttribute
public void findUserById(@PathVariable Long id,Model model) {
model.addAttribute("user", userService.getUserById(id));
} @GetMapping(path = "/{id}/get.json")
@ResponseBody
public String getUser(Model model) {
System.out.println(model.containsAttribute("user"));
return "success";
} }

Spring Boot—10ModelAndView、Model,以及@ModelAttribute注解的更多相关文章

  1. Spring Boot 2.0 教程 | @ModelAttribute 注解

    欢迎关注微信公众号: 小哈学Java 文章首发于个人网站: https://www.exception.site/springboot/spring-boot-model-attribute Spri ...

  2. spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,guava限流,定时任务案例, 发邮件

    本文介绍spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,定时任务案例 集成swagger--对于做前后端分离的项目,后端只需要提供接口访问,swagger提供了接口 ...

  3. Spring Boot集成JPA的Column注解命名字段无效的问题

    偶然发现,Spring Boot集成jpa编写实体类的时候,默认使用的命名策略是下划线分隔的字段命名. Spring Boot版本:1.5.4.release 数据表: id int, userNam ...

  4. 必须知道的Spring Boot中的一些Controller注解

    这篇文章是抄其他人的,原址:https://cloud.tencent.com/developer/article/1082720 本文旨在向你介绍在Spring Boot中controller中最基 ...

  5. Spring Boot系列——AOP配自定义注解的最佳实践

    AOP(Aspect Oriented Programming),即面向切面编程,是Spring框架的大杀器之一. 首先,我声明下,我不是来系统介绍什么是AOP,更不是照本宣科讲解什么是连接点.切面. ...

  6. Spring Boot(四)@EnableXXX注解分析

    在学习使用springboot过程中,我们经常碰到以@Enable开头的注解,其实早在Spring3中就已经出现了类似注解,比如@EnableTransactionManagement.@ Enabl ...

  7. spring boot的ComponentScan和ServletComponentScan注解

    ComponentScan 这个注解可以扫描带@Component的类.众所皆知,@RestController和@Configuration和@Service和@Configuration等都有带C ...

  8. 精尽Spring Boot源码分析 - @ConfigurationProperties 注解的实现

    该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...

  9. spring boot 使用拦截器,注解 实现 权限过滤

    http://www.cnblogs.com/zhangXingSheng/p/7744997.html spring boot 使用拦截器 实现 用户登录拦截 http://www.cnblogs. ...

随机推荐

  1. day 52 Django 的中间件加载顺序

    前情提要: django的中间键的作用是进行加载 可以通过中间键进行辅助操作 1.中间件的概念 中间件顾名思义,是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局 ...

  2. 【LeetCode】414. 第三大的数

    给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n). 示例 1: 输入: [3, 2, 1] 输出: 1 解释: 第三大的数是 1. 示例 2 ...

  3. 解决ASP.NET MVC 下使用SQLite 报no such table的问题

    观察后发现项目中数据库的存放位置不正确. Web项目添加到App_Data文件夹下, 文件始终不复制 Web.Config文件下的连接字符串 <add name="SQLiteconn ...

  4. WINDOWS 下 修改APACHE 并发数

    某次,配置大型站点.日IP过2W. 刚解析完,就特别卡,每个页面都是慢吞吞的打开的. 至少30秒.但是,3389进入服务器很快,CPU 内存都是几乎为0. 想到WINDOWS下使用的是APACHE,并 ...

  5. MySQL中date类型的空值0000-00-00和00:00:00

    1.如果mysql中使用了date类型,并且默认值为'0000-00-00', 那么数据库中的'0000-00-00 00:00:00', '0000-00-00', '00:00:00'这三个值是相 ...

  6. 在Linux系统安装Nodejs最简单步骤

    一.去官网下载和自己系统匹配的文件 英文网址:https://nodejs.org/en/download/ 中文网址:http://nodejs.cn/download/ 通过  uname -a ...

  7. Java序列化机制和原理

    Java序列化算法透析 Serialization(序列化)是一种将对象以一连串的字节描述的过程:反序列化deserialization是一种将这些字节重建成一个对象的过程.Java序列化API提供一 ...

  8. canvas文字自动换行、圆角矩形画法、生成图片手机长按保存、方形图片变圆形

    canvas的文字自动换行函数封装 // str:要绘制的字符串 // canvas:canvas对象 // initX:绘制字符串起始x坐标 // initY:绘制字符串起始y坐标 // lineH ...

  9. 小程序之底部tabBar

    用法简介: 1.app.json中配置下tabBar即可,注意tabBar至少需要两个最多五个Item选项 这里简单列举一些属性值:对于tabBar整体属性设置: 对于tabBar中每个Item属性设 ...

  10. 利用meta标签将http请求换成https请求

    最近网站升级为https之后,为了防止一些http文件没有修改而引起的问题,可以加一个meta标签: <meta http-equiv="Content-Security-Policy ...