Spring Annotation 个人笔记

@Controller 表明该类是Handler类

@RequestMapping 表明Handler作用范围 如@RequestMapping("/user") 这个注记 既可以放在类层面+接口 层面上 也可以只放在类层面 或者 接口层面上。

同时, @RequestMapping 支持占位符 例如 @RequestMapping ("/user/*/createUser")  @RequestMapping ("/user/fuck??")   同时 也可以从支持引用传入参数@RequestMapping ("/user/{Uid}")        默认是get

@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable String ownerId, Model model) {
  Owner owner = ownerService.findOwner(ownerId);  
  model.addAttribute("owner", owner);  
  return "displayOwner"; 
}
when a request comes in for  /owners/fred , the value of  ownerId  is  fred .

@ModelAttribute  模型绑定的作用  是否可以将ModelMap中的属性绑定到请求处理方法的入参中呢?答案是肯定的。Spring为此提供了一个@ModelAttribute的注解

用法 :

@ModelAttribute("user")     //1
public User getUser(){
	User user = new User();
	user.setUserId("1001"); 
	return user;
}

@RequestMapping(value = "/handle62")    //2
public String  handle62(@ModelAttribute("user") User user){
	user.setUserName("tom");
	return "/user/showUser";
}
1处的代码用于预定义一个user对象 将他加入modelMap( An  @ModelAttribute  on a method indicates the purpose of that method is to add one or more model attributes.

@RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result) {

    new PetValidator().validate(pet, result);
    if (result.hasErrors()) {
        return "petForm";
    }
    
    // ...
}
同时可以进行对象的校验

      对入参标注@ModelAttribute(“xxx”)的处理方法,Spring MVC按如下流程处理(handle71(@ModelAttribute(“user”) User user)):

1. 如果隐含模型拥有名为xxx的属性,将其赋给该入参,再用请求消息填充该入参对象直接返回,否则到2步 。

2. 如果xxx是会话属性,即在处理类定义处标注了@SessionAttributes("xxx"),则尝试从会话中获取该属性,并将其赋给该入参,然后再用请求消息填充该入参对象。 如果在会话中找不到对应的属性,则抛出HttpSessionRequiredException异常。否则到 3。

3. 如果隐含模型不存在xxx属性,且xxx也不是会话属性,则创建入参的对象实例,再用请求消息填充该入参。


@SessionAttributes
@Controller
@RequestMapping("/editPet.do")
@SessionAttributes("pet")
public class EditPetForm {
    // ...
}
将对象放入seesion域中 

@RequestParam 用于绑定传入参数 
@RequestParam(value = "userName", required = false) 默认为必须 设为不必须后  可传入可不传入
@RequestMapping(value="/handle1")
public String handle1(@RequestParam("userName") String userName,
      @RequestParam("password") String password,
      @RequestParam("realName") String realName){
...
}


@CookieValue
@RequestMapping(value="/handle2")
public String handle2(@CookieValue("JSESSIONID") String sessionId,
       @RequestHeader("Accept-Language") String accpetLanguage){
  ...
}


@Component是所有受Spring管理组件的通用形式,Spring还提供了更加细化的注解形式:@Repository、@Service、@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean。目前版本(2.5)中,这些注解与@Component的语义是一样的,完全通用,在Spring以后的版本中可能会给它们追加更多的语义。所以,我们推荐使用@Repository、@Service、@Controller来替代@Component。 

一些文章地址备份

http://blog.springsource.com/2011/01/04/green-beans-getting-started-with-spring-mvc/

http://viralpatel.net/blogs/2010/06/tutorial-spring-3-mvc-introduction-spring-mvc-framework.html

http://www.iteye.com/topic/1072244

http://www.iteye.com/topic/1120061

http://elf8848.iteye.com/blog/875830


http://danke54.iteye.com/blog/774055

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值