Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsp 添加路径变量会更改视图路径_Jsp_Spring Mvc_Url Redirection - Fatal编程技术网

Jsp 添加路径变量会更改视图路径

Jsp 添加路径变量会更改视图路径,jsp,spring-mvc,url-redirection,Jsp,Spring Mvc,Url Redirection,我有一个控制器,它具有以下请求映射 @RequestMapping(value = "/user/{id}", method = RequestMethod.GET) public ModelAndView loadUserPage(@PathVariable("id") String id) { ModelAndView modelAndView = new ModelAndView("editUser.jsp","editUser", new User()); modelA

我有一个控制器,它具有以下请求映射

@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;
}
在my home.jsp中,我有一个href链接,如下所示

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

但是当我单击上面的链接时,URL指向localhost:8080/user/1234(假设用户id为1234),但它抛出404错误,表示找不到/user/editUser.jsp。我想知道为什么要将“/user”附加到jsp路径上


如果我使用@RequestParam并将href URL更改为“/user?id=${eachUser.userId}”,我就不会遇到这个问题

您没有添加路径变量参数名称,在本例中为id

@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
public ModelAndView loadUserPage(@PathVariable("id") String id) {
...
}
我认为当您需要匹配uri中的模式或从uri获取信息时,可以使用@PathVariable。这就是它返回匹配uri的原因。主要用于休息

@RequestParam主要用于从url获取特定参数。如果您需要id的值,此时可以使用@RequestParam

这就是我在使用了这两种方法后所理解的。
试试这个,并告诉我它是否有意义。

与我一起工作的两种方法
@RequestParam
@PathVariable
。下面是测试代码和输出。我在return语句中使用“.jsp”时出错

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;
    }

}
在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>

和输出

当我更改

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

没有附加额外的路径。这就解决了问题。

这能回答问题吗?
new ModelAndView("/editUser.jsp","editUser", new User());