When I click a link (brandname) in front end, I need send a request to the server using th:href in thymeleaf. For that I followed below code
<a th:href="#{*{category.url}}" th:text=*{brandname}></a>
category.url and brandname values are coming dynamically from their respective Objects, But for above request I need to append extra content for that I tried something like below
<a th:href="#{*{category.url}+'?someExtraContent'}" th:text=*{brandname}></a>
and it is adding the extra content successfully, now my task is that 'extra content' I need to append dynamically, I read Local Variable concept in thymeleaf and I tried below code to append content to the request url dynamically
<a th:with="var=*{brandname}" th:href="#{*{category.url}+'?'+var}" th:text=*{brandname}></a>
I assumed that the var will stores the value and it will append to the request url but it is not happening I am getting the url like below
Ex:
localhost:8080/mycompany/mens?var
But I need something like below
Ex:
localhost:8080/mycompany/mens?nike
can anyone help me how to append dynamic content to the request url in thymeleaf
I found solution,
Previous code
<a th:with="var=*{brandname}" th:href="#{*{category.url}+'?'+var}" th:text=*{brandname}></a>
I replaced above code with
<a th:with="var=*{brandname}" th:href="#{*{category.url}+'?'+${var}}" th:text=*{brandname}></a>
Now I am able to append the dynamic content to my request url...
I have changed this var to ${var}
Related
I am trying to do same small service and sometimes I am sending requests to my Rest Api.
I am getting two problems:
How to fill field in form in html using thymeleaf? I have form like this and would like to fill fields with current values (th:value="${}" - not working for me):
<form th:action="#{/offences/edit}" method="post" th:object="${createOffenceForm}"> <input th:field="*{name}" th:value="${currentOffence.name}"/> <input th:field="*{penaltyPoints}" th:value="${currentOffence.penaltyPoints}"/> <input th:field="*{amountOfFine}" th:value="${currentOffence.amountOfFine}"/> <button type="submit">UPDATE</button> </form>
The problem is with loading css styles to html when I redirect to the site with path variable. For example i created html with two buttons. First one is hardcoded:
<a th:href="#{/offences/test}" class="link" style="text-decoration: none"><button class="buttonOK" type="submit">EDIT</button></a>
after redirect my site looks like this (everything works, it should be like that):
`
and here is after second button - redirect with path variable:
<a th:href="#{'/offences/edit/' + ${offence.id}}" class="link" style="text-decoration: none"><button class="buttonOK" type="submit">EDIT</button></a>
and view after load:
For the issue with your form data not being filled, this is because th:field and th:value are not meant to be used at the same time. So th:field ends up overwriting you value.
In your case I would suggest either manualy setting name (and id) or replacing th:object with the a filled version of the bean you want to return and only using th:field
As for the second issue it looks like a faulty fragment return on a post call to me but need to atleast have the controller functions and more complete html to say anything about that. And in general it's advisable to have 1 question per problem and not bundle things.
Ok so i found soultion.
To fill fields in form I only had to add to ModelMap form with fields.
Something like this and thymeleaf autofilled fields in html.
modelMap.addAttribute("createOffenceForm", new CreateOffenceForm(offenceDTO.getName(), offenceDTO.getPenaltyPoints(), offenceDTO.getAmountOfFine()));
Second solution is simple too. When changed mapping in Controller from #GetMapping("/path/{id}") to #GetMapping("/path-{id}") - everything works. There is no other problems with styles...
I have Razor Page with the following piece of code to create a link that adds a dynamic value to the route when clicked. This works perfectly.
<a class="page-link" asp-route-systemRightsPage="#(i.Index)">#i.Index</a>
Now, I have different values for the route, in this case I'm using systemRightsPage, but I also have 3 more types that I can navigate to (personsPage, personRightsPage, errorsPage), and so on.
Is it possible to use somehow a dynamic value on the asp-route-{value} part?
I tried using this:
<a class="page-link" asp-route-#(RouteValue)="#(i.Index)">#i.Index</a>
This code renders correctly in HTML to:
<a class="page-link" asp-route-personpage="2">2</a>
But the link doesn't work anymore, as nothing happens if I click it.
Has anyone done anything similar?
You can use the all-route-data attribute to pass arbitrary parameter names and their values:
#{var values = new Dictionary<string,string>{{"personpage", "2"}};}
<a asp-page="/whatever" asp-all-route-data="values">Click</a>
https://www.learnrazorpages.com/razor-pages/tag-helpers/anchor-tag-helper
I am a Laravel/web newbie, and am working with a Laravel application inherited from someone else. In the blades, I see two ways in which forms are written.
<form action="{{ route('getdata') }}"
{!! Form::open(array('url'=>'getdata','
I think in the first case, the form starts in HTML format and route alone is defined in laravel format whereas in the second case, even form is defined in Laravel format. Which is preferred and what are the differences? I tried replacing the second format with the first and got a Route [/getdata] not defined error. Laravel Version is 6.
In the first example:
<form action="{{ route('getdata') }}"
You are writing plain html, except for the {{ route('getdata') }} part, that you can read similar to <?php route('getData') ?>. Basically, just the action (the url) of the form is a PHP call to a function that will echo the url in that position, while the rest is plain html (an hardcoded text).
In the second example, {!! Form::open(array('url'=>'getdata',' you are using a Facade to access a class that will generate an html output (similar to the code from the first example ) and you are passing to the method Open() an url, that will be placed inside the generated html in the action field.
The problem that you have using the second method, is that your are not passing your route, but a string. Change it like this:
{!! Form::open(array('url'=>route('getdata'),'
To fix the error of the undefined route, you should just call name()function at the end of your route:
Route::('your_route_url','controller#method')->name('getdata');
You didn't define any route named getdata acctually.
Change your code route to url :
<form action="{{ url('getdata') }}
I use expressjs and hbs(Handlebars) as the template engine.
One of the parameters that are passed to the template when loading the page contains HTML code.
When the page loads, instead of processing the parameter and displaying the elements, it is displayed as text.
How can I solve this?
//Server-side:
let parameter = "<h2 id="how-to-use">How To Use</h2>";
//HTML:
<div id="container">{{parameter}}</div>
//--------------------------------------
//result after page load
<h2 id="how-to-use">How To Use</h2>
//Instead of
How To Use
got it.
use the triple {{{ }}} brackets
Is it possible to have a colon from a form go into a link to populate a form on a new page? At the moment the link keeps changing to %3A.
For example, current: http://www.test.com/?field=123%3A456
Trying to have: current: http://www.test.com/?field=123:456
You can't.
You need to decode the parameters
for serverside php you can use
http://php.net/manual/en/function.urldecode.php
$field = urldecode($_GET['field'])
for javascript you can use
decodeURIComponent('123%3A456');