Disable a checkbox in spring and pass the checked value to controller - java

Hello I have to disable checkbox in spring html form and pass the checked value to controller. I use the below code, but the problem is that I am not able to pass the checked value to controller when the disabled attribute is set to true
<form:checkbox path="agreementCats" disabled="true" value="${x.value}" class="category-checkboxes" id="agreementCat-cb-${i.index}" /><label class="control-label" for="agreementCat-cb-${i.index}"> ${x.label}</label>
<form:hidden path="agreementCats" value="${x.value}" />
The above code sends all the values to controller. Please help!!

in HTML have two tag with similar meaning but different context is Disabled and ReadOnly.
disabled: Disabled the input in the form and the data isn't in form query.
readonly: Omit the user UI interaction but the input is in form query.
Try use readonly tag instead disabled, I hope this help you.
That solution works for all inputs except checkbox... Checkbox can't use readonly, but you can do a little trick, is a dirt trick, but it can you help to not permit user touch check box.
If you know in server side what checkbox can be edited, try this code:
<input type="checkbox" <c:if test="condition">onclick="return false;"</c:if> ></input>
That code not permit user click the checkbox if the condition is true. Maybe with this can solved your problem.

Instead of disabled="true" change it to readonly= "true"
Disabled doesn't send a value if you insert but readonly does.

Related

HTML button/input I want to keep different text and value and read value in server

I tried googling this but didn't get a proper answer, so here goes the question to the experts on this forum:
I want to render a button on my web page such that it's value is different from what is displayed on screen. e.g if I use below html tag, Confirm is displayed.
<button name="type" value="save">Confirm</button>
However I want to read value "save" in the server so that I can have certain logic based on that. If I use
request.getParameter("type");
I get "Confirm" instead of "save".
My objective is to have multiple buttons with different values and at server side I want to know what button was clicked. I don't want to link the server code with the displayed text.
I can do a workaround such that onsubmit I call a javascript function that captures the button clicked and puts "save" in hidden field with name="type". But this seems like such a common client-server problem that there must be a more elegant solution to this problem that I am not aware of.
Appreciate any help.
Can you post more of your code? Both the HTML form and the server side code? The behavior you are expecting IS in fact the expected behavior for the code you have shown. I suspect you have a bug elsewhere in your code (like maybe reusing the variable name somewhere).
It would make sense that <button> works such that value attribute is sent in POST request whereas the displayed text is whatever is between <button>...</button>.
Unfortunately, standards don't define this behavior, so the browsers are left to implement it the way they want. In Firefox, value attribute is posted. But in Internet Explorer (I know about IE8, don't know if it changed in later version), value is not posted. Instead displayed text is posted!
In a nutshell you have to workaround this problem. There are a couple of good suggestions posted here.
This is what I found most useful or worked for me the most. Add a hidden field with the same name as I intend to use in the server.
<input type="hidden" name="type" id="btntype" />
<button type="submit" name="submitbtn" value="save">Confirm</button>
<button type="submit" name="submitbtn" value="verify">Verify</button>
<button type="submit" name="submitbtn" vallue="cancel">Cancel</button>
And this in Javascript:
$(document).ready(function() {
$(":submit[name='submitbtn']").on("click", function() {
$("#btntype").val($(this).val());
});
});
Now, in the servlet, I can use:
request.getParameter("type");
It will give value save if Confirm is clicked, verify if Verify is clicked and cancel if Cancel button is clicked.
This way also it will work. Just define the id for button.Whenever that particular submit button will be clicked just call .on() method
<button id="filter" value="value1" type="submit">Confirm</button>
<button id="filter" value="value2" type="submit">Verify</button>
<button id="filter" value="value3" type="submit">Cancel</button>
$(window).on("click", "#filter", function() {
alert($(this).val());
});

How to prevent reset in a Struts form?

I have this struts form:
<s:form ...>
<input id="foo" value="123"/>
</s:form>
I change the value of foo then I hit back button. When I come back , foo has lost my value and now is 123.
How can I avoid this ?
This is less of Strust's concern but more of browser's decision.
Check out the following related discussions:
Browser back button restores empty fields
Clear Form on Back Button? (doing the opposite)

How to get the value of disabled check box in Spring MVC

I am working on Spring MVC application and I am having a problem of getting the value of the check box when it is disabled.
in report.jsp page :
<form:checkbox type="checkbox" path="corporateColumn" id="corporateColumn" value="true" checked="checked" disabled="true" />
in ReportForm.java :
boolean corporateColumn ;
public boolean isCorporateColumn() {
return corporateColumn;
}
public void setCorporateColumn(boolean corporateColumn) {
this.corporateColumn = corporateColumn;
}
In ReportController.java ;
boolean corporateColumn = reportDTO.isCorporateColumn(); // this evaluates to false
//Which expected as true when corporateColumn checkbox is checked
Everything works fine unless it is used as disabled="true" (/ disabled="${'true'}") for checkbox field.
I had the similar issue previously also when getting the value of a disabled textfield and overcome it by making the field readonly.
So I am not sure in Spring MVC whether it is not possible to get the value of input field when it is disabled.
Any guidance would be really appreciated.
Thanks!
It's sorry to say that but that's impossible.
Use developer tool(like in Chrome) to check posted data, you can confirm browser will not send your checkbox's data if it's disabled or unchecked.
Why don't you simply use 'readonly' instead of 'disabled'?
Disabled field data is not passed on to server.

hide value by a hyperlink

I was asking that : whenever I pass a value by a link then it looks like this:
Click here to view details
Now when i click on that hyperlink i am going to some.jsp and retrieving value of search like:
request.getparameter("someid");
But I am also seeing all those sensitive details in the browser URL, which is vulnerable. I want to hide all these details so that nothing will be shown in the browser's url but processing will be done internally. How can i do it? Please ignore jsp tags, I am learning JSTL and will soon replace scriplets but initially i want to implement it on jsp tags. Any help is much appreciated.
If you'd turn the link into a button you could pass it as a hidden POST value and have your some.jsp page read that. For example:
<form method="post" action="some.jsp">
<input type="hidden" name="someid" value="<%=something%>" />
<input type="submit" value="Click here to view details" />
</form>
Then on your some.jsp, you can read the someid POST value and do with that whatever you want.
First of all , if you want to show sensitives stuffs in URL, Why are you usng GET request.
You should use POST request. Store the value of someid in request attribute.
You can encrypt and decrypt the value of someid for doing it. See the example given here:
Encrypting a String with DES

How to use RichFaces a4j:commandButton not using submit

I have an a4j:commandButton which looks like this
<a4j:commandButton id="stopBtn" type="button" reRender="lastOp"
action="#{MyBacking.stop}" value="Stop" />
</a4j:commandButton>
When the app is deployed, and the button clicked, the stop() method is not being called. All the a4j:commandButton examples refer to forms, but this button is not in a form - it's a button the user is going to use to cause the server to run some back-end logic. At the moment, the method is
public void stopNode() {
logger.info("STOPPING");
setLastOp("Stopped.");
}
Other methods which don't use this type of button are updating the lastOp field, but I'm not seeing anything on the console with this one. Am I right to cast this as a button? Should I put this in a h:form tag?
The firebug console says:
this._form is null
which I don't understand.
Any help well appreciated.
UICommand components ought to be placed inside an UIForm component. So, your guess
Should I put this in a h:form tag?
is entirely correct :) This because they fire a POST request and the only (normal) way for that is using a HTML <form> element whose method attribute is set to "post". Firebug also says that a parent form element is been expected, but it resolved to null and thus no actions can be taken place.
Only "plain vanilla" links like h:outputLink and consorts doesn't need a form, because they just fires a GET request.
Yes, wrap it in a form. I'm sure BalusC will post a detailed explanation while I'm typing my answer. (yup, there it is)
I have to ask why you didn't just try a form first, before posting here.
Look at your code:
<a4j:commandButton id="stopBtn" type="button" reRender="lastOp" action="#{MyBacking.stop}" value="Stop" />
You finished <a4j:commandButton with />, why need that orphan </a4j:commandButton> ?
If for some reason you don't want to place the button inside a form, you can do something like this:
<a4j:commandButton onclick="fireAjax()"/>
<h:form>
<a4j:jsFunction name="fireAjax" action=".."/>
</h:form>

Categories

Resources