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. postgresql数据库uuid重复引发血案

    问题背景 .定时任务调用存储过程.将数据插入临时表时.出现了uuid重复的报错. 报错信息 [SQL]select DB_DATA.PR_SELECT() [Err] ERROR: duplicate ...

  2. numpy的ravel()和flatten()函数比较

    功能 两个函数的功能都是将多维数组降为一维. 用法 import numpy as np arr = np.array([[1, 2],[3, 4]]) arr.flatten() arr.ravel ...

  3. centos7.2 get pid by process name with python3.6

    centos7.2 get pid by process name with python3.6 #-*- encoding:UTF-8 -*- import os import sys import ...

  4. (转)rpm2cpio和cpio

    原文:https://blog.csdn.net/jubincn/article/details/6687550 rpm2cpio命令:将rpm格式的文件转为cpio格式的文件.rpm是linux中常 ...

  5. APP 渠道推广【摘自网络】

    渠道的合作方式无非三种,一种是付费合作,那很简单,谈好价格付钱.第二种是免费,主要是就是首发,还有就是跟渠道的运营小伙伴搞好关系让帮忙给个免费的位置等,第三种是活动奖品合作,简而言之,就是渠道商会逢年 ...

  6. SPSS学习系列之SPSS Modeler怎么修改默认的内存大小(图文详解)

    不多说,直接上干货! 问题来源: 如果你的电脑内存配置比较低的话,会随着数据量增加(尤其是大数据),带不起的情况很有可能发生,会出现一些内存报错... ... 解决办法: 打开“工具”,在modele ...

  7. css自动适应宽度

    .itemContent { background-color:#F0F0F0; padding:3px 11px 11px 11px; text-align:justify; text-justif ...

  8. 简述C和C++的学习历程

    总是被问到,如何学习C和C++才不茫然,才不是乱学,想了一下,这里给出一个总的回复. 一家之言,欢迎拍砖哈. 1.可以考虑先学习C. 大多数时候,我们学习语言的目的,不是为了成为一个语言专家,而是希望 ...

  9. Eclipse中Maven项目出现红色感叹号问题

    在Eclipse环境下,有时我们会遇到Maven项目出现红色感叹号的情形,而一旦项目出现感叹号,Eclipse便无法帮我们进行自动编译等工作,尽管有时候不会影响运行,但每次只能手动启动Maven重新编 ...

  10. linux内核学习之保护模式(一)

    来源:http://blog.csdn.net/yishuige/article/details/50434746 这一章涉及intel8086系列cpu的保护模式编程,应该是学习内核编程,驱动编程及 ...