1

I have one JSP say myPage.jsp

in jsp I am havin two div

each div is containing one form as below:

div A:

<div class="validation-box">                
            <form:form id="compnay-detail" method="post" action="companySave.do" commandName="company">
                    <table cellpadding="0" cellspacing="0">
                        <tr>
                            <td class="col"><label>Address Type</label> <form:input type="text" path="addressType" class="textbox" name="addressType" id="addType" maxlength="20" />
                            </td>
                            <td class="col"><label>Street</label> <form:input path="street"
                            class="textbox" name="street" id="street" maxlength="50" />
                            </td>               
                        </tr>
                        <tr>
                            <td colspan="3" class="no-padding">
                                <div class="button-pannel margin-top">
                                <span class="lhscrv"> <span class="rhscrv"> <input type="submit" class="bttn" id="saveAddress" value="Save" />
                                </span>
                                </span> <span class="lhscrv"> <span class="rhscrv"> <input
                                        type="button" class="bttn" id="reset" value="Reset" />
                                </span>
                                </span> <span class="lhscrv"> <span class="rhscrv"> <input
                                        type="button" class="bttn" id="cancel" value="Cancel" />
                                </span>
                                </span>
                            </td>
                        </tr>
                    </table>
            </form:form>
            </div>

div B:

<div class="validation-box">                
            <form:form id="compnayAddDetail" method="post" action="test.do" commandName="address">
                    <table cellpadding="0" cellspacing="0">
                        <tr>
                            <td class="col"><label>Address Type</label> <form:input type="text" path="addressType" class="textbox" name="addressType" id="addType" maxlength="20" />
                            </td>
                            <td class="col"><label>Street</label> <form:input path="street"
                            class="textbox" name="street" id="street" maxlength="50" />
                            </td>               
                        </tr>
                        <tr>
                            <td colspan="3" class="no-padding">
                                <div class="button-pannel margin-top">
                                <span class="lhscrv"> <span class="rhscrv"> <input type="submit" class="bttn" id="saveAddress" value="Save" />
                                </span>
                                </span> <span class="lhscrv"> <span class="rhscrv"> <input
                                        type="button" class="bttn" id="reset" value="Reset" />
                                </span>
                                </span> <span class="lhscrv"> <span class="rhscrv"> <input
                                        type="button" class="bttn" id="cancel" value="Cancel" />
                                </span>
                                </span>
                            </td>
                        </tr>
                    </table>
            </form:form>
            </div>

MY CONTROLLER CLASS:

@RequestMapping("/companySave.do")
    public String saveCompany(Map<String, Object> map, @ModelAttribute("company")
    Company company,@ModelAttribute("address")
    CompanyAddress address, BindingResult result)
    {           
        //......   code.......
        return "redirect:/companyAddressPage/"+company.getId()+".do";

    }

    @RequestMapping("/companyAddressPage/{id}.do")
    public String companyAddressPage(@PathVariable("id") long id,Map<String, Object> map, @ModelAttribute("company")
    Company company,@ModelAttribute("address")
    CompanyAddress address, BindingResult result)
    {   //......   code.......
        return "myPage";
    }

    @RequestMapping("/test.do")
    public String saveAddressCompany(Map<String, Object> map, @ModelAttribute("company")
    Company company,@ModelAttribute("address")
    CompanyAddress address, BindingResult result)
    {   //......   code.......
        return "myPage";
    }

AIM:

after submitting first form which is present in div A i want to enable div B which will contain another form.

Problem :

First form is submitting properly but when i click on submit button of second form it is throwing exception (action is not even getting called)

Exception :

Unable to convert value test from type 'java.lang.String' to type 'long'; nested exception is java.lang.NumberFormatException: For input string: "test"] with root cause

any idea....???

Thanks in advance...!!!

JOHND
  • 2,597
  • 6
  • 27
  • 35
  • It seems that your test.do gets to be handled by the method annotated with: @RequestMapping("/companyAddressPage/{id}.do") and there's a parse error since the {id} part with value "test" (without quotes) cannot be parsed to a long. Is the @ModelAttribute("company") intentional in method annotated with @RequestMapping("/test.do")? – heikkim May 21 '12 at 07:53
  • actually i am having two model attributes address and company,in company i am having companycode which is common in address also – JOHND May 21 '12 at 08:25
  • why it is calling /companyAddressPage/{id}.do ,i am calling test.do on submit action of second form – JOHND May 21 '12 at 08:28

2 Answers2

1

EDIT: see comments also.

You need to add a forward slash in front of your second form's action attribute:

action="/test.do"

Otherwise you will end up to /companyAddressPage/test.do since you've just been redirected to /companyAddressPage/1.do (where 1 is an example id). This is because second form's action attribute is relative.

heikkim
  • 2,955
  • 2
  • 24
  • 34
  • thanks for your reply ...but it's showing "HTTP Status 404 - /test.do" error after doing changes as per your suggestion – JOHND May 21 '12 at 08:33
  • Then I assume you're not running your app as the root web app. Either you need to resolve the root URI for your app and add it to the form action or remap @RequestMapping("/test.do") to e.g. @RequestMapping("/companyAddressPage/test.do") and declare it before @RequestMapping("/companyAddressPage/{id}.do") – heikkim May 21 '12 at 08:54
  • thanks a lot it's working....but tell is there any way to come out of that "/companyAddressPage/" or i have to use this as a prefix for every action...?????...please reply me..... – JOHND May 21 '12 at 09:18
  • Discussion about the issue: http://stackoverflow.com/questions/5012525/get-root-base-url-in-spring-mvc (and issues linked in the issue) One simple solution would be to make the action go up a level: action="../test.do" but I did not want to mention this since it usually creates whole other set of issues when the controller gets more complex (more redirects, more views using same methods). A good idea I can think of is to add @RequestMapping to type (controller) level also and make every controller responsible of one "URI-block" (e.g. CustomerController; /customer/, UserController; /user/). – heikkim May 21 '12 at 09:40
  • Spring framework reference answers your question: http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping (16.3.2 Mapping Requests With @RequestMapping) – heikkim May 22 '12 at 10:38
0

Whatever value you get in the @PathVariable it is always having String type value only.

So instead of using

@PathVariable("id") long id

use this

@PathVariable("id") String id

and then convert it to long manually before you use in your function.

Hope this helps you.

Cheers.

Japan Trivedi
  • 4,445
  • 2
  • 23
  • 44