0

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?

Paula Kristin
  • 733
  • 1
  • 9
  • 22

1 Answers1

5

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).

Community
  • 1
  • 1
qingbo
  • 1,961
  • 14
  • 18