Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional parameters in thymeleaf url

Tags:

thymeleaf

I am generating a url in thymeleaf using the following code.

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

This works perfectly, when the studentId is supplied. However I also want to cater for the scenario when the studentId is not supplied. Currently if the studentId is not supplied it will generate a url like so

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

However this is not what I want, in the case that the studentId is null, I'd rather not have the studentId portion of the url generated at all. Is there a simple way to do this using thymeleaf?

like image 270
Samuel Parsonage Avatar asked Nov 10 '22 06:11

Samuel Parsonage


1 Answers

I would create a string and concatenate some part something like

string url ="/schedule/data/bookableTimes/";
if (teacherUser) url.add(teacher)
if (studentId) url.add(studentId)
@{*url}
like image 148
David_Garcia Avatar answered Dec 04 '22 07:12

David_Garcia