how to bind select option value in spring mvc - spring

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.

Related

Spring Form:Select selected value

I know this has been asked before but the solutions provided doesn't seem to work on my end.
I'm currently developing a web application and I'm currently working on the user profile page. What I'm doing is that when the user visits his/her profile, he/she will be able to manage his/her profile.
I have a spring form:select box which populates a List of RolesObject and a List of GroupsObject passed from the controller. The object consists of an id and name fields. What I want is when the form:select loads, it will select the present role id and group id of the user from the list.
controller
public ModelAndView viewProfile(parameters) {
ModelAndView mav = new ModelAndView();
mav.setViewName("viewProfile");
...
List<RolesObject> roles = rolesService.getRolesList();
List<GroupsObject> groups = groupsService.getGroupsList();
Map<String, List> map = new HashMap<String, List>();
map.put("roles", roles);
map.put("groups", groups);
mav.addObject("map", map);
return mav;
view
<form:select path="role_id" id="role_id" cssClass="form-control" cssStyle="width: 100%;" value="${current.getRole_id()}" required="required">
<c:forEach var="role" items="${map.roles}">
<form:option value="${role.getId()}" label="${role.getName()}" />
</c:forEach>
</form:select>
<form:select path="group_id" id="group_id" cssClass="form-control" cssStyle="width: 100%;" value="${current.getGroup_id()}" required="required">
<c:forEach var="group" items="${map.groups}">
<form:option value="${group.getId()}" label="${group.getName()}" />
</c:forEach>
</form:select>
My problem is the list populates but it does not select a default value.
Let's say my role values are
value=1, label=role_1
value=2, label=role_2
and the user's current role is role_2 which has an id of 2, when the select form loads, it does not automatically load role_2 but instead still shows role_1 as the selected value
For preselection of the option you need to set the role_id and group_id in the controller and set it as the modelattribute of the form object on page.

java springframework <select> tag

I am struggling with "SELECT/OPTIONS" tag. I have 2 ArrayList<<String>>: "pNames" and "pIds". Form should display "pNames", while the values returned to controller should be "pIds". From my spring controller I am passing the following 2 ArrayLists. How do I implement this in Spring MVC?
ArrayList<String> pIds = pps.getPIds();
ArrayList<String> pNames = pps.getPNames();
model.addAttribute("pIds", pIds);
model.addAttribute("pNames", pNames);
<form:select id="pps" name="pps" path="pIds" multiple="multiple">
<form:options items="${pIds}" itemValue="${pNames}" itemLabel="${pNames}"/>
</form:select>
Above code is not working.
You need to convert your Lists to a Map<String, String>. Then you can add the map to your model and you only need to do:
<form:select path="pIds">
<form:options items="${mapName}" />
</form:select>
For more information, please checkout this question: Use <form:select> tag with a map

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>

object references an unsaved transient instance, Error only when trying to save with empty value

I have an object EvalQuestionType with a one-to-many relationship to another object EvalSelectGroup. I save the relationship in a spring form like so:
<tr>
<th><sf:label path="evalSelectGroup.id">Select Group</sf:label>
<td><sf:select path="evalSelectGroup.id">
<sf:option value="">None</sf:option>
<sf:options items="${selectGroups}" itemLabel="name" itemValue="id" />
</sf:select>
<br />
<sf:errors path="evalSelectGroup.id" cssClass="error" /></td>
</tr>
It works fine if I select an option, but if I try to save with None selected (empty) I get the transient instance error. The value can be null (in the db and my hibernate mapping file). Is there a special way to format empty values in a Spring form select?
Spring 3.2.1
Hibernate 3.6
This may not be the best or right way to do it, but in the controller if I take the EvalQuestionType object passed by the form and set the evalSelectGroup object to null it works fine.
//if none selected in form
if(questionType.getEvalSelectGroup().getId() == null){
questionType.setEvalSelectGroup(null);
}

How to get both value and label of selected Item

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.

Resources