【问题标题】:how to call request mapping method level through class level RequestMapping如何通过类级别RequestMapping调用请求映射方法级别
【发布时间】:2015-01-20 10:30:46
【问题描述】:

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

当我在地址栏中提供此网址时。我得到了准确的输出。 http:localhost:8888/classLevel/spring?name=john

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

【问题讨论】:

  • 嗯,你的表单动作是/classLevel,你的方法映射到/classLevel/spring,所以这很符合预期,不是吗?
  • 我不知道如何先映射。

标签: spring spring-mvc


【解决方案1】:

问题在于您的表单操作,您有 action="/classLevel" 应该是 action="/classLevel/spring" 因为您的方法有 /spring 作为 RequestMapping 所以改变:

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

收件人:

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

因为正如您在 url 测试中所做的那样,方法调用应该是:/classLevel/spring。

查看 Spring 文档 的 Mapping Requests With @RequestMapping 部分了解更多信息。

【讨论】:

  • 好的。如果我有多个本地 RequestMapping 手段.. 那我怎么能给出路径
  • 好的,我得到了你所说的答案。但是如果我有不止一种方法级别的映射方法,那么我该如何映射呢?..
  • 它与 REST url 类似,给控制器类 RequestMapping 然后是您方法的 RequestMapping:/classLevel/spring 例如。
  • 看看我编辑中的链接,它会给你你正在寻找的解释。
【解决方案2】:

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

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

  1. 如果您在视图名称的开头声明/,则您使用的是绝对路径。
    即class level@RequestMapping无所谓,直接将自己介绍为final view name。
  2. 如果您不在视图名称的开头声明/,则您使用的是相对路径(相对于类路径),因此它追加到类级别 @RequestMapping 以构造最终视图名称。

所以,你在使用 Spring MVC 时必须考虑以上注意事项。

示例:
1.在spring(boot)结构的static文件夹下创建两个HTML文件test1.html和test2.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. 创建一个控制器类,如下所示。此示例显示了在 relative 和 absolute 路径中返回 String 和 ModelAndView 的不同情况。

@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的appliction.properties文件中的InternalResourceViewResolver或spring.mvc.view.suffix=.html,并且不要在上述代码中声明.html后缀。

最好的问候

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    相关资源
    最近更新 更多