How to get both value and label of selected Item - spring

My code on jsp :
<form:select id="accountId" path="accountId" >
<c:forEach items="${accountTypeList}" var="var">
<form:option value="${var.accountId}" label="${var.accountNo}"></form:option>
</c:forEach>
</form:select>
I want both value and label of selected entry in controller.

Please Check Get text of select option's label and populate it in a hidden input field
you can use a hidden field and fill that field with javascript/jquery so when you submit that form you can get that value.
--update
if you still use accountNo in next page, you can add some temporary variable in your form class like accountNo2.

Related

How to bind List<String> values to <select> tag in jsp without comma separated values?

First things first-
My Class-
public class StakeHolder{
private String stakeHolderName;
private Date startDate;
private Date endDate;
}
My Controller Request Mapping-
#RequestMapping(value = { "/add" }, method = RequestMethod.GET)
public String addGet(Model model) {
StakeHolder stakeholderObj = new StakeHolder();
//To be selected in drop-down for 'stakeHolderName' attribute of StakeHolder
List<String> organizationList = stakeholderObjService
.getApplicantOrganizations();
model.addAttribute("orgList", organizationList);
model.addAttribute(STAKEHOLDER_OBJ_STRING, stakeholderObj);
return STAKEHOLDER_ADD_VIEW;
}
My JSP code for drop-down -
<form:select path="stakeHolderName" name="stakeHolderSelect"
id="stakeHolderSelect" style="width:220px;" items="${orgList}" >
When i submit the form with any value from drop down I have a server-side validator to verify all the values of attributes. When there is an error in date format it returns to the same page. When the data is correct and submitted again the dropdown value gets binded to my class's 'stakeHolderName' attribute in comma separated format which is not required.
its something like
StakeHolder [stakeHolderName=,TestOrg1,TestOrg1,TestOrg1, startDate=null, endDate=null]
The original values keeps getting appended to the name each and every time it get submitted with a preceding comma. How can I get the value "TestOrg1" just once without any comma?
Appreciate the help.
try to change your drop down code to this in JSP
<form:select path="stakeHolderName">
<form:options items="${orgList}" />
</form:select>
thank you
Sorry for posting the solution so late.
The problem was found elsewhere. It was actually on the jsp. The select option had a prior radio button selection to it and on its click event i had to show a pre-filled input text box with default value for the same path variable. So when i submitted the form, it binded the value to the input text as comma separated values.
I couldn't find a solution for it through spring path variable. So i used normal html input boxes with different names and binded those values through init binder.

Path attribute in form:select - spring 2.5 JSP

I've been put on the task of learning spring (2.5), and I've got into a problem using the attribute where, when the form is loaded, I wont get any pre-selected values. So the situation is as follows:
In my system I have Companies, Customers and Users. Customer extends Company, and a Company can have a List of Users (the getMethod is public so hence Customer also have a List of Users).
So this is how the form select looks in my .JSP:
<form:select multiple="true" id="selectAccountManager" path="${customer.users}" cssClass="input select_img" >
<c:forEach items="${customerUsers}" var="user" >
<form:option value="${user.id}
<c:out value="${user.displayName}" />
</form:option
</c:forEach>
</form:select>
Right now, I get an error on the path="${customer.users}". If I use path="users" my system works, but then I wont get any of the values within the form as "pre-selected" when the page loads. I've tried with path="customer.users" but when I do this I get a referenceError in js-console.
The .JSP is mapped to an EditCustomerController where customerUsers is put into a map by
map.put("customerUsers", UserControlHelper.getAllUsers());
So I guess this is where the problem lies as I always get all users from the system?
TL;DR: How do I set selected values on a form where I load the items from one class, and want the select-filter to come from another?
(First of all, Spring 2.5 is old and I suggest you to use 3.2.x or even 4.0.x)
To pre-select option you should set appropriate field on the model and Spring will pre-select it automagically. Like that:
<form:form method="post" modelAttribute="myForm">
<form:select multiple="true" path="users">
<form:options items="${customerUsers}" itemLabel="displayName" itemValue="id" />
</form:select>
</form:form>
And in the controller:
User defaultUser = new User(1, "DEFAULT");
MyForm myForm = new myForm();
myForm.getUsers().add(defaultUser);
model.addAttribute("myForm", myForm);
So I found out what the problem was.
When I created my map I only put the users in there. What the User-object actually was mapped upon was the id of the user (something I retrieved through ${user.id} as can be seen in the originalpost. What I had to do was to create the path in a correct way as such:
Controller - java:
Map<Int,String> users = new HashMap<Int,String>();
for(User user: UserControlHelper.getAllUsers()){
users.add(user.getId(), user.getDisplayName())
}
model.put("customerUsers", users);
and my jsp:
<form:select path="users" items="${customerUsers}"></form:select>

how to bind select option value in spring mvc

I want to add value in database by select box option.and all is working good.but when i select value from the select box.selected value is added into database with new ID and skillCategory.
i never want to add it with new Id. so can someone tell me .what should i do
my jsp code :
<form:select id="skillCat" path="skillcategory.skillName">
<c:forEach var="cate" items="${catagory}">
<form:option value="${cate.skillcategoryId}">${cate.skillName}</form:option>
</c:forEach>
</form:select>
my controller code for skillCategory :
private void prepareDataForWalkin(ModelMap map) {
map.addAttribute("catagory", skillService.getSkillList());
}
You are indeed passing ${cate.skillcategoryId} to skillcategory.skillName by doing
<form:option value="${cate.skillcategoryId}">${cate.skillName}</form:option>
If you want some other value instead of id to be passed, put that into to <form:option>'s "value" attribute instead.
For example
<form:option value="${cate.skillName}">${cate.skillName}</form:option>
passes value of ${cate.skillName} to your command variable skillcategory.skillName.

Post values to controller from table rows that are created dynamically

I am using codeigniter for a project.
I have a table with rows added dynamically through jquery append.
So when I click on a button submit, it post values for all the rows values to the controller, but I am not sure how to do that.
My table row is like this
<td>
<input type="text" class="createWOBlockBG large ui-autocomplete-input" name="basefabrics" id="basefabrics1" autocomplete="off">
</td>
I do not want to use ajax calls.. How do I pass many of the inputs from the view to the controller?Thanks in advance for the help.
If your table with rows and as mentioned submit button is inside a form
then specify action attribute of your form to the controller where you want to access your values
and in the controller you can get the values as follows
$value = $this->input->post('name_of_your_input');
considering your input
$base_fabrics = $this->input->post('basefabrics');
if your form action is calling the same controller method which loaded the view, you can check for the click of submit button and then fetch values
if ($this->input->post('name_of_submit_btn'))
{
$base_fabrics = $this->input->post('basefabrics');
}
EDIT: if all your inputs have same name then better name it as name="basefabrics[]"
so that when you fetch the value
$base_fabrics = $this->input->post('basefabrics');
$base_fabrics will be an array

Firefox is caching hidden inputs even with autocomplete turned off

I have a form with the autocomplete attribute set to off.
Inside this form, there is a hidden input element (generated using ASP.NET MVC's Html.HiddenFor() method, but, that should be irrelevant):
<input type="hidden" value="0" name="Status" id="Status" data-val-required="The Status field is required." data-val-number="The field Status must be a number." data-val="true">
When the form is submitted, this value is incremented by one and the model is returned to the view. If I write the status value to the page, I can see that the value was correctly incremented.
However, this hidden input field is always cached. It's never getting the correct value. I tried setting the autocomplete attribute directly on the input element, but without success.
How can I get this hidden field to get the correct value? I'd prefer not to use any Javascript.
Edit: Supplying more code...
Controller
//This code is being executed, and the status is being incremented.
shippingOrder.Status = (shippingOrder.Status != (int)OrderStatus.Closed) ? shippingOrder.Status + 1 : shippingOrder.Status;
View
#using (Html.BeginForm("Number", "Order", FormMethod.Post, new { id = "orderSummary", autocomplete = "off" })) {
...
#Html.HiddenFor(m => m.Status)
}
According to this post here html helpers such as HiddenFor will always first use the posted value and after that the value in the model.
As you said, when writing the value to the page you can see it incremented, yet the helper is using the previously posted value, which is the intended behaviour.
The link does suggest the use of ModelState.Remove("Status") or ModelState.Clear() before assigning the new value.
It also suggest that another option could be not using a HiddenFor helper but instead to build the hidden field yourself. Similar to this:
<input type="hidden" name="Status" value="#Model.Status" />
Either way it looks like your problem is based on similar circumstances.

Resources