0

I have a controller that has the following request mapping

@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
public ModelAndView loadUserPage(@PathVariable("id") String id) {

    ModelAndView modelAndView = new ModelAndView("editUser.jsp","editUser", new User());
    modelAndView.addObject("activeUser",getActiveUserByID(id));
    return modelAndView;
}

and in my home.jsp, I have a href link as below

<a href="${pageContext.request.contextPath}/user/${eachUser.userId}">Click Here</a>

But when I click on the above link, the URL directs to localhost:8080/user/1234 (assuming that the user id is 1234) but it throws 404 error saying that the /user/editUser.jsp is not found. I was wondering why the "/user" is appended on to the jsp path?

And I don't hit this issue if I use @RequestParam and change the href URL to "/user?id=${eachUser.userId}".

Shahzeb Khan
  • 3,582
  • 8
  • 45
  • 79
Novice
  • 423
  • 1
  • 9
  • 17

3 Answers3

1

Both of the ways @RequestParam and @PathVariable working with me. Here is the test code and output. I got an error when used ".jsp" in the return statement.

package com.mpro.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.mpro.web.model.User;

@Controller
public class TestController {

    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public ModelAndView loadUserPage(@RequestParam("id") String id) {

        ModelAndView modelAndView = new ModelAndView("testpage","editUser", new User());
        modelAndView.addObject("activeUser",5);
        return modelAndView;
    }

    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
    public ModelAndView loadUserPage1(@PathVariable("id") String id) {

        ModelAndView modelAndView = new ModelAndView("testpage","editUser", new User());
        modelAndView.addObject("activeUser",5);
        return modelAndView;
    }

}

In jsp

<a href="${pageContext.request.contextPath}/user?id=5">Click Here for request parameter</a>
    <a href="${pageContext.request.contextPath}/user/5">Click Here for path variable</a>

and output enter image description here

Shahzeb Khan
  • 3,582
  • 8
  • 45
  • 79
0

You missed adding the path variable argument name, in this case id

@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
public ModelAndView loadUserPage(@PathVariable("id") String id) {
...
}

I think @PathVariable is used when you need to match a pattern in a uri or to get information from the uri. This is why it returns the matched uri. Mainly used in REST.

@RequestParam is mainly used to get a particular parameter from the url. If you need the value of id, this is when you can use @RequestParam.

This is what I have understood after using both of them. Try this and tell me if it makes sense.

rishon1313
  • 98
  • 8
0

When I changed the

new ModelAndView("editUser.jsp","editUser", new User()); 

to

new ModelAndView("/editUser.jsp","editUser", new User());

no extra path was appended. This solved the issue.

Novice
  • 423
  • 1
  • 9
  • 17