Reputation: 57
where i am trying to use button(not necessarily for purpose of submit) and onclick i want to call spring mvc's controller method whose requestmethod type should be POST not GET.
<input type="button" onClick="location.href='/result'"/>
This is my controller method which is being called on click.
@RequestMapping(value="/result")
public ModelAndView postPrintHello(@ModelAttribute("product") Product product){
ModelAndView model = new ModelAndView();
model.addObject("product", product);
model.setViewName("result");
return model;
}
But when i Add RequestMethod.POST it is not called. Please suggest me wayout for this and also let me know if i missed something from view front.
Upvotes: 4
Views: 27811
Reputation: 1474
try this
HTML:
<input type='button' value='Submit'>
JS:
$('input').on('click', function () {
$.ajax({
type:'POST',
url :"result",
success: function(data) {
console.log('success',data);
},
error:function(exception){alert('Exeption:'+exception);}
});
e.preventDefault();
});
Without JS or JQuery
<input type="button" onclick="location.href='/result'" value="Submit" >
Note: With this way you can only send GET Request, your controller should have this annotation method = RequestMethod.GET
and if You want POST request without Jquery use Form
<form action="/result" method="post">
<input type="submit" value="Submit" />
</form>
Upvotes: 5
Reputation: 79
First in form tag you have required some changes
<form action="/servlet" method="post">
<input type="submit" name="login" />
</form>
it will call your doPost() method
Upvotes: 0
Reputation: 61
Either make button type submit or call javascript method and submit form like document.forms["myform"].submit();
Upvotes: 0