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.
Related
In my application I have a dropdown list in the form, I want to take one value of that dropdown list from User and store it in the database. I am new to spring development and I do not know what is the datatype of the dropdown list in MySql and plus how should I configure the controller and entity class?
I have used ENUM in MySql database.
`domain` enum('Web-Development', 'Software Development','Application Development') DEFAULT NULL,
You can follow this approach followed for Dropdown:
1] Create a lookup table where you define all your consatnts with their associated type and id something like as said in this link :
Look up table.
2] Your Drop-down will display these values from lookup table as soon as a value is selected in drop down their associated id will be used in backend to store the selected value can be used to store in database rather than actual values as this id should be unique and be used for DB CRUD operations.
this might help, I have solved the issue for tempory base or for small scale development. All I have done is, I have changed datatype in MySql from ENUM to VARCHAR(45). (ENUM also works fine). And then I have manually added the dropdown values in the controller class.
#ModelAttribute("domainList")
public List<String> getCountryList() {
List<String> domainList = new ArrayList<String>();
domainList.add("Web-Development");
domainList.add("Software Development");
domainList.add("Application Development");
return domainList;
}
In my JSP file, I have called this under <form:select>, <form:option>, and <form:options>
<td>
<form:select path="domain">
<form:option value="NONE" label="Select" />
<form:options items="${domainList}" />
</form:select>
</td>
For, Data binding in Entity class I have just used return type as String and generate getters, setters and toString.
That's all I have done, it is working. Happy Coding...
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?
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>
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}'/>;">
listValue attribute of the tag is used to specify the label that should display to user when selecting that option.
Example -
<s:select name="user" list='users' listValue="username" listKey="userId"
label="Select an User" />
Is it possible to do the same when enumerating the list with an OGNL literal?
<s:select name="distance" cssClass="textBoxStyle" headerValue="Select Distance"
headerKey="" list="{'1 Km', '2 Km', '3 Km', '5 Km', '8 Km', '12 Km', '20 Km'}" />
I want the 1Km to be displayed to the user and integer 1 not string 1Km to be submitted with the form.
Instead of using a raw array like the one you have coded, you should consider getting a Map from the Action which lies behind this view. In that case, the Map in the Action will look like:
Map<String, Integer> myMap = new HashMap<>();
myMap.put("1 Km", 1);
...
and the code in the view will be:
<s:select name="distance" cssClass="textBoxStyle" headerValue="Select Distance"
headerKey="" list="myMap" />
with the keys of the map being viewed as labels, and the values as the option bodies.
You'd need to use the listKey and listValue attributes, and build an actual list of objects.
Technically you could probably do some perverse OGNL, and you could certainly do it via JavaScript and a hidden field pre-submit, but as the other answer states, you're better off doing it in the Java layer rather than the view layer.
I disagree with the other answer in that a list of objects seems more communicative, and easy to build, but from a technical standpoint there's no difference in this case, so it's more a matter of opinion.