Set default value of select in thymeleaf - java

I'm using Spring boot + thymeleaf and I'm trying to set the default value of a select element in order to show the selected object that is stored in database (edit form).
To do that, from the controller, I insert into the model the entity with its values, but, when I try to set the default value of the select, it always gets the first option.
This is the code:
<select class="selectpicker"
id="alarmPeriod" name="alarmPeriod"
th:selected="${alarm.alarmPeriod}"
th:value="${alarm.alarmPeriod}">
<option th:each="period:${periods}"
th:value="${period}" th:text="${period}">
</option></select>
I have tried with th:field="*{alarm.alarmPeriod}" but the thymeleaf processor crash.
How could I set the default value of the select with my stored entity value?
PD: alarm is my entity and alarmPeriod is an attribute of alarm.

selected is an attribute for the option tag. Since you're trying to mark one of the options as selected, you must add the th:selected attribute to your options, with an expression that will evaluate to true or false depending on your alarmPeriod.

The selected option should be put as an option like you would do with with an htnl select tag, the following piece of code should work
<select class="selectpicker"
id="alarmPeriod" name="alarmPeriod">
<option value="${alarm.alarmPeriod}" selected="selected">
${alarm.alarmPeriod}
</option>
<option th:each="period:${periods}"
th:value="${period}" th:text="${period}">
</option></select>

Related

How to auto select drop down box value in struts?

I am loading java list values in my drop down box.Now i just want to auto select one value from the list by default.we may introduce logic for this either in java side or jsp side.
Here is my JSP part..
<label class="labelStyles" for="planAction">Plan Action:</label>
<html:select styleId="planAction" name="PAMaintenanceProblemProgramForm"
property="planAction">
<html:options collection="validPlanAction" property="code" labelProperty="desc"/>
</html:select>
Java Code is
session.setAttribute("validPlanAction",delegate.getPlanActionList());
How do i make this as auto select by default?

Struts 2 - Setting the label attribute for options in a select tag

For legacy code support reasons, I need to create a select tag in struts 2 and also need the label attribute in the resulting option list to match the value. An example probably will explain it better.
The generated code should look something like this:
<select>
<option label="Darth Vader">Darth Vader</option>
<option label="Luke Skywalker">Luke Skywalker</option>
<!-- etc -->
</select>
My struts markup is as follows:
<s:select headerKey="-1" headerValue="" list="users" size="4" multiple="true"
tabindex="2" listKey="key" listValue="value"/>
The "users" collection is of type Map. Essentially, I want the value of each entry of the map to also be the label attribute for the option elements that struts creates. I've tried using the listLabelKey and label attributes provided by Strut's select tag with no luck. Is there an easy way to accomplish this that I'm just missing?
EDIT
I have some legacy javascript code that dynamically selects/unselects dropdown elements and relies on the label attribute on option being explicitly filled in. Using the listKey and listValue attributes simply generates the following HTML.
<select>
<option>Darth Vader</option>
<option>Luke Skywalker</option>
</select>

getting disabled true data from jsp to struts2 action class

I have disabled=true textboxes and combox boxes in my jsp.
When I try to map those value back to action, they disappear.
I don't want to call to DB again.
How can I set those disabled=true textboxes and combo boxes's data value into hidden values?
Thanks ahead.
The property of disabled elements' value not being submitted with the form is not an issue with struts2, but an HTML behavior. To handle this behavior, use the following implementations:
Use readonly="readonly" attribute to prevent modification rather than using disabled. (This won't be available for few elements)
Use onfocus="return false" to prevent any focus on html elements. This will prevent the modification of their value. You can use CSS to make these elements look like readonly or disabled.
Before you disable an element, create a hidden element and attach the value of the element you are about to disable. This will make sure that the item gets submitted.
See the following implementation for select element. You may make use of the id attribute of s:select to set the html id of select element.
<select id="demoSelect" class="readonly">
<option value="0">A</option>
<option value="1">B</option>
<option value="2" selected="selected">C</option>
<option value="3">D</option>
<option value="4">E</option>
<option value="5">F</option>
</select>
<input type="hidden" value="2" name="demoSelectDefault"/>
jQuery:
$(document).ready(
function() {
$("select.readonly").live('change', function() { //live() makes sure that this is executed if you apply the class to the element even after the initial load. So, if you set the readonly class to a select element, you are done.
var selectElement = this;
$("input[type=hidden][name=" + this.id + "Default]").each( //This is implemented in case of multiple select support. You will need to select nothing at first and then make this select each of this element
function() {
selectElement.value = this.value;
}
);
});
}
);
Here, when you implement this using struts, populate the s:select, then use an s:hidden element to generate a corresponding default value.

playframework: dropdownlist selected value

How to pass a value from one dropdown to a second dropdown.
Example:
Template
#{select 'pl'}
#{list platforms, as: 'platform'}
#{option}${platform.description} #{/option}
#{/list}
#{/select}
#{select 'pl'}
#{list cdrs, as:'cdr'}
#{option}${cdr.description} #{/option}
#{/lisst}
#{/select}
If you mean using Javascript to "chain" two selects like Country/City, what I did was moving the code for the second select to its own view (e.g. Cities/selectCities.html)
#{select 'city', items: cities, valueProperty: 'id', labelProperty: 'name' /}
and use an include in the view where I'll have both chained selects
<select name="country" id="select-country">
<option value="ES">Spain</option>
<option value="US">United States</option>
</select>
<span id="select-city">
#{include 'Cities/selectCities.html' /}
</span>
now some javascript in that same view to reaload second select in case first select changes
$('#select-country').change(function() {
var selectAction = #{jsAction #reloadCities(':country') /};
$('#select-cities').load(selectAction({country: $(this).val()}));
});
and in the controller we have our reaload cities method rendering only the second select again
public static void reloadCities(String country) {
List<City> cities = City.find("byCountryCode", country).fetch();
render("#selectCities", cities);
}
and that's about it, for me it's working using play 1.2.5
Your question is not really clear but if I understand it right, you want to change a select depending on the value of the selected item in the first select. That is impossible as it is a runtime animation.
You will have to take a look at Ajax/Javascript in order to do things like that.
If you mean once is rendered in the browser, you must use Javascript.
Otherwise, use the value keyword as explained in the documentation (here)

stringtemplate: problem generating html select with pre-selection

I am trying to generate a html select element with one option preselected. I am unable to think of a way to do this with stringtemplate.
If user.choice is set to "B" then I want to print an html select element with option B preselected.
user.choice = "B";
StringTemplate myPage = group.getInstanceOf(....);
myPage.setAttribute("user", user);
on printing the template should generate:
<select>
<option value="A" >A Selected</option>
<option value="B" SELECTED >B Selected</option>
<option value="C" >C Selected</option>
<option value="D" >D Selected</option>
</select>
Can someone tell me how to write the template for doing this. The number of choices (A,B...) is fixed (known at time of writing template).
This is a pretty common requirement when generating html pages for websites. But nothing like a comparison operation for passed values seems to be available in stringtemplate. Am I missing something obvious?
I am using stringtemplate group (.stg) files so solutions that have templates referencing other templates are fine. Using stringtemplate 3.2.1 in java. Using "$" delimiter instead of the now default "<>" to make html generation easier.
StringTemplate enforces a very strict separation between your view and model. It doesn't support doing conditional operations on anything other than boolean values. I think the engine really wants you to have done the computation before you pass the data off to be rendered.
I'd suggest storing the value with the actual list items themselves. Say you already have a "value" and a "text" property on the "list" object (which are stored in your collection), you could add a selected boolean property to the list item as well. You could then use it as follows:
<select>
$list:{ l |
<option value=$l.value$ $if(l.selected)$selected="selected"$endif$>$l.text$</option>
}$
</select>

Categories