Spring MVC 注解类型
Spring 2.5 引入了注解
基于注解的控制器的优势
1. 一个控制器类可以处理多个动作,而一个实现了 Controller 接口的控制器只能处理一个动作
2. 基于注解的控制器的请求映射不需要存储在配置文件中,使用 RequestMapping 注解类型,可以对一个方法进行请求处理。
@Controller 注解类型
org.springframework.stereotype.Controller 注解类型用于指示 Spring 类的实例是一个控制器类
package com.example.controller; import org.springframework.stereotype;
... @Controller
public class CustomerController {
// request-handling methods here
}
Spring 使用扫描机制来找到应用程序中所有基于注解的控制器,但需要做两项配置
1. 在 Spring MVC 的配置文件中声明 spring-context
2. 使用 <component-scan /> 元素指定控制器类的基本包,确保所有的控制器类都在基本包下,并且不要指定一个过于广泛的基本包,防止 Spring 扫描无关的包
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- ... -->
<context:component-scan base-package="com.example.controller" />
<!-- ... -->
</beans>
@RequestMapping 注解类型
org.springframework.web.bind.annotation.RequestMapping 注解类型的作用是映射一个请求和一种方法,可以使用 RequestMapping 注解一种方法或类。
一个采用 @RequestMapping 注解的方法将成为一个请求处理方法,并由调度程序在接收到对应的 URL 请求时调用。
package com.example.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
... @Controller
public class CustomerController { //@RequestMapping("/customer_input")
@RequestMapping(value = "/customer_input") // value 属性将 URI 映射到方法, value是默认属性,如果只有 value 属性的话,可以省略属性名称
public String inputCustomer() {
// do something here
return "CustomerForm"
}
}
@RequestMapping 的属性有,多个属性用逗号分割
value // java.lang.String[] 将 URI 映射到方法,默认属性,只有 value 属性时 可省略属性名
method // RequestMethod[] 指示该方法仅处理那些 HTTP 方法;如果没有指定 method 属性,则请求处理方法可以处理任意 HTTP 方法。
consumes // java.lang.String[]
headers // java.lang.String[]
name // java.lang.String
params // java.lang.String[]
path // java.lang.String[]
produces // java.lang.String[]
@RequestMapping 注解类型也可以用来注解一个控制器类,此时所有方法都将映射为相对于类级别的请求。
例如 控制器类的@RequestMapping 注解的映射使用“/customer”;而方法的 @RequestMapping 注解的映射使用 “/delete”
则映射到该方法上的 URI 为 “http://domain/context/customer/delete”
@Autowired 注解类型
该注解类型属于 org.springframework.beans.factory.annotation 包
作用是将依赖注入到 Spring MVC 控制器
@Service 注解类型
该注解类型属于 org.springframework.stereotype 包
该注解类型指示类是一个服务,类必须要注明为 @Service 才能被作为依赖注入,此外还需要在 Spring MVC 配置文件中添加一个 <component-scan /> 元素来扫描依赖基本包。
<context:component-scan base-package="dependencyPackage" />
@RequestParam 注解类型
该注解类型属于 org.springFramework.web.bind.annotation 包
作用是获取请求参数,@RequestParam 注解的参数类型不一定是字符串
请求参数可以用于发送值给服务器,是 URL 的一部分,请求参数采用 key = value 形式,并用 & 分割。
例如,下面的 URL 带有一个名为 productId 的请求参数,其值为 3;分别用传统方式和 @RequestParam 方式获取请求参数值
http://localhost:8080/app18b/product_retrieve?productId=3
String productId = httpServeltRequest.getParameter("productId"); // 传统方式
public void sendProduct(@RequestParam int productId) // 使用 @RequestParam 注解方式
路径变量, 该变量必须放在花括弧之间 @PathVariable 注解类型
该注解类型属于 org.springFramework.web.bind.annotation 包
路径变量用于发送值到服务器,是 URL 的一部分,路径变量类似于请求参数,但没有 key 部分,只是一个值。类型可以不是字符串。
为了使用路径变量,首先需要在 RequestMapping 注解的值属性中添加一个变量,该变量必须放在花括弧中。
下面的 RequestMapping 注解定义了一个名为 id 的路径变量,然后在方法签名中添加一个同名变量,并加上 @PathVariable 注解。
当该方法被调用是,请求 URL 的 id 值将被复制到路径变量中,并可以在方法中使用。
@RequestMapping(value="/product_view/{id}")
public String viewProduct(@PathVariable Long id, Model model){
Product product = ProductService.get(id);
model.addAttribute("product", product);
return "productView";
}
也可以在请求映射中使用多个路径变量。例如,下面定义了 userId 和 orderId 两个路径变量
@RequestMapping(value = "/product_view/{userId}/{orderId}")
@ModelAttribute 注解类型
该注解类型属于 org.springframework.web.bind.annotation 包。可以用 @ModelAttribute 来注解方法参数或方法。
第一个作用 修饰参数
带 @ModelAttribute 注解的参数会将相应的参数对象添加到 Model 对象中(若方法中没有显示地添加)
// Spring MVC 将在每次调用 submitOrder 方法时创建一个 Order 实例;此方法中,输入或创建的 Order 实例将用 newOrder 键值添加到 Model 对象中。
@RequestMapping(method = RequestMethod.POST)
public String submitOrder(@ModelAttribute("newOrder") Order order, Model model) {
....
}
// 如果未定义键值名,则将使用该对象类型的名称,此方法中,会使用键值 order 将 Order 实例添加到 Model 对象中
@RequestMapping(method = RequestMethod.POST)
public String submitOrder(@ModelAttribute Order order, Model model) {
...
}
第二个作用 修饰非请求处理方法
@ModelAttribute 可以标注一个非请求的处理方法。被 @ModelAttribute 注解的方法会在每次调用该控制器类的请求处理方法时被调用。
这Spring MVC 会在调用请求处理方法之前调用带 @ModelAttribute 注解的方法。意味着,如果一个控制器类有两个请求处理方法,以及一个有 @ModelAttribute 注解的方法,该方法的调用就会比每个处理请求方法更频繁。
带 @ModelAttribute 注解的方法可以返回一个对象或一个 void 类型,如果返回一个对象,则返回的对象会自动添加到 Model 中。如果方法返回 void 则还必须添加一个Model类型的参数,并自行将实例添加到 Model 中。
@ModelAttribute
public Product addProduct(@RequestParam String productId){
...
return productService.get(productId);
} @ModelAttribute
public void populateModel(@RequestParam String id, Model model){
...
model.addAttribute(new Account(id));
}
Spring MVC 注解类型的更多相关文章
- Spring MVC注解的一些案列
1. spring MVC-annotation(注解)的配置文件ApplicationContext.xml <?xml version="1.0" encoding=& ...
- spring mvc 注解@Controller @RequestMapping @Resource的详细例子
现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...
- spring mvc(注解)上传文件的简单例子
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- spring mvc 注解入门示例
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...
- spring mvc 注解示例
springmvc.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...
- spring mvc 形参类型
spring mvc 形参类型 1 没有占位符号的,形参的名字为参数的名称 请求路径为:organtrans/t1/t5?a=1(a为形参的的名称必须一致) @RequestMapping(" ...
- 关于Spring mvc注解中的定时任务的配置
关于spring mvc注解定时任务配置 简单的记载:避免自己忘记,不是很确定我理解的是否正确.有错误地方望请大家指出. 1,定时方法执行配置: (1)在applicationContext.xml中 ...
- Spring MVC 注解[转]
[学习笔记]基于注解的spring3.0.x MVC学习笔记(九) 摘要: 本章节,仅为@SessionAttributes的功能扩展介绍介绍,结合@requestparam注解进行简易无数据库分页. ...
- Spring MVC 注解之controller层
第一层注解:@Controller 和 @RestController. 这两个注解的作用是:处理页面的HTTP请求,不同点 @RestController相当于@Controller +@Respo ...
随机推荐
- go io包
https://studygolang.com/articles/9424 https://blog.csdn.net/trochiluses/article/details/44338407
- Java http请求工具类
该工具类可以调用POST请求或者Get请求,参数以Map的方式传入,支持获获取返回值,返回值接收类型为String HttpRequestUtil.java package com.util; imp ...
- jps,jstack
当java程序发生死循环的时候可以结合使用这两个命令查看死循环发生的位置.
- Hibernate一对多单向关联和双向关联映射方法及其优缺点 (待续)
一对多关联映射和多对一关联映射实现的基本原理都是一样的,既是在多的一端加入一个外键指向一的一端外键,而主要的区别就是维护端不同.它们的区别在于维护的关系不同: 一对多关联映射是指在加载一的一端数据的同 ...
- PHP伪原创同义词替代代码示意
PHP伪原创同义词替代代码示意很多网站后台都是支持PHP,虽然用同义词百度能够识别,但至少比原封不动好些,没有AI原创NLP原创度高,但也有一定的效果.下面就是PHP代码实例: <?phpreq ...
- PHP判断是否都是中文
{ } }
- vue通过ID(参数)修改URL复用同一个页面(组件)不重新加载的问题
项目中经常会用到同一个页面,结构是相同的,我只是在vue-router中通过添加参数的方式来区分状态,参数可以在页面跳转时带上params,或者query,但是有一个问题,即使我们修改了参数,URL也 ...
- 2019年华南理工大学程序设计竞赛(春季赛)-H-Parco_Love_GCD
题目链接:https://ac.nowcoder.com/acm/contest/625/H 题意:给定n个数(<=1e9)的序列,其中n<=5e5,求该序列所有子序列的对应的gcd对1e ...
- CentOS 下搭建Jenkins
1.下载安装包 A 可以连接外网: 导入仓库 wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins.io/redhat-stable/jen ...
- PAT1026 (大模拟)
A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For a ...