Spring Boot—10ModelAndView、Model,以及@ModelAttribute注解

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";
    } 

}

原文地址:https://www.cnblogs.com/gispathfinder/p/8921184.html

时间: 2024-07-31 01:19:04

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

Spring boot之SpringApplicationBuilder,@@Configuration注解,@Component注解

SpringApplicationBuilder: 该方法的作用是可以把项目打包成war包 需要配置启动类,pom.xml文件等,具体见:http://blog.csdn.net/linzhiqiang0316/article/details/52601292 @SpringBootApplication public class FavoritesApplication extends SpringBootServletInitializer{ /** * 如此配置打包后可以用tomcat下使

spring boot 学习心得 使用JpaRepository注解自定义SQL查询数据库多表查询

一. 首先在@Entity注解的类里面要写好外键关系. 这个 @ManyToOne 注解可以建立外键关系, 不要在自己傻傻的写一个 private int grades_id;  写了这个注解以后它会自动的把 Classes 这张表加上 grades_id 字段. 小伙伴们自己试验一下@ManyToOne/@ManyToMany/@OneToMany/@OneToOne这些注解会产生什么效果, 哈哈, 我当时高兴了一下午. 二. 在JpaRepository接口中写自定义多表查询语句 大体的语法

spring boot之入门Controller常用注解

Controller常用注解 @Controller  处理http请求 @RestController Spring4之后新加的注解,原来返回json数据需要@ResponseBody配合@Controller,现在合并成@RestController @RequestMapping  配置url映射,value配置url方法路径,method配置请求方式, 例:@RequestMapping(value="hello",method = RequestMethod.GET) @Pa

spring boot 配置属性值获取注解@Value和@ConfigurationProperties比较

功能比较 :     @ConfigurationProperties  @Value  映射赋值 批量注入配置文件中的属性 一个个指定 松散绑定(松散语法)① 支持 不支持 SpEL② 不支持 支持 JSR303数据校验③ 支持 不支持 复杂类型封装④ 支持 支持 说明 ① 指属性在配置文件中value是否对驼峰,下划线“_“ .连接线”-“ 都支持,如下写法,在javaBean中,属性值firstName是否都能获取到值 – person.firstName:使用标准方式 – person.

Spring boot注解分析

Spring Boot从一开始就告诉我们,她更喜欢基于Java的配置,即注解的方式.所以她提供了一大堆注解,并让我们习惯使用注解.其最大的特点是无需 XML 配置文件,能自动扫描包路径装载并注入对象,并能做到根据 classpath 下的 jar 包自动配置.这个过程摒弃了spring以往项目中大量繁琐的配置,通过自身默认配置,极大的降低了项目搭建的复杂度.同样在spring boot中,大量注解的使用,使得代码看起来更加简洁,提高开发的效率.这些注解不光包括spring boot自有,也有一些

Spring Boot 最核心的 25 个注解,都是干货!

Spring Boot 最核心的 25 个注解 1.@SpringBootApplication 这是 Spring Boot 最最最核心的注解,用在 Spring Boot 主类上,标识这是一个 Spring Boot 应用,用来开启 Spring Boot 的各项能力. 其实这个注解就是 @SpringBootConfiguration.@EnableAutoConfiguration.@ComponentScan 这三个注解的组合,也可以用这三个注解来代替 @SpringBootAppli

Spring Boot 最核心的 25 个注解

1.@SpringBootApplication 这是 Spring Boot 最最最核心的注解,用在 Spring Boot 主类上,标识这是一个 Spring Boot 应用,用来开启 Spring Boot 的各项能力. 其实这个注解就是@SpringBootConfiguration.@EnableAutoConfiguration.@ComponentScan这三个注解的组合,也可以用这三个注解来代替 @SpringBootApplication 注解. 2.@EnableAutoCo

第5章 Spring Boot 功能

Spring Boot 功能 本节将会介绍Spring Boot的一些细节. 在这里,您可以了解您将要使用和自定义的主要功能. 如果还没有准备好,您可能需要阅读第二部分“入门指南”和第三部分“使用 Spring Boot”部分,以使您有基础的良好基础. 23. SpringApplication SpringApplication类提供了一种方便的方法来引导将从main()方法启动的Spring应用程序. 在许多情况下,您只需委派静态SpringApplication.run()方法: publ

spring boot学习(十三)SpringBoot缓存(EhCache 2.x 篇)

SpringBoot 缓存(EhCache 2.x 篇) SpringBoot 缓存 在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManager),Spring Boot根据下面的顺序去侦测缓存提供者: * Generic * JCache (JSR-107) * EhCache 2.x * Hazelcast * Infinispan * Redis * Guava * Simple 关于 Spring Boot 的缓存机制: 高速缓