0

I am a newbie on Spring framework and maybe this is an easy question.

I have a link as follows and attempt Spring controller handles the value"201610061023" of this link.However,my code did not work.

I know this value can be attached as a parameter or pathvariable in path but I just curious can I pass this value implicitly?

Thank you very much.

<a href="./Order" name="test">201610061023</a>

@RequestMapping(value = "/Order")
public String requestHandlingMethod(@ModelAttribute("test") String name, HttpServletRequest request) {
    return "nextpage";
}
Roaid
  • 316
  • 5
  • 17

2 Answers2

0

Your a-tag is wrong, you need to submit the id, there is no implicit way to submit the link-text (except a lot of java script code)!

 <a href="./Order/201610061023" name="test">201610061023</a>


@RequestMapping(value = "/Order/{orderId}")
public String requestHandlingMethod(@PathVariable("orderId") long orderId, @ModelAttribute("test") String name, HttpServletRequest request) {
    return "nextpage";
}

or

 <a href="./Order?orderId=201610061023" name="test">201610061023</a>


@RequestMapping(value = "/Order")
public String requestHandlingMethod(@RequestParam("orderId") long orderId, @ModelAttribute("test") String name, HttpServletRequest request) {
    return "nextpage";
}

See @RequestParam vs @PathVariable for the difference between this two approaches

Community
  • 1
  • 1
Ralph
  • 118,862
  • 56
  • 287
  • 383
  • Thank you for help.I know these two ways and in this case I want to find an implicit way of passing parameter. – Roaid Oct 11 '16 at 12:13
0

Spring will not handle the title of the link simply because the title of the link will not be sent by the browser. To send it you can either:

  1. add the value as parameter: <a href="./Order?id=201610061023" name="test">201610061023</a>
  2. add the value as path variable: <a href="./Order/201610061023" name="test">201610061023</a>
  3. add a JavaScript that will copy the title onClick into the href or send the generated URL with document.location. This can be automated, but it's pretty uncommon.
Boris Schegolev
  • 3,601
  • 5
  • 21
  • 34