How to auto select drop down box value in struts? - java

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?

Related

Set default value of select in thymeleaf

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>

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>

Using Struts tag within HTML tag for hiding/displaying table

I want to hide a table on load of a page and hide it on click of a button on that page.I have created a property of type String in Action class named as "displayTablle" and assigned it a value "none" by default.So that when this page is opened by calling action,this property will be none and following code used in table tag should hide the table:
<table border="true" id="dataTable" style="display:"<s:hidden id="disTable" name = "displayTable" value="%{displayTable}"/>;">
<s:submit value="Fetch Data" align="center" action="displayDataAction" />
Then on click of Fetch Data button, I am setting value of this property to blank string i.e " ", so that table will be displayed, But I am stuck with the syntax and <s:hidden> is not getting bound properly, as when I open the page, last part of the table tag's code i.e ;"> is getting printed as is.
Can anybody suggest, what should be the right syntax to bind s:hidden in html table tag? Can we do it like this?
The property tag is used to write text to the JSP page. It has also option for unescaping that text, but it's not required in your case.
<table border="true" id="dataTable" style="display:<s:property value='%{displayTable}'/>;">

Struts 2 - Adding an OptGroup to a <s:select>

So I have a <s:select> tag which is dynamically populated by a returned map of type List<folder> from a connected class. I want to add a header (optgroup) at two points within the select but can't work out how to build upon the already implemented system.
My select is as follows:
<s:select id="%{uid}_folder" theme="simple" list="folderList"
listKey="id" listValue="title" value="folderId" />
So as you can see it is populated by using a map to declare the value and key of the select from the key and value of the map.
The map is created through a long process collecting different values from different tables.
I just want to add two optgroup(s) were appropriate. I have gone down to the map generation and can add info at the appropriate place to add say, <optgoup> but the HTML is escaped (which we need)
Please ask if any additional info is required, I am struggling to phrase this question properly.
If you want to add a header to the select tag you can do it with headerKey and headerValue attributes.
<s:select id="%{uid}_folder" theme="simple" list="folderList" listKey="id"
listValue="title" value="folderId" headerKey="-1" headerValue="Select Folder" />
The select tag doesn't have optgroup generated elements.

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.

Categories