Spirng MVC中常用的注解

1、@Controller
告诉Ioc容器该类是一个Controller
例子:
@Controller
public class AddressController {
2、@Autowired
@Autowired 注解可以用于“传统的”setter 方法,如下例:

public class SimpleMovieLister {

private MovieFinder movieFinder;

@Autowired
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}

// ...
}
这个注解也可以用于以属性为参数/多个参数的方法

public class MovieRecommender {

private MovieCatalog movieCatalog;

private CustomerPreferenceDao customerPreferenceDao;

@Autowired
public void prepare(MovieCatalog movieCatalog, CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}

// ...
}
@Autowired注解甚至可以用于构造器与字段:

public class MovieRecommender {

@Autowired
private MovieCatalog movieCatalog;

private CustomerPreferenceDao customerPreferenceDao;

@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
this.customerPreferenceDao = customerPreferenceDao;
}

// ...
}
也可以一种提供来自ApplicationContext的特殊类型的所有 beans,注解字段或者方法,例如:

public class MovieRecommender {

@Autowired
private MovieCatalog[] movieCatalogs;

// ...
}

3、@RequestMapping
根据路径映射到不同的方法上
例子:
@RequestMapping(value = "/member/listaddress.html", method = RequestMethod.GET)
public String getMyAdresses(Model m, HttpServletRequest request) {
4、@PathVariable
获得路径上的参数
例子:
@RequestMapping(value = "/member/address/{id}.html", method = RequestMethod.GET)
public String getEditMyAdresses(@PathVariable("id") String id, Model m, HttpServletRequest request) {
5、@RequestParam
获得request的参数(这个参数是http协议中的消息内容)
例子:
@RequestMapping(value = "/member/updateaddressui.html", method = RequestMethod.GET)
public String updateAddressUI(Model m, HttpServletRequest request, @RequestParam("sid") String sid, @ModelAttribute MemReceptInfoVO memReceptInfoVO) {
6、@ModelAttribute
从servlet的四个容器中获得属性(page,request,session,application)
例子:
public String addMyAddress(Model m, HttpServletRequest request, @ModelAttribute MemReceptInfoVO memReceptInfoVO) {
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值