【问题标题】:Optional parameters in thymeleaf urlthymeleaf url 中的可选参数
【发布时间】:2015-08-19 06:17:05
【问题描述】:

我正在使用以下代码在 thymeleaf 中生成一个 url。

@{/schedule/data/bookableTimes/{teacherUsername}(teacherUsername=${teacher.user.username},lessonLengthType=${lessonLengthType},studentId=${#httpServletRequest.getParameter('studentId')})}

当提供 studentId 时,这非常有效。但是,我也想满足未提供 studentId 的情况。 目前,如果未提供 studentId,它将生成一个类似这样的 url

/schedule/data/bookableTimes/teacher?lessonLengthType=full&studentId=

但这不是我想要的,在 studentId 为空的情况下,我宁愿根本不生成 url 的 studentId 部分。有没有使用百里香的简单方法?

【问题讨论】:

标签: thymeleaf


【解决方案1】:

我会创建一个字符串并连接一些类似的部分

string url ="/schedule/data/bookableTimes/";
if (teacherUser) url.add(teacher)
if (studentId) url.add(studentId)
@{*url}

【讨论】:

  • 嗨,David,您的意思是在 Controller 中执行此操作吗?我想我可以做到,但这并不是我所希望的解决方案。
  • 不需要你可以纯粹在 Thymeleaf 中完成。你到底想在哪里做呢?
猜你喜欢
  • 2020-01-13
  • 1970-01-01
  • 1970-01-01
  • 2013-03-29
  • 1970-01-01
  • 2021-07-09
  • 1970-01-01
  • 1970-01-01
  • 2012-12-30
相关资源
最近更新 更多