Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.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
Java 在@RequestMapping Spring MVC中找不到@PathVariable[pathVars]_Java_Spring_Spring Mvc - Fatal编程技术网

Java 在@RequestMapping Spring MVC中找不到@PathVariable[pathVars]

Java 在@RequestMapping Spring MVC中找不到@PathVariable[pathVars],java,spring,spring-mvc,Java,Spring,Spring Mvc,我只是一个SpringMVC的新手,下面是我的代码,当我试图去拜拜时,我得到以下错误 Could not find @PathVariable [pathVars] in @RequestMapping Spring MVC 下面是我的代码 package com.springapp.mvc; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org

我只是一个SpringMVC的新手,下面是我的代码,当我试图去拜拜时,我得到以下错误

Could not find @PathVariable [pathVars] in @RequestMapping Spring MVC
下面是我的代码

package com.springapp.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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.servlet.ModelAndView;

import java.util.Map;

@Controller
public class HelloController {
    @RequestMapping("/")
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Hello world!");
        return "hello";
    }
    @RequestMapping("/runThis/{bye}/{hye}")
    public ModelAndView printBye(@PathVariable Map<String,String> pathVars) {
        String Bye =  pathVars.get("bye");
        String Hye =  pathVars.get("hye");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message", "you are"+Bye+ "AND Here COmes" +Hye+"!");
        return modelAndView;
    }
}

通过请求映射,您可以指示URL有一个固定部分/runThis/和两个可变部分{bye}和{hye},并且您希望将它们映射到方法的两个参数,并使用匹配的名称,因为您不指示任何其他内容

但在方法中,您声明了一个名为pathVars的参数。因此,名称、类型和参数数量不匹配。这就是Spring MVC抱怨的问题

所以你想要的是:

@RequestMapping("/runThis/{bye}/{hye}")
public ModelAndView printBye(@PathVariable String bye, @PathVariable String hye) {
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("message", "you are" + bye + "AND Here COmes" + hye + "!");
    return modelAndView;
}

它甚至比您的代码更简单,类型更安全。请阅读文档。这是一个非常基本的示例。

通过请求映射,您可以指示URL有一个固定部分/runThis/和两个可变部分{bye}和{hye},并且您希望它们映射到方法的两个参数,并具有匹配的名称,因为您不指示任何其他内容

但在方法中,您声明了一个名为pathVars的参数。因此,名称、类型和参数数量不匹配。这就是Spring MVC抱怨的问题

所以你想要的是:

@RequestMapping("/runThis/{bye}/{hye}")
public ModelAndView printBye(@PathVariable String bye, @PathVariable String hye) {
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("message", "you are" + bye + "AND Here COmes" + hye + "!");
    return modelAndView;
}
它甚至比您的代码更简单,类型更安全。请阅读文档。这是一个非常基本的示例。

您需要在dispatcher servlet配置xml中使用它

或者在spring的更高版本中使用

@在spring配置类中启用WebMVC注释

请理解@EnableWebMvc是用来取代mvc:annotation-driven的

并确保您的应用程序中的xmlns是正确的

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
您需要在dispatcher servlet配置xml中

或者在spring的更高版本中使用

@在spring配置类中启用WebMVC注释

请理解@EnableWebMvc是用来取代mvc:annotation-driven的

并确保您的应用程序中的xmlns是正确的

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

在某些情况下,如果ModelAndViewjspPageName语句中的jspPageName

指向不存在的页面,您将得到相同的错误


注意:在某些情况下,如果ModelAndViewjspPageName语句中的jspPageName

指向不存在的页面,您将得到相同的错误


注意:我在代码中使用Spring mvc 4.2.4和JDK 8来代替它:

public ModelAndView printBye(@PathVariable Map<String,String> pathVars) {

而不是在代码中使用此选项:

public ModelAndView printBye(@PathVariable Map<String,String> pathVars) {

你认为@PathVariable做什么?我假设path变量用于将请求映射到受尊重的操作。我知道我的PathVar目前是空的,但即使我为它们制作了一些映射器,我仍然会得到相同的错误。不要假设。请阅读。@SotiriosDelimanolis:请检查代码,我做了一个小的修改,我仍然得到相同的错误,请在这里帮助我。您使用了错误的基础结构组件。我怀疑您在使用java配置时缺少或@EnableWebMvc。看起来您使用的是DispatcherServlet默认值,它使用旧的基础结构AnnotationMethodHandlerAdapter,而不是支持此功能的RequestMappingHandlerAdapter。您认为@PathVariable有什么作用?我假设使用path变量将请求映射到受尊重的操作。我知道我的PathVar目前是空的,但即使我为它们制作了一些映射器,我仍然会得到相同的错误。不要假设。请阅读。@SotiriosDelimanolis:请检查代码,我做了一个小的修改,我仍然得到相同的错误,请在这里帮助我。您使用了错误的基础结构组件。我怀疑您在使用java配置时缺少或@EnableWebMvc。看起来您正在使用DispatcherServlet默认值,该默认值使用旧的基础结构AnnotationMethodHandlerAdapter,而不是支持此功能的RequestMappingHandlerAdapter。如果方法参数为Map或MultiValueMap,则会使用所有路径变量名称和值填充Map。感谢快速响应,我在这里完全是个新手,我想问的是,我正在看一个教程,上面写着。@PathVariable映射路径变量可以处理参数化URI请求。所以我想你错过了我想要实现的目标。@SotiriosDelimanolis我知道你可以为映射分配请求参数和HTTP头。它真的也适用于路径变量吗?你有参考文档吗?是的,我引用了javadoc。文档中也提到了这一点。如果方法参数是Map或MultiValueMap,那么Map将填充所有路径变量名称和值。感谢您的快速响应,伙计,我在这里完全是一个新手,我想问的是,我正在看一个教程,其中说。@PathVariable映射路径变量可以处理参数化URI请求。所以我想你错过了我想要实现的目标。@SotiriosDelimanolis我知道你可以为映射分配请求参数和HTTP头。它真的也适用于路径变量吗?你有参考文件吗?是的,我有 引用javadoc。文件中也提到了这一点。