Struts 1: How to set <html:multibox /> value using form bean? - java

I would like to have the value of my multibox to be set dynamically from the form. I'm using a <display:table /> tag to show a list in my form in a table however, I have checboxes on every row on the table which I would like the value and disabled attributes set depending on the object in the list that corresponds with that row in the table. This is what I'm currently doing.
<display:table name="sessionScope.SearchForm.companyDevices" requestURI="my/action.jspa">
<display:column>
<html:multibox property="selectedDevices"
value="${macAddress}" <!-- HERE -->
disabled="${isAssigned}"/> <!-- AND HERE -->
</display:column>
<display:column property="macAddress" title="Mac Address" />
<display:column property="vendor" title="Vendor"/>
<display:column property="model" title="Model"/>
<display:column property="deviceStatus" title="Device Status" />
</display:table>
As you can see a column property uses the same macAddress bean value and it displays the macAddress there successfully, however in the multibox it doesn't set the value to the macAddress for some reason. Same goes for the disabled attribute.
I can't seem to identify what is wrong. How do I set dynamic values for multiboxes in a display:table?

I figured out a way. I replaced the multibox tag above with
<display:table name="sessionScope.SearchForm.companyDevices" requestURI="my/action.jspa" id="device"> <!-- ID ATTRIBUTE ADDED -->
<html:multibox property="selectedDevices"><bean:write name="searchForm" property="companyDevices[${row_rowNum - 1}].macAddress" /></html:multibox>
rowNum is an implicitly created variable in struts and to retrieve the row Number for a particular row. To identify a row an Id needs to be assigned. By setting the id to 'device' in the display:table I use device_rowNum (the implicitly created variable from the combination of my the id attribute and rowNum) get a row's particular number which is associated with its position in the list to be able to retrieve the value that I want.

Related

What is the Struts2 equivalent for <bean:message> tag with property attribute in Struts1? For example:<bean:message name="user" property="label" />

In Struts 1 the <bean:message> tag is usually used with the key attribute in order to get the message from the properties file by specifying the message-key in the key attribute.
And in struts 2 we do that by using either <s:text> or <s:property> tags.
Consider the below example:
In struts 1,
<bean:message key="user.name.required"/>
The equivalent in Struts 2 is,
<s:text name="user.name.required" /> or
<s:property value="getText('user.name.required')"/> where,
user.name.required = Name is required
is the content of the properties file.
But I'm confused with a different scenario.
<bean:message name="user" property="label" />
for this, I tried <s:property value="getText('label')"/>
but the value returned is the string "label" instead of the message string "User data is missing"
and I also tried <s:property value="label"/>
But the value returned is message key i.e. "user.data.missing" instead of the message string "User data is missing"
<bean:message name="user" property="label" /> in Struts 1 returns the message User data is missing.
where,
user.data.missing=User data is missing
is the content of the properties file.
My question is how do I achieve this in Struts 2?
Try the following code
<s:property value="getText(label)"/>
where label is a variable in the value stack which contains a message key i.e. "user.data.missing" .

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>

Fetching value for display:column title in Struts 2

I am working on a display table version 1.2 on Struts 2 application. I am having problem with fetching value for "titleKey" in my display table column.
<display:column property="propertyName" titleKey="common.property" />
But here Title key is not able to display the value from properties file.
common.property is available in application resources and if I do
<s:text name="common.property"/>
It properly displays the value.
You can use EL expression for the title of the column. Before that the text for the title could be set to the value stack. EL expressions in Struts2 have access to the value stack when searching for variables.
<s:set var="commonProperty" value="%{getText('common.property')}"/>
<display:column property="propertyName" title="${commonProperty}" />

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.

Retrieving inputvalue from JSF-repeat

I have recently come across a question involving JSF and Javabeans.
To get a value from an input you need a declared property with getter and setter in the bean. Now I am trying to generate a list of entities to edit a value (in this code example called number), but how can i retrieve these values (as the ID of h:inputText is empty at this point)?
I have tried to name this ID "name_#{bean.id}", but there is no way to set this value in the bean.
Any help would be appreciated!
index.xhtml
<ui:repeat var="entity" value="#{bean.getEntities()}">
<p>
Value:
<h:inputText id="" value="#{entity.number}" />
</p>
</ui:repeat>
Bean.java
public List<Entity> getEntities() {
return entities;
}
You don't need the client id of the inputText to get the values to the server. As stated in Daniel's comment changes are saved if you submit the surrounding form.
You don't even need to set the id parameter. JSF does it for you.
But your value attribute is not correct. It needs to be
value="#{bean.entities}"

Categories