I have this iterator that loops through an object.
<s:iterator value = "choices" status = "key">
<s:set var = "test" value ="%{#key.index}"/>
<input type = "radio" name="choices[{key.index}].answer" />
<s:textfield name = "choices[%{#key.index}].value" value = "%{choices[%{#key.index}].value}"/>
<br>
</s:iterator>
where answer is a boolean value, that I am trying to set through radio buttons.
but the problem is, upon on the generated html, teh radio buttons. is like this
<input type = "radio" name="choices[%{#key.index}].answer" />
It did not have the indice/index. it only gave me the %{#key.index}
you have to use property tag to get the actual value
<input type = "radio" name="<s:property value='%{choices[#key.index].answer}' />" />
Better use radio tag of struts tag library, like this
<s:radio name="choices[%{#key.index}].answer" / >
Related
I am working in spring mvc, I used the following line to generate the checkboxes from DB table.
<td><input type="checkbox" name="loanId" value="${loan.id}" class="it"></td>
I need to get the index of this selected values in my java controller. I am able to get the values which are selected, but how to get the index values?
following code is I am using
String[] loanIds = request.getParameterValues("loanId");
for (String string : loanIds) {
System.out.println("loanIds****"+string);
}
You need javascript to get the index of checkbox and set it to hidden field :
var ids = document.getElementsByName('loanId');
var ind = document.getElementById('loanIndex');
var put = function() {
var arr = [];
var i = -1;
while (ids[++i])
if (ids[i].checked) arr.push(i);
ind.value = arr.join(',');
alert('selected index: ' + ind.value);
};
var i = -1;
while (ids[++i])
ids[i].onchange = put;
<input type="checkbox" name="loanId" />
<input type="checkbox" name="loanId" />
<input type="checkbox" name="loanId" />
<input type="checkbox" name="loanId" />
<input type="hidden" name="loanIndex" id="loanIndex" value="" />
In controller:
String[] loanIndex= request.getParameter("loanIndex").split(",");
There is no direct method for this purpose.
You can have index as value which is passed on selecting checkbox. You can use this index-value later to get selected record data from in-memory collection i.e. when user selects checkbox and submits request, on server side you will fetch ParameterValues from request and list data from datasource, compare ids then derive selected list.
Using Jquery
var indexString;
var index;
$('#.it').each(function () {
$(this).find('.SomeCheckboxClass').each(function () {
if ($(this).attr('checked')) {
index = $(this).index();
indexString = index + ",";
}
});
});
post this indexString, it is a comma separated index string.
I have a form which has multiple input fields that will be added dynamically when user click the Add button.
I already managed to achieve this, but the problem I am facing now is I cannot pass the parameters that has been added dynamically using JavaScript function, to a servlet.
This is the code of dynamically adding multiple input fields:
<script type="text/javascript">
$(document).ready(function(){
var counter = 2;
$("#addDynamicDivs").click(function () {
var newTextBoxDiv1 = $(document.createElement('div'))
.attr("id", 'TextBoxDiv1');
newTextBoxDiv1.attr("style",'float: left;');
var newTextBoxDiv2 = $(document.createElement('div'))
.attr("id", 'TextBoxDiv2');
newTextBoxDiv2.attr("style",'float: left;');
var newTextBoxDiv3 = $(document.createElement('div'))
.attr("id", 'TextBoxDiv3');
newTextBoxDiv3.attr("style",'float: left;');
var newTextBoxDiv4 = $(document.createElement('div'))
.attr("id", 'TextBoxDiv4');
newTextBoxDiv4.attr("style",'float: left;');
newTextBoxDiv1.after().html('<label class="inputlabel2">Speaker Name : </label>' +
'<input type="text" name="speakername" id="speakername" value="" >');
newTextBoxDiv2.after().html('<label class="inputlabel2">Speaker Country : </label>' +
'<input type="text" name="speakercountry" id="speakercountry" value="" >');
newTextBoxDiv3.after().html('<label class="inputlabel2">Speaker Company : </label>' +
'<input type="text" name="speakercompany" id="textbox" value="" >');
newTextBoxDiv4.after().html('<label class="inputlabel2">Speaker Desc : </label>' +
'<textarea name="speakerdesc" id="speakerdesc" rows="10" cols="30" ></textarea>');
newTextBoxDiv1.appendTo("#TextBoxesGroup");
newTextBoxDiv2.appendTo("#TextBoxesGroup");
newTextBoxDiv3.appendTo("#TextBoxesGroup");
newTextBoxDiv4.appendTo("#TextBoxesGroup");
});
});
</script>
It appears that the only parameters passed to the servlet are from the form, not including the parameters from the dynamically added input fields.
How can I pass all the parameters from the function to the servlet?
Thanks,
raz
You have to include the dynamic params inside the form. You should put inside the form the div elemt where you are including the dynamic params.
<form>
<div id="TextBoxesGroup">
</div>
</form>
I have a form that includes a radio button, and I'm trying to turn the radio button into a "bootstrap switch" (http://www.bootstrap-switch.org/). This is how I'm currently doing it:
<input type="radio" name="myRadio" value="true">
<script>
$ ('[name="myRadio"]').bootstrapSwitch();
</script>
My radio button gets replaced with a bootstraps switch, but it's value is set to null regardless of wether the switch is turned on or off. If I remove the script and use a plain radio button, everything works as it's supposed to.
I want to retrieve the radio button value in a servlet like this:
String value = request.getParameter("myRadio");
What is wrong, and how can I fix it?
Attach switchChange event to bootstarp switch and then get it's state like
$("[name='my-checkbox']").bootstrapSwitch();
$('input[name="my-checkbox"]').on('switchChange.bootstrapSwitch', function(event, state) {
console.log(this); // DOM element
console.log(event); // jQuery event
console.log(state); // true | false
});
This is for those who have paired radio buttons with Enable/Disable kind of values
Original HTML:
<INPUT TYPE="RADIO" NAME="radio_active" VALUE="Yes" onClick="trunOff(0)" checked>Activated
<INPUT TYPE="RADIO" NAME="radio_active" VALUE="No" onClick="turnOff(1)">Deactivated
Replace above with HTML:
<input type="hidden" name="radio_active" data-on="Yes" data-off="No" value="Yes" data-onchange="trunOff">
<input type="checkbox" class="bs-switch-radio" data-hidden="radio_active" data-size="small" data-label-width="auto" data-on-text=" Activated " data-off-text=" Deactivated " checked />
jQuery:
jQuery('.bs-switch-radio').bootstrapSwitch();
jQuery('.bs-switch-radio').on('switchChange.bootstrapSwitch', function(e,state){
var hidden = jQuery(this).attr('data-hidden');
var jhidden = jQuery("input[name='"+hidden+"']");
var on = (jhidden.attr('data-on'))? jhidden.attr('data-on'): 1;
var off = (jhidden.attr('data-off'))? jhidden.attr('data-off'): 0;
var state_val = (state)? on : off;
jhidden.val(state_val);
var fnOnchange = jhidden.attr('data-onchange');
var fn = window[fnOnchange];
if(typeof fn === 'function') {
fn(state);
}
});
In jsp, is there any attribute to provide the information that values in the dropdown got changed?
Say Example:
I have dropdown country - On page load it have "US" in the dropdown and I'm changing to "UK" and then again changing to "US". In this case I have not done any change so I have nothing to save but if I click save button my page is getting submitted. Is there anyway to avoid this page submission.?
Yes, it is. But you have to use javascript, not jsp. Example code
html:
<form class="checked">
<input type="hidden" id="countrySelectInitial" value="UK" /> <!-- we need this element to store initial value -->
<select name="country" class="checkedInput" id="countrySelect">
<option value="US">US</option>
<option value="UK" selected>UK</option>
</select>
<input type="submit" class="submitButton" disabled />
</form>
js:
$(".checkedInput").change(function() {
var newValue = $(this).val();
var idValue = $(this).attr('id'); // we need id to retrieve initial value. Initial value is stored in element with id <id>Initial
var oldValue = $("#" + idValue + "Initial").val();
if(newValue === oldValue) {
// if value don't changed - disable button. user have no possibility to submit form
$(this).parents("form.checked").find(".submitButton").attr("disabled", true);
} else {
// if value changed - remove "disabled" attribute. User can click button and submit form
$(this).parents("form".checked).find(".submitButton").removeAttr("disabled");
}
});
I'll try to put an image in my checkboxlist like this
<s:checkbox name="evento.eve_tarjeta_cred1" id="chkAmex" />
<img alt="10" src="./images/tarjetasCredito/amex.png">
<s:checkbox name="evento.eve_tarjeta_cred2" id="chkCmr" />
<img alt="10" src="./images/tarjetasCredito/cmr.png">
But i need to pass a list in one field to save it in my DB like
"true,false".
I'll try in this way
<s:checkboxlist id="chkTarjeta" name="evento.eve_tarjeta_cred"
list="{
'<img alt="10" src="./images/tarjetasCredito/amex.png">',
'<img alt="10" src="./images/tarjetasCredito/cmr.png">'
}"
/>
but it doesn't work. HELP
The error is probably in the <img/> tags declaration: when you open the first double quote, you are closing the list attribute.
Put them in the Action (and read the values with a getter from the list attribute), or try escaping them manually;
<s:checkboxlist id="chkTarjeta" name="evento.eve_tarjeta_cred"
list="{
'<img alt="10" src="./images/tarjetasCredito/amex.png">',
'<img alt="10" src="./images/tarjetasCredito/cmr.png">'
}"
/>
I'm not sure if Struts2 will escape the values or not; if it will, the only chance would be to extend the Tag.
To submit values in one string line use the same names on both checkboxes and your evento.eve_tarjeta_cred variable must be a String.
Use <label> tags to create labels of image for checkboxes.
Note: that unchecked chekboxes (false values) won't be submitted.
<label>
<s:checkbox name="evento.eve_tarjeta_cred" id="chkAmex" />
<img alt="10" src="./images/tarjetasCredito/amex.png" />
</label>
<label>
<s:checkbox name="evento.eve_tarjeta_cred" id="chkCmr" />
<img alt="10" src="./images/tarjetasCredito/cmr.png" />
</label>
I solved it like this, in my JSP i got this
<s:checkbox name="t1" id="chkAmex" />
<img alt="10" src="./images/tarjetasCredito/amex.png">
<s:checkbox name="t2" id="chkCmr" />
<img alt="10" src="./images/tarjetasCredito/cmr.png">
and in my Action i declare a global variable "tarjetas", with get and set, obviously
tarjetas = t1 + "," + t2;
evento.setEve_tarjeta_cred(tarjetas);
eventoservice.RegistraEvento(evento);
and to show it in Update case, just do this
String[] tarjetasArray = evento.getEve_tarjeta_cred().split(",");
for (int i = 0; i < tarjetasArray.length; i++) {
t1 = tarjetasArray[0];
t2 = tarjetasArray[1];
}