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. Class与Style绑定

    本文主要介绍如何使用Vue来绑定操作元素的class列表和内联样式(style属性). 因为class和style都是属性,所以通过v-bind命令来处理它们:只需要通过表达式计算出结果即可,不过字符 ...

  2. Django中涉及金融的项目

    在Django中,如果一个项目涉及了金融,他的要求是十分严格的. 所以嘞,这里就有一些坑,很多坑,第一次开发的时候很容易出现一系列的错误 在涉及金融计算的地方,不能使用float类型 什么鬼,但事实就 ...

  3. require/load/include/extend的区别

    require 一般用于加载一个库,当多次使用require加载一个库时,只有第一次有效,后面的都会加载失败,也就是会返回"false",以为require会追踪文件是否被加载. ...

  4. 【Java并发编程】:内存可见性

    加锁(synchronized同步)的功能不仅仅局限于互斥行为,同时还存在另外一个重要的方面:内存可见性.我们不仅希望防止某个线程正在使用对象状态而另一个线程在同时修改该状态,而且还希望确保当一个线程 ...

  5. 【链表】Add Two Numbers

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  6. Qt 绘图与动画系统

    Qt 提供了内置的绘图系统以及独立的QtOpenGL模块提供对OpenGL的支持.Qt提供了基于状态机的QPainter系统和面向对象的Graphics View系统. QPainter 基于状态机的 ...

  7. IOS开发常见第三方总结

    链接](https://github.com/languages​​/Objective-C/most_watched) * [three20](https://github.com/facebook ...

  8. UIKit 框架之UISearchBar、UITableViewController

    // // tableViewController.m // tableVC // // Created by City--Online on 15/6/1. // Copyright (c) 201 ...

  9. 浅析人脸检测之Haar分类器方法:Haar特征、积分图、 AdaBoost 、级联

    浅析人脸检测之Haar分类器方法 一.Haar分类器的前世今生 人脸检测属于计算机视觉的范畴,早期人们的主要研究方向是人脸识别,即根据人脸来识别人物的身份,后来在复杂背景下的人脸检测需求越来越大,人脸 ...

  10. iOS开源项目周报0330

    由OpenDigg 出品的iOS开源项目周报第十四期来啦.我们的iOS开源周报集合了OpenDigg一周来新收录的优质的iOS开源项目,方便iOS开发人员便捷的找到自己需要的项目工具等. FengNi ...