【问题标题】:Spring boot Requested mapping value variableSpring boot 请求的映射值变量
【发布时间】:2019-05-12 11:14:29
【问题描述】:

我的控制器上有这个功能:

@RequestMapping(value = "/Myvariable")
public String fct(Model md, @RequestParam String Myparameter) {//code}

@RequestMapping(value = "/Myvariable2")
public String fct2(Model md, @RequestParam String Myparameter2) {//code}

@RequestMapping(value = "/Myvariable3")
public String fct3(Model md, @RequestParam String Myparameter3) {//code}

函数与这样的 URL 相关:

http://localhost:3000/Myvariable?Myparameter=***

http://localhost:3000/Myvariable2?Myparameter2=***

http://localhost:3000/Myvariable3?Myparameter3=***

如何使只有一个RequestMapping和一个代码只有一页html来发挥三页的作用?

【问题讨论】:

  • 尽量提供更多细节,给我们一些例子来了解你的情况!
  • 我有三个具有相同功能的页面:localhost:3000/home?Myparameter=01localhost:3000/study?Myparameter=02localhost:3000/work?Myparameter=05 当我输入其中一个网址时,我会调用它的控制器吗?但每个控制器内部的代码都是相同的,唯一改变的是页面名称。我想设置一页,例如localhost:3000/variable,这个变量可以是 {home,work,study} 。
  • 好的,所以你可以做一个 requestMapping 并设置你的变量或参数,你喜欢一个开关或 if 条件来测试是家庭、工作还是学习,然后你返回正确的页面!

标签: html spring spring-boot jakarta-ee frameworks


【解决方案1】:

你可以使用PathVariable和多个参数:

@RequestMapping(value = /{myVar})
public String fct(Model md, @PathVariable("myVar") String myVar, @RequestParam String Myparameter, @RequestParam String Myparameter2, @RequestParam String Myparameter3) {

指示方法参数应绑定到 URI 模板变量的注释。支持 RequestMapping 带注释的处理程序方法。

考虑将变量名改为camelCase 为myParameter2

【讨论】:

  • 但是在这种情况下如何设置页面名称?我想要一个html页面
猜你喜欢
  • 2019-11-25
  • 1970-01-01
  • 1970-01-01
  • 2019-02-03
  • 2019-11-13
  • 2020-04-19
  • 2018-03-18
  • 2022-01-12
  • 2021-07-01
相关资源
最近更新 更多