Hardcoding of path and how to avoid it? - spring

spring-boot-starter-parent 2.3.5.RELEASE
I'm studying a video course. Thymeleaf it used. The lecturer has hardcoded a part of a path.
<a hredf="#" th:href="#{/recipe/show} + ${recipe.id}}">View</a>
Could you tell me whether such hardcoding is Ok? Why didn't they put this path to a variable and didn't pass it to the context?
Is there a way not to hardcode paths?

this kind of links are usually immutable.
like for showing receipts id.
and hard coding them aint a problem.
but if your path can be change, you can change use a variable like what you have done:
${recipe.id}
if you had another variable section in your path you could use variable for them too.
<a hredf="#" th:href="#{/recipe/} + ${actionName} + ${recipe.id}">View</a>
There is some kind of not to hard coding in Spring (JSF) to use ControllerName and Action Name as route name like this:
<h:commandButton action = "#{receiptController.show}" />
or:
<h:commandLink action = "#{receiptController.show}" value = "link text">
<f:param name = "receiptId" value = "${receipt.id}" />
</h:commandLink>
According to : https://www.thymeleaf.org/doc/articles/standardurlsyntax.html
thymeleaf has not implement This Feature.
you can only pass variables to it.
so you can have an static class of Constant url map.

Related

Thymeleaf custom dialect - nested attributes

I would like to ask if it is possible to have nested attributes in HTML tag which can reuse result of previous one. For example
<p custom:one="some text to process" custom:two="process result of custom:one">
where custom:one can be used standalone but custom:two have to be used with custom:one. The final result will be produced by custom:two
if I got you right, you can do it with local variables
You need to specify th:with to declare a variable.
Note that the declared variable is available within the element.
<div th:with="newValue = 'Hello ' + ${val}">
<span th:text="${val}">One</span>
<span th:text="${newValue}>Two</span>
</div>
Let me know if that's what you're looking for.

pass thymeleaf variable as parameter in method

I concatenate the state and my stateCounter to one String. This is my value for my HashMap which stores all the States but I don't get it to to put this variable in my method.
<span th:with="stateName=${item.state} + ${item.stateCounter}"></span>
<td th:text="${{order.getStateRepository().get(${stateName})}}"></td>
Some notes:
(1) Using a <td> tag suggests you are also using a <table>. Having a <span> tag next to a <td> tag inside a table is not valid HTML (assuming this is what it looks like in your template - maybe this is just a copy/paste thing).
(2) If your order object has a stateRepository field, then you don't need to use a getter order.getStateRepository(). You can just use the field name order.stateRepository. You are already doing this with item.state, for example. Thymeleaf will figure out from the field name how to use the related getter - e.g. item.getState().
(3) The scope (availability/visibility) of a local variable, such as stateName in th:with="stateName=${item.state} is limited to the tag in which it is declared, and also to any sub-tags. You do not have any sub-tags in your <span> - therefore the variable is not visible anywhere outside of your span. So, it is not available inside the <td> tag.
(4) Do you need to use a local variable in your example?
Instead of using get(stateName), you can use the expression referred to by stateName directly:
get(item.state + item.stateCounter)
So overall, this should work (based on the assumptions above):
<td th:text="${order.stateRepository.get(item.state + item.stateCounter)}"></td>
Maybe you do need the local variable, of course. It may depend on the wider context of the Thymeleaf template.
You are trying to use local variable. It will work if you try as follows:
<div th:with="stateName=${item.state} + ${item.stateCounter}">
<td th:text="${order.getStateRepository().get(stateName)}"></td>
</div>

Spring Freemarker Form Bind : Problem with Exponent Value

I have Spring bind form with freemarker. But large number show Exponent value. How to show value without Exponent.
For small numeber...
<#spring.formInput 'CaseMaster.year' 'placeholder="e.g. 2013" ' 'number'/>
For large number...
<#spring.formInput 'CaseMaster.suitValue' 'placeholder="e.g. Suit Value" ' />
HTML View:
I wnat to show value like 80000000 or 80000000.00 or like 8,00,00,000.00 in the second field.
I've dug down into the macro and it looks like you have no control over the formatting to the populated value. Perhaps there's a more elegant solution, but at this point, you might try creating your own macro that gives you formatting control. (This is untested.)
<#macro numberInput path attributes="">
<#spring.bind path/>
<input type="text"
id="${status.expression?replace('[','')?replace(']','')}"
name="${status.expression}" value="status.value?c" ${attributes}<#closeTag/>
</#macro>
You would call it like this:
<#numberInput 'CaseMaster.suitValue' 'placeholder="e.g. Suit Value" ' />

filter_input behavior for integer input tag name

I have form inputs that have their name attribute in Integer like below
<input type="hidden" name="100351312" value="test" />
If I use
echo filter_input( INPUT_POST, '100351312' )
It returns NULL
Whereas
echo $_POST['100351312'] prints the value correctly.
filter_input really needs three inputs; the type, the variable (both of which you have) and the filter, which you don't have. Here are some types of filters: http://php.net/manual/en/filter.filters.php
That said, if you're using WordPress you can almost always find a WordPress helper function you can use instead of filter_input()

Why is #Html.Label() removing some characters

When I use the following code in my razor view it renders <label for=""> someText</label> and not <label for="">1. someText</label> but I can't figure out why 1. is removed while rendering.
#Html.Label(String.Format("{0}. someText",1))
Edit:
The following code renders <label for="">1# someText</label> as expected.
#Html.Label(String.Format("{0}# someText",1))
You are misusing the Html.Label method. It is for:
Returns an HTML label element and the property name of the property
that is represented by the specified expression.
That's why it gets confused if you have a point . in the first parameter because it expects a property expression there.
However, you can use the second overload:
#Html.Label("", String.Format("{0}. someText",1))
Or just write out the HTML:
<label>#String.Format("{0}. someText", 1)</label>
You can avoid using the "Html Helper's label" and directly use html "label" and place whatever you want to display correctly. It can also save some time ;)
The syntax which you are using is wrong or We can say that this is not a way to use property with RAZOR syntax.
You ca use this that may be help full for you.
**
#Html.LabelFor(model => model.PropertyName,
String.Format("{0}. " + #Model.PropertyName.ToString() + ",1))
**
I was using this for a data table that contained a double (Lat/Long) and saw this same problem. Thanks for the tips (I am not allowed to comment).
For me, the problem was solved ..
#foreach (var cell in item.ItemArray)
{
<td>
#Html.Label("",cell.ToString().Trim())
</td>
}

Resources