In JSP, why do my radio buttons cause onSubmit to no longer be called?
Major Update: Due to Spring, the validator is called before the onSubmit invocation occurs. When no radio buttons are selected, this runs fine. When a radio button is selected, the POST data contains a variable for the radio button and this causes a NestedServletException.
This variable is not bound to any property or class. Is there a "correct" way that such a variable needs to be handled, either within JSP or within my code?
I have a JSP page, hooked up with Spring, where the onSubmit listener is no longer invoked after I add some radio buttons to the JSP page. Without the radio buttons, a bare version of the page looks like this.
<form:form>
<fieldset>
<input type="submit" value="Submit"/>
</fieldset>
</form:form>
This works as expected. My debugger reaches a breakpoint for onSubmit once the submit button is pressed. Then I add the following code for radio buttons, which can be seen below.
<form:form>
<fieldset>
<input type="submit" value="Submit"/>
</fieldset>
<fieldset class="contentHalfFieldset"><legend>NID</legend>
<input type="radio" name="someName" value="value1" />
<input type="radio" name="someName" value="value2" />
<input type="radio" name="someName" value="value3" />
</fieldset>开发者_JS百科
</form:form>
Now, when I don't select any radio button, the submit button still causes the submission event to reach the breakpoint in the onSubmit method. It still works as intended. However, when I select one of the radio buttons and then press the submit button, the page simply refreshes.
Why is that? What's happening that makes input buttons prevent the processing of a submission?
------- Generated HTML -------
<form id="cmd" method="post" action="/page/pageAction.html?param=k" onsubmit="disableSubmit(this);">
<fieldset class="contentHalfFieldset"><legend>NID</legend>
<label class="labelClass">First radio button:</label>
<input type="radio" name="nidStatus" value="first" /><br style="clear: both"/>
<label class="labelClass">Second radion button:</label>
<input type="radio" name="nidStatus" value="second" /><br style="clear: both"/>
<label class="labelClass">Third radio button</label>
<input type="radio" name="nidStatus" value="third" /><br style="clear: both"/>
</fieldset>
</form>
------- More Requested Code -------
This is the disableSubmit(..) method.
function disableSubmit(parentElement) {
elements = parentElement.elements;
for(var i = 0; i < elements.length; i++) {
var element = elements[i];
if(element.type == "submit") {
element.disabled=true;
element.value="Please Wait...";
}
}
};
Spring tries to bind your variables if they happen to be named as the same thing as your controls (in my case, the radio button). Depending on your code to handle the variable. which you are not intending to bind, an exception can occur before the onSubmit method is reached.
In my case, I had a class that had a "nidStatus" variable, of an enum type. When the variable "US&nidStatus" (see comments in response to Michael Kopinsky) was sent in the POST action, there was an automatic attempt to bind this. Interestingly, validation went fine before the exception was thrown, so -- if you found this question despite someone marking it unacceptable -- make sure to check your errors variable in your validate() method, as the handler will still continue on beyond that point as if things are fine.
精彩评论