How to set the name parameter on a multiple s:select - struts2

Imagine you have the following scenario:
a property in your action: private Entity entity;
Entity has a List<Entity2> entity2List
Entity2 has a Entity3 entity3
Now, you need an <s:select> with multiple="true" to populate entity3. This corresponds to having a request in the form entity.entity2List[n].entity3.id (n=0,1,2...depending on how many items are selected).
Question: In the JSP, what should the "name" parameter of the s:select be to achieve this?
I have tried the following without success (meaning entity.entity2List has is empty):
<s:select multiple="true" name="%{entity.entity2List[].entity3.id}" list="#someList" listKey="id" listValue="code"/>
<s:select multiple="true" name="entity.entity2List[].entity3.id" list="#someList" listKey="id" listValue="code"/>

Related

How to pass value from g:select tag

I want to pass value from the selection bar to the textarea but failed, saying description of null object is not defined.
${dbList} is a list of DB object. DB object contains a property called description.How do I make the textarea display that description property.
<g:select name="copyDB" from="${dbList}" value="copyDB"
onchange="document.getElementById("text").value=${copyDB.description}" />
<textarea id="text"></textarea>
add attr optionKey & optionValue to tag g:select
<g:select name="copyDB" from="${dbList}" value=""
optionKey="[a property of DB object]" optionValue="description"
onchange="document.getElementById("text").value=${copyDB.description}" />

jstl <select> to persist selection

I have a JSP page where am printing the arraylist content via an iterator
<s:iterator var="BeanList" value="BeanList">
<option value='<s:property value="#BeanList.simpleID"/>'>
<s:property value="#BeanList.simpleText" />
</option>
</s:iterator>
Every time the user selects an option, the form submits to the action handling. I want to be able to take the value of the clicked option, and when the page is reloaded after the submit, the same value persists in the select drop down.
Any help will be greatly apprecaited,
You are using HTML Select Tag, with values populated from Struts2 Property Tag.
No JSTL is involved.
But believe me, you can avoid this using the Struts2 Select Tag directly.
Official documentation: http://struts.apache.org/release/2.3.x/docs/select.html
In Action
#Getter #Setter List<String> allCities;
#Getter #Setter String selectedCity;
In JSP:
<s:select list="allCities"
name="selectedCity" />
Faster and cleaner than iterating manually :)
Eventually you can add an optional header value:
<s:select list="allCities"
name="selectedCity"
headerKey="-1"
headerValue="Select a City" />
For that you need to declare a variable with select box name in Action class, and put setter and getter for that. And then, when you are submitting the form, the name matches and it automatically populate in Action class.
When retrieving the data set value to same variable.Then it will populate automatically by using name.
This will happen by using params interceptor internally.

GSP- Select tag. How to achive selected="selected"

I have a select tag in my GSP file as
<g:select name="clientId" id="clientId" size = "4" from="${com.springcommunity.fleet.partymodel.roles.ClientRole.list()}" class = "filter_combo" optionKey="id" />
i want client with id 2 is selected initially (in simple html it is achived by using selected="selected")
how can i do it?
You need to specify the value attribute in this tag. http://grails.org/doc/2.0.x/ref/Tags/select.html
So in your example,
<g:select ... value="${com.springcommunity.fleet.partymodel.roles.ClientRole.get(2)}" />
One thing to be aware of here is that the value that you're selecting must be an object equal to the item in the list, and not just an id - this is where a lot of people get tripped up. So you can't just say value='2', you need to specify the object in the list that you have in your from attribute.
From the docs -
value (optional) - The current selected value that evaluates equals()
to true for one of the elements in the from list.

pass hidden field in action class

<s:iterator value="categoryList">
<s:url id="category" action="/editProduct.action"/>
<s:a href="%{category}">
<s:property value="name"/>
</s:a>
<s:hidden name = "categoryId" id = "categoryId" value = "<s:property value='name'/" />
</s:iterator
Problem:
I have a list of categories which are click able. Every category has a unique ID. Problem is that when user click on the category from list then categoryId will pass in editProduct action class
I am searching its solution from two days please help me and define the complete solution
You can't nest JSP tags like that.
<s:hidden name="categoryId" id="categoryId" value="%{name}"/>
This, however, makes no sense--if it's not a form, why would you put the categoryId into a hidden form field? You're not submitting a form, you're clicking a link.
Put the categoryId into the URL as a parameter.
<s:url id="category" action="/editProduct.action">
<s:param name="categoryId" value="%{name}"/>
</s:url>
Note that unless "name" is actually something very ID-like, it's probably a bad idea to use it as a primary key.
You may want to go over some HTML and web app basics before proceeding too much further; it will save you time in the long run.

How do I set default selected value in Struts2 <s:select> tag?

I am new in Struts2. I have user form, role column have a drop-down list. When user form is in edit mode, the stored values are placed into corresponding controls. But I can't set drop-down list by default selected value. How can I do it?
In addition to Nate's answer, I've found that I need to put apostrophes around the data in the value attribute if the key type is a String in order for it to recognize that my input value is a String.
If the value in your select tag matches a key from the list in the select tag, Struts will do the correct thing and make that value the default. Note that the types must match.
https://struts.apache.org/tag-developers/select-tag.html
To illustrate this in an example :
<s:select name="employee.course.courseId" value="3" label="%{getText('label.courseName')}" list="courses" listKey="courseId" listValue="courseName" />
Here the Employee object contains an object called "course" and it has
a property "courseId"
listKey is selected as courseId and the listValue is selected as
courseName
Hence the output will be like :
<option value="1">Computer Science</option>
<option value="2">Electronics</option>
<option value="3">Mechanical</option>
The value attribute is set to "3" and it matches the 3rd attribute in
the list, which is Mechanical
Therefore this will be the default selected value in the dropdown,
hence the output html will be like :
<option value="1">Computer Science</option>
<option value="2">Electronics</option>
<option value="3" selected="selected">Mechanical</option>
Hope this helps.
Even though you follow everything correctly and it doesn't preselect then you need to make sure that return type of the key matches.
e.g. for following list
<select onchange="showHideDefDiv('typeListId')" style="selectCss50" class="selectCss50" id="typeListId" name="definitionDiv_I">
<option value="blank"> </option>
<option selected="selected" value="definitionDiv_I">I</option>
</select>
<s:select list="%{editRulePojo.groupPojoList}" listKey="%{groupType}"
listValue="%{groupTypeValue}" value='definitionDiv_I' />
doesn't work, while
<s:select list="%{editRulePojo.groupPojoList}" listKey="%{groupType}"
listValue="%{groupTypeValue}" value='%{editRulePojo.groupType}' />
works.
From Struts2 documentation:
Note: For any of the tags that use lists (select probably being the most ubiquitous), which uses the OGNL list
notation (see the "months" example above), it should be noted that the map key created (in the months example,
the '01', '02', etc.) is typed. '1' is a char, '01' is a String, "1" is a String. This is important since if
the value returned by your "value" attribute is NOT the same type as the key in the "list" attribute, they
WILL NOT MATCH, even though their String values may be equivalent. If they don't match, nothing in your list
will be auto-selected.
Right way to do show the selected value in drop down is to create get method in your action class :
Action Class:
public String getDefaultValue() {
return "defaultVal";
}
JSP:
<s:select theme="simple" list="yourList" name="name" id="id" listKey="id" listValue="description" value="defaultValue"/>
<!--name attribute inside select tag must be a variable in action class with getter/setter -->
<!-- test variable sets the value of selected item in action class -->
<select name="test">
<!-- name attribute could be anything you want but value attribute must be a model class variable-->
<s:set name="lead_string_LS_ID" value="MasterDataModel.string_LS_ID" />
<!-- value attribute must be a list to iterate, status (an instanceof IteratorStatus will be pushed into stack upon each iteration)or result -->
<!-- var Name used to reference the value pushed into the Value Stack (my list contain leadSource_String_Id)-->
<s:iterator value="leadSource_list" status="result" var="leadSource_String_Id">
<!--#lead_string_LS_ID is value taken from <set> tag above. Note # must be in-front of the name
leadSource_String_Id is element specified in var of <iterator> tag
-->
<s:if test='(#lead_string_LS_ID.equals(leadSource_String_Id))'>
<option value="<s:property value='leadSource_String_Id'/>" selected="selected">
<s:property value="leadSource_String_Name" />
</option>
</s:if>
<s:else>
<option
value="<s:property value='leadSource_String_Id'/>">
<s:property value="leadSource_String_Name" />
</option>
</s:else>
</s:iterator>
</select>

Resources