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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
Spring 如何通过类级别的RequestMapping调用请求映射方法级别_Spring_Spring Mvc - Fatal编程技术网

Spring 如何通过类级别的RequestMapping调用请求映射方法级别

Spring 如何通过类级别的RequestMapping调用请求映射方法级别,spring,spring-mvc,Spring,Spring Mvc,我用spring做了一个简单的程序。当我没有使用类级RequestMapping时,我得到了方法级RequestMapping的答案。但是我想同时使用类级和方法级的RequestMapping 这是我的控制器代码 package com.birthid; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import o

我用spring做了一个简单的程序。当我没有使用类级RequestMapping时,我得到了方法级RequestMapping的答案。但是我想同时使用类级和方法级的RequestMapping

这是我的控制器代码

package com.birthid;

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

@Controller
@RequestMapping("/classLevel")
public class Controaller1 
{
     @RequestMapping("/spring")
     public ModelAndView display(@RequestParam("name") String name)
     {
         ModelAndView model=new ModelAndView("view");
         model.addObject("msg", name);
         return model;
     }      
}
html代码

<html>
<head>
   <meta http-equiv="content-type" content="text/html; charset=UTF-8">
   <title>Hello App Engine</title>
</head>

<body>
   <h1>valith web application!</h1>
   <form action="/classLevel" method="get">
      name:<input type="text" name="name"/><br>
      <input type="submit" value="clik me"/>
   </form>
</body>
</html>

你好应用程序引擎
valith web应用程序!
名称:
当我在地址栏中给出这个url时。我得到了准确的输出<代码>http:localhost:8888/classLevel/spring?name=john


但当我按下html页面中设计的按钮时,就会出现错误。

问题在于表单操作,您有
action=“/classLevel”
它应该是
action=“/classLevel/spring”
,因为您的方法有
/spring
作为请求映射,所以更改:

<form action="/classLevel" method="get">

致:


因为在url测试中,方法调用应该是:
/classLevel/spring

查看Spring文档的一节以了解更多信息。

如您所知:
在SpringMVC中,您可以将视图作为
字符串
模型和视图
对象返回

重要提示:
在这两种情况下,您都必须注意相对/绝对路径:

  • 如果在视图名称的开头声明
    /
    ,则使用的是绝对路径
    也就是说,它与类级别
    @RequestMapping
    无关,直接将自己作为最终视图名引入
  • 如果没有在视图名称的开头声明
    /
    ,则使用的是相对路径(相对于类路径),因此它附加到类级别
    @RequestMapping
    以构造最终视图名称
  • >,在使用Spring MVC时,必须考虑上面的注释。 示例:
    1.在spring(boot)结构的

    static
    文件夹中创建两个HTML文件
    test1.HTML
    test2.HTML
    : 请注意,类级别
    @RequestMapping
    相对路径的情况下充当文件夹路径。

  • 创建一个控制器类,如下所示。此示例显示了在相对路径绝对路径下返回
    字符串
    模型和视图
    的不同情况
  • 注意:
    您可以通过
    viewsolver
    指定视图文件的后缀(例如
    internalresourceviewsolver
    spring.mvc.view.suffix=.html
    在spring Boot的
    appliction.properties
    文件中,并且不要在上述代码中声明
    .html
    后缀


    最好的注意

    好的,你的表单操作是/classLevel,你的方法被映射到/classLevel/spring,所以这是意料之中的,不是吗?我不知道如何首先映射。好的。如果我有多个本地RequestMapping方法……那么我如何才能给出你所说的pathk我得到的答案。但是如果我有多个方法级映射方法,那么如何我可以映射吗?。它与REST url类似,先给控制器类RequestMapping,然后给你方法的RequestMapping:
    /classLevel/spring
    。看看我编辑中的链接,它会给你你想要的解释。
    <form action="/classLevel/spring" method="get">
    
    --- resources
        --- static
            --- classLevelPath     //behaves as a folder when we use relative path scenario in view names   
                --- test2.html      //this will be used for relative path [case (2)]
            --- test1.html          //this will be used for absolute path [case (1)]
    
    
    
    @Controller
    @RequestMapping("/classLevelPath")
    public class TestController {
    
        //case(1)
        @RequestMapping("/methodLevelAbsolutePath1")
        public String absolutePath1(Model model){
            //model.addAttribute();
            //...
            return "/test1.html";  
        }
    
        //case(1)
        @RequestMapping("/methodLevelAbsolutePath2")
        public ModelAndView absolutePath2(Model model){
            ModelAndView modelAndView = new ModelAndView("/test1.html");
            //modelAndView.addObject()
            //....
            return modelAndView; 
        }
    
        //case(2)
        @RequestMapping("/methodLevelRelativePath1")
        public String relativePath1(Model model){
            //model.addAttribute();
            //...
            return "test2.html";
        }
    
        //case(2)
        @RequestMapping("/methodLevelRelativePath2")
        public ModelAndView relativePath2(Model model){
            ModelAndView modelAndView = new ModelAndView("test2.html");
            //modelAndView.addObject()
            //....
            return modelAndView;  
        }
    
    
    }