Spring Boot—10ModelAndView、Model,以及@ModelAttribute注解
2023-09-11 14:19:52 时间
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: spring Aware的目的是为了让Bean获得Spring容器的服务
- 20-spring学习-Spring MVC基本操作
- Spring Boot 2.x :通过 spring-boot-starter-hbase 集成 HBase
- spring boot:swagger3文档展示分页和分栏的列表数据(swagger 3.0.0 / spring boot 2.3.3)
- spring Boot打可执行的jar包
- Spring boot后台搭建二集成Shiro添加Remember Me
- spring boot:actuator的安全配置:使用spring security做ip地址限制(spring boot 2.3.2)
- Spring在web开发中的应用
- Spring Boot应用的测试——Mockito
- Core java class in Spring
- Spring Boot应用的健康监控
- CSE 支持spring 4/5 以及spring boot 1/2 maven组件依赖关系配置参考
- 学习Spring Boot:(十四)spring-shiro的密码加密
- spring clould -多模块 -swagger2 配置 nginx 的正确设置
- 毕业设计 Spring Boot的考研信息收集与查询系统(含源码+论文)
- 毕业设计 Spring Boot的家教应聘招聘管理系统(含源码+论文)
- 一文详说Spring Boot集成RabbitMQ,细说消息队列常用5种模式,消息持久化,两种延迟发送的机制以及解决Spring Boot集成RabbitMQ的常见问题
- 一文解读spring boot的@SpringBootTest、@RunWith、webEnvironment等测试注解,比如spring boot的单元测试、切片测试、功能测试以及mock测试
- 解决com.alibaba.fastjson.JSONException: write javaBean error问题以及解决Spring Boot加入Shiro导致spring aop失效的问题
- 001-Spring Cloud Edgware.SR3 升级最新 Finchley.SR1,spring boot 1.5.9.RELEASE 升级2.0.4.RELEASE注意问题点
- 020-Spring Boot 监控和度量
- 009-Spring Boot 事件监听、监听器配置与方式、spring、Spring boot内置事件
- Spring Boot Thymeleaf示例
- 从Spring Boot信息泄露到AWS控制台劫持(攻破aws亚马逊服务器)
- 【java】Spring Boot --spring boot项目整合xxl-job
- 启动Spring boot项目报错:java.lang.IllegalArgumentException: LoggerFactory is not a Logback