I want to know how to display a different value in struts 2 select.
eg: i want to put Jan,Feb as month where the value should pass 1,2 respectively.
if anybody aware of this please let me know
thank you.
See the following example (from the Struts 2.1.8 API doc) :
<s:select label="Months"
name="months"
headerKey="-1" headerValue="Select Month"
list="#{'01':'Jan', '02':'Feb', [...]}"
value="selectedMonth"
required="true"
/>
The list attribute contains a map where the key is the value that will be sent and the value is the value that will be displayed.
Of course, months are static, but you could use a list of domain objects or whatever beans you need. In this case the list should be stored, typically as a field of your action class. Then you will refer to the list or map :
<s:select label="User"
name="users"
headerKey="-1" headerValue="Select User"
list="users"
value="selectedUser"
required="true"
/>
In this case your action will contain a map with the usernames and their ids and a getter for it : getUsers().
If the getUsers() method of your action returns a list of User objects, and the User class has at least (let's assume) the id and username fields, you will have to specify which field to use for value to be passed and which field to use for display in the select. This is done with the listKey and listValue attributes of the select tag :
<s:select label="User"
name="users"
headerKey="-1" headerValue="Select User"
list="users"
listKey="id"
listValue="username"
value="selectedUser"
required="true"
/>
Related
I am trying to write a control that takes a list of names, allows the user to select from this list and then posts back the username for the name that has been selected.
Firstly, I used a Syncfusion ejDropDownList control to accomplish this, where the code I used was:
<ej-drop-down-list id="UserList" datasource="(IEnumerable<User>)ViewBag.Users" ej-for="Username">
<e-drop-down-list-fields text="Name" value="Username" />
</ej-drop-down-list>
This works fine and when I submit the form, the Username field is bound correctly in the associated model.
Since there are too many names in the list to realistically use a drop-down list, an autocomplete control is preferable.
However, when I implemented the control in the following manner:
<ej-autocomplete id="UserList" datasource="(IEnumerable<User>)ViewBag.Users" ej-for="Username" filter-type="Contains">
<e-autocomplete-fields text="Name" value="Username" />
</ej-autocomplete>
Then the 'Username' value in the model is bound to the 'Name' field.
Is anyone familiar with Syncfusion controls who can tell me how I can bind the selected value (as opposed to the selected text) to the 'Username' column successfully?
Thanks,
Sean
Syncfusion Autocomplete not have a field type of value. You can also use Key field for your scenario.<ej-autocomplete id="UserList" datasource="(IEnumerable<User>)ViewBag.Users" ej-for="Username" filter-type="Contains">
<e-autocomplete-fields text="Name" key="Username" />
</ej-autocomplete>
refer the documentation link: https://help.syncfusion.com/aspnetmvc/autocomplete/data-binding
I'm developing a Liferay portlet using spring MVC.
In the view part I have a dropdown field like that:
<form:select path="addressUsage">
<option>Home address</option>
<option>Postal address</option>
</form:select>
We know that in this case if the user select for example the first option so the value that the view will pass to the controller is "Home address" (in the attribute addressUsage of the corresponding class)
But what I want is that the dropdown is displayed with "Home address" and "Postal address" options and what will pass to the controller is:
-> "HOME" in the case the user select "Home address" option.
-> "POSTAL" in the case the user select "Postal address" option
So I thought that I add a name attribute to the option tag. so the dropdown will be like that
<form:select path="addressUsage">
<option name="HOME">Home address</option>
<option name="POSTAL">Postal address</option>
</form:select>
So my question: is it possible to pass the name attribute of the corresponding selected option instead of the option text through the path attribute?
I think what you're looking for is the value attribute.
<option value="HOME">Home address</option>, for example.
You can see here the value attribute of the option tag's Definition and Usage -
The value attribute specifies the value to be sent to a server when a
form is submitted.
The content between the opening and closing tags is
what the browsers will display in a drop-down list. However, the value
of the value attribute is what will be sent to the server when a form
is submitted.
Note: If the value attribute is not specified, the content will be
passed as a value instead.
I am new to rails and haven't really done to much with data outside of the model.
I have a form that references a table controller for files. I want to add an dropdown that will display a list of projects from a project table for the user to assign a the file to a project if they want too. Assigning a project is not a requirement. The file can be unassigned to a project. I do have a project_id column in the file table for those projects assigned but it is allowed to be null and I did not build a relationship because I need to have cases where there are none.
Can someone please tell me how to do this?
When they evaluate the file, the screen posts back with a save or update button depending if it has already been saved in the database.
At the same time as the save and update pop up on the right I want to display a list box of the projects with an assign project button. If new just a list, if update either just a list because not assigned or display the selected value in the list if already assigned, while allowing them to change it from the list if desired.
In the file controller method that posts back to the UI I have this code:
#file_alias_filedata = FileAliasFiledata.all
#projects = Project.all
for update
#projects = Project.find(params[:id])
In the form I have this code:
<p> <label> Select Project to Assign:</label> <br />
<%= select_tag 'projects', (#projects.present? ? options_for_select(#projects, #selected_project) : []) %> </p>
The form runs but I get this in the dropdown box:
#<Project:0x))7ff531ab4518>
Can someone please help me figure out how to accomplish my task and why I see the strange box value?
What am I doing wrong?
Thank you for your help!
Assigning a project is not a requirement. The file can be unassigned
to a project. I do have a project_id column in the file table for
those projects assigned but it is allowed to be null
From the docs:
belongs_to(name, scope = nil, options = {}) public
Specifies a
one-to-one association with another class. This method should only be
used if this class contains the foreign key. If the other class
contains the foreign key, then you should use has_one instead. See
also ActiveRecord::Associations::ClassMethods’s overview on when to
use has_one and when to use belongs_to.
Methods will be added for retrieval and query for a single associated
object, for which this object holds an id:
association(force_reload = false)
Returns the associated object. nil is returned if none is found.
Likewise,
has_many(name, scope = nil, options = {}, &extension) public
Specifies
a one-to-many association. The following methods for retrieval and
query of collections of associated objects will be added:
collection(force_reload = false) Returns an array of all the
associated objects. An empty array is returned if none are found.
But belongs_to() and has_many() are supposed to make things more convenient for you. You certainly do not have to use them.
Next,
and why I see the strange box value? What am I doing wrong?
You see the strange value for the same reason the following two loops display different things:
class Dog
attr_reader :name
def initialize(name)
#name = name
end
end
#dogs = [
Dog.new("Sam"),
Dog.new("Betty"),
Dog.new("Pete"),
]
#dogs.each {|dog| puts dog}
#dog_names = #dogs.map {|dog| dog.name }
#dog_names.each {|dog_name| puts dog_name}
--output:--
#<Dog:0x0000010099a308>
#<Dog:0x0000010099a2b8>
#<Dog:0x0000010099a268>
Sam
Betty
Pete
You will see the same result if you do something like the following in a view:
<div>
<%= select_tag "dog", options_for_select(#dogs) %>
</div>
<div>
<%= select_tag "dog_name", options_for_select(#dog_names) %>
</div>
If you read the docs here:
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select
...you will see several examples, which should make it clear that options_for_select() takes an argument that is:
An array of Strings.
A Hash where both the keys and values are Strings.
An enumerable that iterates over some Strings.
etc.
Do you see a pattern? It's Strings! options_for_select() needs an argument that consists of Strings. If the argument is not a collection of Strings, e.g. an array of project objects, then options_for_select() tries to convert the objects to strings by calling to_s() on the objects. And the default to_s() method is Object#to_s() which is inherited by all objects and produces a string containing the class name and the object_id.
I am also new to rails, but I think you can try using the options_from_collection_for_select method.
<%= select_tag :search_state, options_from_collection_for_select(State.find(:all, :select => :name), :name, :name) %>
Hope this help. Cause it certainly helped me.
I have a problem in my web app with struts 2 and a date value. I've got a form and inside it an s:textfield that shows a date value. I read this value from request, and the problem is that the name of that value is in other variable.
The Action I'm calling does this:
...
public String execute(){ return SUCCESS;}
public MyObject getObject1(){
MyObject result = new MyObject();
result.setDate(new java.util.Date());
return result;
}
...
On SUCCESS it goes to my form.
The code in my form (what I was wondering to write) is:
<s:form action="save">
<s:set name="objName" value="object1"></s:set>
<jsp:include page="../includedform.jsp"></jsp:include>
</s:form>
And in the "includedform.jsp" there is:
<s:textfield name="%{objName}.date" label="Date" >
<s:param name="value">
<s:date name="%{%{objName}.date}" format="dd/MM/yyyy" />
</s:param>
</s:textfield>
The syntax:
%{%{objName}.anagrafica.dataNascita}
doesn't work (it shows nothing, obviously). So i'll need something like this but working :)
Other tags s:textfield inside "includedform.jsp" (ommited in the code bellow for simplicity) without date fields are working, because I am using only the name attribute and struts looks automatically for the value. This is the code I use for these textfields:
<s:textfield name="%{objName}.name" label="Name"/>
your question is very confusing and you need to rephrase to make it more clear and readable.
i am not sure why you are doing this
<s:textfield name="%{objName}.date" label="Date" >
while this can be done like
<s:textfield name="objName.date" label="Date" > OR
<s:textfield name="%{objName.date}" label="Date" >
when you write objName.date OGNL assume that you have a bean in your action class namely objName and this bean has property namely date, s ultimately this will get converted to
getObjName().getDate() by ONGL
On a similar fashion <s:date name="%{%{objName}.date}" format="dd/MM/yyyy" /> datee tag works
For more details please refer to the official doc
Struts2 Date tag
There is one solution which i tried and it worked :)
1)Keep the column in which you want to store date in database's table in "DATE" data type only.
2)Use only Textfield tag in JSP page.Dont use Date tag.
Note:Make sure that you make user input date in YYYY-MM-DD format only by placing a placeholder in textfield tag !
3)Keep the variable for accessing that field of string type only.Dont use DATE data type.
4)Now run Query: Select DATE_FORMAT(datecolumnname,'%d/%m%/Y') as date_two from tablename; and you will get date from that columnin dd/mm/yyyy format even if it is stored in YYYY-MM-DD format in table .
5)And you can also compare your datecolumn's data with curdate() and related functions as well and it works :) .
6)Like i used query: Select id,name,DATE_FORMAT(datecolumnname,'%d/%m/%Y') as datecolumn2 from tablename where datecolumnname >= curdate();
I have a list of objects in struts2 select tag. i want to retrieve the selected list object, not just id or any property as none of them or unique property, is there any way to get whole object? my code goes as below i want to retrieve all the fields, name,id,date,cba, seq.. as none of them are unique fields, i can not just get the id, then get the other fields from database.
any help???
<s:select name="appls" list="applList" id="appls"
listKey="id"
listValue="%{enroller.name.fullName + '-' + enroller.cba+'......' + openDateStr + '.....' + seq}"
multiple="true" />
What do you mean, "get the whole object" or "retrieve the list object"?
In any case, IMO you should build complicated option labels in Java, not in the view.
listValue can be an arbitrary OGNL expression, using the same value stack as the page itself, plus each of the select's list items pushed onto the top. Without knowing more about what you're actually trying to do, it's difficult to help much beyond that.