SpringBoot:Thymeleaf页面动态传参的方法

HTML中th:href的格式用法:

<a th:href="@{'/demo/process/bigPhoto?photoName='+${photo.photoName}}">点我查看大图</a>

总结起来格式就是:
th:href="@{‘字符串’+${动态变量}}"。

在另一个Thymeleaf页面取得request的属性参数前面要加前缀param
比如:<p th:text="${param.photoName}">,这个就是从request对象中取出了photoName属性

如果是取session属性,前面就要加上session前缀
比如:<p th:text="${session.photoName}">,这就从session对象中取出了photoName属性

参考博客1:https://blog.csdn.net/wanderlustLee/article/details/80714457
参考博客2:https://blog.csdn.net/sun1021873926/article/details/61615219

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot+Thymeleaf中,Controller中的修改方法通常需要以下步骤: 1. 在Controller中定义一个处理修改请求的方法,例如: ```java @RequestMapping(value = "/edit/{id}", method = RequestMethod.POST) public String edit(@PathVariable("id") Long id, Model model, @ModelAttribute("form") MyForm form) { // 根据id查询要修改的据 MyData myData = myDataService.findById(id); if (myData == null) { throw new NotFoundException(); } // 将form中的据赋值给myData myData.setName(form.getName()); myData.setValue(form.getValue()); // 更新据 myDataService.save(myData); // 重定向到列表页面 return "redirect:/list"; } ``` 2. 在Thymeleaf模板中,使用form表单提交修改请求,例如: ```html <form th:action="@{/edit/{id}(id=${myData.id})}" method="post" th:object="${form}"> <div class="form-group"> <label for="name">Name</label> <<input type="text" class="form-control" id="name" name="name" th:value="*{name}" /> </div> <div class="form-group"> <label for="value">Value</label> <input type="text" class="form-control" id="value" name="value" th:value="*{value}" /> </div> <button type="submit" class="btn btn-primary">Save</button> </form> ``` 3. 在Controller中,需要将修改前的据显示在修改页面上,例如: ```java @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET) public String edit(@PathVariable("id") Long id, Model model) { // 根据id查询要修改的据 MyData myData = myDataService.findById(id); if (myData == null) { throw new NotFoundException(); } // 将据传递给form对象 MyForm form = new MyForm(); form.setName(myData.getName()); form.setValue(myData.getValue()); // 将form对象传递给页面 model.addAttribute("form", form); return "edit"; } ``` 这样,在访问修改页面时,会将修改前的据显示在表单中,用户可以直接修改并提交。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值