如何通过类级别的RequestMapping调用方法级别的RequestMapping?

4

我使用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>

当我在地址栏中输入此URL时,我得到了精确的输出:http:localhost:8888/classLevel/spring?name=john。但是当我点击HTML页面上设计的按钮时,出现了错误。

3
好的,您的表单操作是 /classLevel,方法映射到 /classLevel/spring,这是非常预期的,不是吗? - JB Nizet
我不知道如何进行映射。 - Faisal
2个回答

1

问题很简单,就在于你的表单提交地址,你使用了 action="/classLevel" ,但应该改为 action="/classLevel/spring",因为你的方法使用了 /spring 作为 RequestMapping。所以请修改为:

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

收件人:

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

因为就像您在URL测试中所做的那样,方法调用应该是:/classLevel/spring
请参阅Spring Docs使用 @RequestMapping 映射请求一节以获取更多信息。

好的。如果我有多个本地RequestMapping,那么我该如何给出路径? - Faisal
好的,我明白你的意思了。但是如果我有多个方法级别的映射,那我该如何映射呢? - Faisal
它与REST url非常相似,只需提供Controller类的RequestMapping,然后是您的方法的RequestMapping:例如/classLevel/spring - cнŝdk
看一下我的编辑中的链接,它会给你你正在寻找的解释。 - cнŝdk

1

如您所知:
在Spring MVC中,您可以将视图作为StringModelAndView对象返回。

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

  1. 如果您在视图名称的开头声明/,则使用绝对路径
    也就是说,它不会考虑类级别@RequestMapping,并直接作为最终视图名称引入自身。
  2. 如果您在视图名称的开头没有声明/,则使用相对路径(相对于类路径),因此它附加类级别@RequestMapping以构建最终视图名称

因此,在使用Spring MVC时,您必须考虑上述注意事项。

例子:
1. 在 spring (boot) 结构的 static 文件夹中创建两个 HTML 文件 test1.htmltest2.html
请注意,类级别的 @RequestMapping 行为相当于相对路径中的 文件夹 路径。

--- 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)]

  1. 创建一个控制器类,如下所示。此示例展示了使用相对路径和绝对路径返回 StringModelAndView 的不同情况。

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


}

注意:
您可以通过ViewResolver(例如在Spring Boot的application.properties文件中使用InternalResourceViewResolverspring.mvc.view.suffix=.html)指定视图文件的后缀,并且不需要在上述代码中声明.html后缀。

此致敬礼


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接