java 谁能解释一下类级控制器和方法级控制器之间的区别..?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10425794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
can anybody explain me difference between class level controller and method level controller..?
提问by JOHND
I am new to spring framework....while searching on google..I found few examples which has @RequestMapping annoted at the class level and few examples showing it at menthod level
我是 spring 框架的新手......在谷歌上搜索时......我发现很少有在类级别注释@RequestMapping 的示例,很少有示例在menthod 级别显示它
When to use class level RequestMapping and menthod level RequestMapping annotation...can anybody explain me difference between class level RequestMapping and method level RequestMapping ..??
什么时候使用类级别的 RequestMapping 和方法级别的 RequestMapping 注释……谁能解释一下类级别的 RequestMapping 和方法级别的 RequestMapping 之间的区别..??
so I am little bit confused about their application at :
所以我对他们的申请有点困惑:
a) Class level
a) 班级
b) Method level
b) 方法级别
Also I found some @Requestmapping
with type :GET/Post,whereas some examples doesn't have type parameter.
我还发现了一些@Requestmapping
类型为:GET/Post,而一些示例没有类型参数。
Which approach is better ..??
哪种方法更好......
Is newer versions(>Spring 2.5) don't need parameter type for request mapping ???
较新的版本(> Spring 2.5)不需要请求映射的参数类型吗???
回答by Sean Patrick Floyd
A controller must be marked as @Controller
at the class level. The @RequestMapping
annotation can be applied at both class and method level. If it is, method annotations will be interpreted as relative URLs (relative to the class-level URL). However, method level annotations must be present, otherwise the method won't be mapped.
控制器必须标记为@Controller
类级别。所述@RequestMapping
注释可以在两个类和方法的水平施加。如果是,方法注释将被解释为相对 URL(相对于类级 URL)。但是,必须存在方法级别的注释,否则将不会映射该方法。
In annotations, parameters can be marked as optional with default values. The method parameter is such a case: it defaults to GET
, but can be explicitly set to POST
or something else.
在注解中,参数可以被标记为可选的默认值。方法参数就是这样一种情况:它默认为GET
,但可以显式设置为POST
或其他。
See:
看:
回答by Ghasem Sadeghi
One of the most important differences specially in Spring MVCis ABSOLUTEand RELATIVEpath.
特别是Spring MVC 中最重要的区别之一是绝对路径和相对路径。
As you know:
In the Spring MVC you can return view as a String
or ModelAndView
object.
如您所知:
在 Spring MVC 中,您可以将视图作为String
或ModelAndView
对象返回。
IMPORTANT NOTE:
In both cases you have to pay attention to relative/absolute path:
重要说明:
在这两种情况下,您都必须注意相对/绝对路径:
- If you declare
/
in the beginning of the view name, you are using absolute path.
Namely it does not matter class level@RequestMapping
and directly introduce itself as the final view name. - If you do notdeclare
/
in the beginning of the view name, you are using relative path(relative to the class path) and therefore it appendsto the class level@RequestMapping
to construct final view name.
- 如果
/
在视图名称的开头声明,则使用的是绝对路径。
即类级别无关紧要@RequestMapping
,直接介绍自己作为最终的视图名称。 - 如果您没有
/
在视图名称的开头声明,则您使用的是相对路径(相对于类路径),因此它附加到类级别@RequestMapping
以构造最终视图名称。
So, you have to consider the above notes when use the Spring MVC.
因此,在使用 Spring MVC 时,您必须考虑上述注意事项。
Example:
1. create two HTML file test1.html
and test2.html
in the static
folder of spring (boot) structure:
Please note that the class level@RequestMapping
behaves as a folderpath in the case of relativepath.
实施例:
1.创建两个HTML文件test1.html
和test2.html
中static
弹簧(引导)结构的文件夹:
请注意,类水平@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)]
- create a controller class like as the below. This example shows different cases with return
String
andModelAndView
in both relativeand absolutepath.
- 创建一个如下所示的控制器类。这个例子显示了不同的情况与回报
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;
}
}
Note:
You can specify the suffix of your view files by a ViewResolver
(for example InternalResourceViewResolver
or spring.mvc.view.suffix=.html
in the appliction.properties
file of Spring Boot and do not declare .html
suffix in the above code.
注意:
你可以通过a ViewResolver
(例如InternalResourceViewResolver
或者spring.mvc.view.suffix=.html
在appliction.properties
Spring Boot的文件中)指定你的视图文件的后缀,不要.html
在上面的代码中声明后缀。
Best Regard
最良好的问候
回答by Narendra Kalekar
To answer your last question i.e. which one is better, I would say in production we use combination of these two. For example if there is a user controller we map the class to "/user" and the methods say for getSettings()
will map to "/settings", and method for getPermissions()
will map to "/permissions" etc.
要回答您的最后一个问题,即哪个更好,我会说在生产中我们使用这两者的组合。例如,如果有一个用户控制器,我们将类映射到“/user”,方法说 forgetSettings()
将映射到“/settings”,方法 forgetPermissions()
将映射到“/permissions”等。
You can map these methods directly to "/user/settings" and "/user/permissions" as well skipping class mapping. But mostly we prefer the first approach.
您可以将这些方法直接映射到“/user/settings”和“/user/permissions”,也可以跳过类映射。但大多数情况下,我们更喜欢第一种方法。