Do Spring MVC controller look for ids or names? - java

If I have the following HTML form:
<form id="myForm" action="/myFormHandler" method="post">
<input type="hidden" id="fizz-id" name="fizz" value="3" />
<input type="hidden" id="buzz-id" name="buzz" value="6" />
</form>
(Notice no submit button). And then I have the following jQuery:
$("#someButton").click(function() {
$("#myForm").submit();
});
Then on the server-side (Spring MVC controller), do the hidden field ids or names get sent to the handler method?
#RequestMapping(value = "/myFormHandler.html", method = RequestMethod.POST)
public ModelAndView handleMyForm(
#RequestParam("fizz-id") String fizz,
#RequestParam("buzz-id") String buzz) {
// Should I be looking for "fizz-id" or "fizz"???
}
Thanks in advance.

Its the name attribute that is sent to the server, so in this case you should look for "fizz".
The id attribute is just used for client side interaction, it is not sent in the request to the server.

with a post request ,it is the name attribute of a HTML element that is used to access it using request.getParameter.

Related

Spring REST Request method 'DELETE', 'POST', 'PUT' not supported [duplicate]

I have the following method in my controller
#RequestMapping(value = "processPurchase/{poid}", method = RequestMethod.DELETE)
public String processOrder(#PathVariable int poid) {
// do some processing
return acceptPurchaseForm;
}
My HTML
<form id="purchase-list-form" class="form-horizontal" action="/MyNewApp/processPurchase/" method="post">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="poid" value="">
With the above I still get the following error
WARN : org.springframework.web.servlet.PageNotFound - Request method 'DELETE' not supported
Any help appreciated.
First of all, I assume you have the HiddenHttpMethodFilter configured in your web.xml. It is required to convert your _method with value delete to the DELETE RequestMethod
Secondly, the poid is being passed in the body of the request but in your controller, you are expecting it to be passed in the URL itself. This might explain why Spring is unable to map the request.
EDIT 1:
To pass poid in URL, you will have to include in your form action when your HTML is generated. It depends on your view technology (I use Freemarker) but you would be required to do something like this:
<form action="/MyNewApp/processPurchase/${poid}" method="post">
Assuming that the poid is written to the model that is binded to your view.

Can I tell spring to ignore query parameters?

If I submit this form:
<form id="confirmForm" method="POST">
<input type="hidden" name="guid" value="guidval"/>
</form>
to this url:
/AltRT?guid=guidval
mapped to this controller method:
#RequestMapping(method = RequestMethod.POST)
public String indexPost(#RequestParam String guid)
I am getting both values for my guid. So the value of guid is guidval,guidval. I would like to only get the value from the form.
Is there any way tell Spring to ignore query string parameters?
EDIT for more clarification: The query string is left over from another (get) request. So, if I could clear the query string that would work as well. Also, I do not want edit the name of the form input because I want this post endpoint to be available to other services without having to change them as well.
You cannot do so because the query string will be sent in the HTTP message body of a POST request, http://www.w3schools.com/tags/ref_httpmethods.asp
There are two ways I could think of now
set the form attribute action
<form id="confirmForm" method="POST" action="AltRT">
<input type="hidden" name="guid" value="guidval" />
</form>
convert the form data into JSON object to send it over and then catch it with #RequestBody in Spring if you have to use the original URL.

Spring-MVC : two jsps into one controller with one requestMapping

Can I use one request mapping for two jsps?
I am currently calling one request mapping from one controller but one of the jsps doesn't seem to be caught by the controller.
Both jsps have the same form action and same form method:
first.jsp look like this:
<form:form method="POST" action="/ShowroomNav/requestQuote" id="requestQuoteForm">
<input type="hidden" value=${product.productCode } name="productCodes" />
<input type="hidden" id="requestQuoteEmailAddress" name="requestQuoteEmailAddress" />
</form:form>
the second.jsp look like this:
<form:form method="POST" action="/ShowroomNav/requestQuote" id="requestQuoteForm">
<input type="hidden" id="requestQuoteEmailAddress" name="requestEmailAddress" />
<c:forEach var="product" items="${products}">
<input type="hidden" value=${product.productCode } name="productCodes" />
<div class="box">
<img
src="public/productImages/${product.productCode}/${product.productCode}A.jpg"
style="max-width: 100%"
onclick="productProfile('${product.productCode}')" /><br /> <label
class="name">${product.productName}</label>
</div>
</c:forEach>
</form:form>
both of them submits the function by a javascript call of :
$("#requestQuoteSubmitButton").one("click",function(){
$("#requestQuoteEmailAddress").val($("#requestQuoteEmailAddressModal").val());
alert($("#requestQuoteEmailAddress").val());
$("#requestQuoteForm").submit();
});
this is how the controller.java looks like:
#RequestMapping(value = "/requestQuote", method = RequestMethod.POST) // or GET
public String requestQuote(#RequestParam("requestQuoteEmailAddress") String requestQuoteEmailAddress, #RequestParam("productCodes") String[] productCodes) {
System.out.println(">>>> requesting quotes >>>>");
for(int i=0; i<productCodes.length; i++) {
System.out.println(" Product Codes : " + productCodes[i]);
}
System.out.println("requestQuoteEmailAddress : " + requestQuoteEmailAddress );
System.out.println("<<<<< requesting quotes <<<<");
return "productSearch";
}
So I don't know why the second.jsp cannot be caught by the controller as it always shows this error when I try to submit it.
HTTP Status 400 - The request sent by the client was syntactically incorrect.
Can somebody help please?
The problem is (a typo?) in your 2nd line of your second.jsp snippet:
<input type="hidden" id="requestQuoteEmailAddress" name="requestEmailAddress" />
The id attribute is mainly for client side reference, and doesn't matter when form is submitted (see HTML input - name vs. id). What's important is the name attribute. So when the POST request is sent to server, the request body looks like:
requestEmailAddress=...&productCodes=...&productCodes=...
Since you annotated the handler method parameter as #RequestParam("requestQuoteEmailAddress"), Spring MVC looks for requestQuoteEmailAddress instead of requestEmailAddress, thus the error (#RequestParam's required is true by default).

Get value from jsp via spring controller

I've created MVC Template via Spring Tool Suite IDE, but I can't realize, how to get values from jsp. For exmaple - I've created text input in jsp
<input type="text" />
but how to get the value to controller to be able to work with it there? I know, that when I add atribute to model in my controller, then I can access it via ${name} , but how to do it the other way?
You need a form and then submit the value to the controller ...
Something like :
<form name="foo" action="/foo/bar" method="post">
<input name="fieldName" type="text" />
<input type="submit" value="Submit">
<form>
And then get it in your controller :
#Controller
#RequestMapping(value = "/foo")
public class AdminController {
#RequestMapping(value = "/bar")
public String testAction(#RequestParam String fieldName) {
// yourValue contain the value post from the html form
return "yourview";
}
}

Spring MVC 3 form binding

I have a simple HTML form:
<form id="marketplaceForm" enctype="multipart/form-data" method="post">
<select name="category">
<option selected ></option>
<option value="Sales">Sales</option>
<option value="Marketing" >Marketing</option>
</select>
<textarea type="text" id="marketplaceDesc" name="description" value="" class="creattbleArea"></textarea>
<input type="text" id="marketplaceName" name="templateName" >
<input type="file" id="marketplaceLogo" name="logo">
<input type="submit" value="Save" id="update" />
<input type="text" id="marketplacePrice" name="price">
</form>
I need to auto bind this form when I submit it. This works fine:
#RequestMapping(value = "/.....", method = RequestMethod.POST)
public String PublishForm() {
But this throws the following error:
HTTP Status 400 -
The request sent by the client was syntactically incorrect
#RequestMapping(value = "/PublishApplication.htm", method = RequestMethod.POST)
public String PublishForm(#RequestParam("templateName") String templateName,
#RequestParam("category") String category,
#RequestParam("price") String price,
#RequestParam("description") String description
) {
Can any one help me?
Update: I have found that if I remove enctype="multipart/form-data" from the HTML form, it works. Now my question is how to make it work with enctype="multipart/form-data".
I think you may be missing the Multipart resolver from your configuration.
do you have something like this in your configuration?
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="250000"/>
</bean>
see here for the offical spring documentation on the matter.
First of all, make sure the binding to PublishApplication.htm really works. You are using this mapping in your controller, but you haven't specified it in action param of <form> tag. So you may end up with posting the form to some different controller, and server rejects your request. Of course this will not happen if you are using the same controller for both - displaying form and submiting it, and you have aplied RequestMapping annotation at class level.
There is another issue with your controller though. You are not specifying logo as #RequestParam in PublishForm method. I'm not sure if this is not messing up form autobinding. If I recall correctly, those params are required by default.

Categories