How to preselect struts html:radio button - struts-1

How to preselect struts 1 html:radio button?
Thanks
Maria

You should have a value in your Form object that holds the data for your radio button. You'll need to pre-fill that in your Form before forwarding to the JSP (or default it in the Form's constructor if it will always default to that value).
Action.java
form.setRadioValue("123");
View.jsp
<html:radio property="radioValue" value="123" /> // <-- this radio will be checked
<html:radio property="radioValue" value="456" /> // <-- this radio will not be checked

Related

TinyMCE's value in bean on second submit (using jsf.ajax.addOnEvent)

I have set up a basic testcase in which I'm experience some (to me) weird behaviour. When using the setup below, the typed value in the editor will only be visible by h:outputText on the second submit. E.g.
Change value in editor to "myvalue"
Send Ajax-request
h:outputText shows "test" (default value from bean)
Change value in editor to "anothervalue"
Send Ajax-request
h:outputText shows "myvalue"
Send Ajax-request
h:outputText shows "anothervalue"
Note: there's a custom composite, please ask for code if needed (it simply creates textarea for TinyMCE and loads .js file from below)
index.xhtml
<h:body>
<h:form>
<mh:editor id="tinymceEditor"
value="#{bean.value}" />
<h:commandButton value="Ajax">
<f:ajax execute="tinymceEditor"
render="show" />
</h:commandButton>
<h:outputText id="show" value="#{bean.value}" />
</h:form>
</h:body>
jsfhandler.js -> included on header in custom composite mh:editor
jsf.ajax.addOnEvent(function(data) {
switch(data.status) {
case "begin":
tinyMCE.execCommand('mceRemoveControl',true,"tinymceEditor");
tinyMCE.triggerSave();
break;
case "complete":
tinyMCE.execCommand('mceAddControl',true,"tinymceEditor");
break;
case "success":
break;
}
});
Bean.java
#Named
#RequestScoped
public class Bean {
private String value = "test";
}
The JSF ajax begin event is too late in order to take changes in form data into account. The ajax request is already prepared based on form data before this event.
Effectively, the sequence is as follows:
User enters input (and leaves field).
HTML DOM "change" event is triggered on input field.
User clicks submit button.
HTML DOM "click" event is triggered on submit button.
JSF prepares ajax request.
JSF ajax "begin" event is triggered on ajax request.
JSF sends ajax request.
...
Basically, you should be doing tinyMCE.triggerSave() during the HTML DOM "click" event.
<h:commandButton ... onclick="tinyMCE.triggerSave()">
Or, better, during the HTML DOM "change" event of the tinyMCE textarea.

ASP.NET MVC 3 Razor view returning multiple values for a Html.CheckBoxFor

I have a View that contains the following Line of code:
//(DaysOfWeek is a bool[])
#Html.CheckBoxFor(m => m.Data.DaysOfWeek[0])
It starts off as false. When the user "checks" the box and returns, it returns a value for both true and false;
Here is what is being passed back as part of the form data
Data.DaysOfWeek[0]:true
Data.DaysOfWeek[0]:false
Why is it doing that?
This is because standard HTML checkboxes return no value if unchecked. To make this annoying behaviour more intuitive, the CheckBoxFor method creates a checkbox and a hidden control with the same name, with a value of false, something like this:
<input type="checkbox" name="myControl" value="True" /> My control
<input type="hidden" name="myControl" value="False" />
What you will see when the form is posted is either:
False // checkbox unchecked
True,False // checkbox was checked
Therefore, to test if the box was checked you should use Contains('True'):
bool checkboxChecked = formCollection["myControl"].Contains("True");

How to tell the control if the button of the view is dis/enabled

In my view I have this button:
<input type="submit" value="Delete" disabled="disabled" id="btnDelete" name="btnDelete" />
and I have Jquery method which makes it enabled.
This is in the view.
How to check if the button is enabled or disabled in the controller?
I have this method:
public void MyMethod(FormCollection form)
{...}
but form doesn't contain anything about the button.
also, form["btnDelete"] is null.
Yes because input button can not hold any value and returning it is useless.
If you want to have that attribute value in your controller make a hidden input which hold the button value on page load and will be changed on btnDelete click event.

how to preselect value in dropdownlist in jsp struts

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

Setting SqlDataSource SelectParameters from form text box

I am trying to use a text box input as a SqlParameter but it only goes into DataSelecting when the page first loads. Not after the from is submitted.
Here is the code on the aspx page.
protected void DataSelecting(object sender, SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters["#zip"].Value = ZipBox.Text;
}
"
SelectCommand="SELECT Name FROM Names WHERE (ZipCode = #zip)"
OnSelecting="DataSelecting">
SelectParameters>
parameter Name="zip" DefaultValue="1" />
SelectParameters>
SqlDataSource>
FORM
id="ZipSearch" runat="server" action="Default.aspx" method="post">
TextBox ID="ZipBox" runat="server" />
Button id="btnSubmit" Text="Submit" runat="server" />
FORM
Thanks for your help,
Matt
You need to place that code in the button click event. Selecting event is for different purpose.
Old reply (Before OP's comment) :
What do you have in button click event?
The Selecting event would fire before your select command is executed. Hence if your button click event is firing any command, the Selecting event won't be fired.

Resources