how to preselect value in dropdownlist in jsp struts - drop-down-menu

i am working web application on struts 1.3 in netbeans i have two dropdownlist say A and B now i have no probl1em in populating these dropdowns on jsp load ,now user select one value from say dropdown A and value user selected say it is 1 and for another dropdown say B user selected value which is 2 and now user will save these values in database now my requirement is that when we go in edit mode for that when page loads dropdownlist A should have value 1 as preselected and dropdownlist B should have value 2 as preselected. and these values are coming from database
my jsp page
1 dropdown
<tr>
<td> <html:select name="RoomForm" property="location"onchange="getFloorDropdown(this.value);" >
<html:option value="0">Select Location</html:option>
<html:optionsCollection name="RoomForm"
property="list" value="id" label="name" />
</html:select>
<td>
</tr>
RoomForm:-name of my form bean in requst scope
list:-Arraylist collection
id:-it is property in Arraylist which is list having getter and setter in bean
name:-it is property in Arraylist which is list having getter and setter in bean
2 dropdown
<select name="floor" >
<option value="0">Select Floor</option>
</select>
how should i do how select a preselected values in both downdroplist when this form opens in edit mode
any hint will be a great help for me

Zohaib,
In the action that is responsible for retrieving the information from the database, populating the relevant ActionForm and finally loading the Edit view, the ".location" property of that ActionForm must be populated with the value stored in the database. In the JSP, you are going to associate that ActionForm instance as the form backing object. Struts will automatically do the rest, i.e. ensuring that the correct option shows as selected.
HTH.
Ashwin

Related

Checkboxes tag spring mvc and binding

I have checkboxes tag in my web application with spring mvc. Checkboxes are created from a map in controller like this:
Map demOrgs = createMap();
model.addAttribute("demOrgs", demOrgs); // example : (1, my-description)
1 --> will be value of checkbox
my-description --> will be label of checkbox
In my jsp :
<form:form commandName="myBean" method="POST" >
<form:checkboxes items="${demOrgs}" path="demOrg" element='div class="checkboxes"' />
</form:form>
My bean has only one field :
String demOrg;
When I send the form demOrg attribute has the value of checkboxes clicked, for example: (1,5,8)
I store myBean in session, when I go to the next step in my application. But when I return, I want the checkboxes were checked, still checked and isn't that way.
When the bind value of checkbox is a boolean value, allways work but I'm binding a custom value :
<input id="demOrg1" type="checkbox" value="2" name="demOrg">
<label for="demOrg1">My label description</label>
<input id="demOrg2" type="checkbox" value="3" name="demOrg">
<label for="demOrg2">My label description 2</label>
.....
Does anyone know how to do this?
thanks to all!!
What does the signature of your controller method look like? Are you including myBean as a method signature argument, annotated with #ModelAttribute ?
Something like:
#RequestMapping(......)
public String myController (#ModelAttribute MyBeanType myBean, Model model) {
Map demOrgs = createMap();
model.addAttribute("demOrgs", demOrgs);
model.addAttribute(myBean);
}
Optionally you can annotate the method parameter with #Valid as well if you are using JSR-303 bean validation .
I think the trick is to make sure your demOrg property is actually a collection. Check out the
checkbox reference here. In particular, the text that says:
Typically the bound property is a collection so it can hold multiple values selected by the user.
Though "myBean" is stored in the session, isn't it reloaded again from the database when the controller is ran?

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);
}

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

MVC3 get Select Box Option Name as Well as Value

<select id="selectedSchool" size="3" multiple="multiple" name="selectedSchool">
#foreach (var item in ViewBag.pt)
{
<OPTION VALUE="#item.entityID">#item.name</OPTION>
}
</select>
on my page the user selects a school from the list of schools, each school has a unique ID which is needed and passed via the VALUE attribute, I also would like to pick up the name
(item.name) that was selected how can I resolve this info from my control Action Result?
public ActionResult ResultsSchoolsAttended(List<int> selectedSchool)
i iterate through the list of results, but I would aslo like selectedSchool.name to enter back in the db as 'display text'
how can i do this?
Well, MVC can only give you what is in the HTTP POST. And if you look at that, you will see that the <option> inner HTML isn't there. So you have only two choices:
Include it in the value in your markup, or
Look it up in your ResultsSchoolsAttended action based on the submitted value.
I'd pick the second, personally.

MVC equilvelant of repeater with multiple form elements and checkbox to select

I need to replicate the functionality you get with an asp repeater that contains checkboxes, textboxes, and the occasional dropdownlist in an MVC application. Om traditional webforms you get a postback event and its pretty easy to get at all the controls and their values associated with the checkbox (id).
Anybody have any good suggestions on how to get the values back on a post for the items that have the checkbox checked. I am able to get some arrays back in the FormCollection (and even a strongly typed view) but I have not been able to figure out a good way to link the values effectivly.
All rows have a checkbox and a textbox, some rows also have a dropdownlist
To further explain...
row 1 has a checkbox and a textbox
row 2 has a checkbox a textbox, and a dropdown list.
if the user selects row 1 and 2, I need to get the values for all the form elements (for some calculations). Also, I haven't come up with a good method of handling validation errors.
any suggestions are greatly appreciated.
The simplest thing to do would be to iterate the collection for which you would have a repeater for. Using Razor syntax:
#using (Html.BeginForm()) {
<table>
#for (var i = 0; i < Models.Items.Count; i++) {
<tr>
<td>
#Html.CheckBoxFor(x => Model.Items[i].SomeBool)
</td>
<td>
#Html.TextBoxFor(x => Model.Items[i].SomeString)
</td>
</tr>
}
</table>
<button type="submit">Go</button>
}
Then a controller method:
[HttpPost]
public ActionResult Index(MyViewModel viewModel) {
// view model will be bound to values in form
}
Assuming the the Model in the view is of type MyViewModel. Once you have a sense of how this works you can look into ASP.NET MVC grid frameworks.

Resources