springmvc注解

配置文件

springmvc 扫描 Controller

<!-- @Component 包含 @Repository、@Service、@Controller -->
<!-- use-default-filters="true" 会扫描所有 @Component, use-default-filters="false" 自定义扫描的注解 -->
<context:component-scan base-package="Controller存在的包" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

spring 扫描 Service、Dao
使用 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 取消扫描 Controller

注解解释

@RequestMapping
RequestMapping 用来映射 Request 请求与处理器
RequestMapping 注解有六个属性,下面我们把她分成三类进行说明(下面有相应示例)。

1、 value, method

value: 指定请求的实际地址,指定的地址可以是URI Template 模式(后面将会说明);

method: 指定请求的method类型, GET、POST、PUT、DELETE等;
对于这些方法,有专门的注解相对应 @GetMapping@PostMapping@PutMapping@DeleteMapping

2、consumes,produces

consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;

produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;

3、params,headers

params: 指定request中必须包含某些参数值是,才让该方法处理。

headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求

@PathVariable
PathVariable 用来获取 url 中模版变量,和 RequestMapping 配合

@Controller  
public class TestController {  
     @RequestMapping(value="/user/{userId}/roles/{roleId}",method = RequestMethod.GET)  
     public String getLogin(@PathVariable("userId") String userId,  
         @PathVariable("roleId") String roleId){  
         System.out.println("User Id : " + userId);  
         System.out.println("Role Id : " + roleId);  
         return "hello";  
     }   
}

@RequestParam
RequestParam 用来获取 Get 方法提交的参数
RequestParam 用来获取 Post 方法 form 形式提交的参数

@Controller  
@RequestMapping("/pets")    
public class EditPetForm {  
    @RequestMapping(method = RequestMethod.GET)  
    public String setupForm(@RequestParam("petId") int petId, ModelMap model) {  
        Pet pet = this.clinic.loadPet(petId);  
        model.addAttribute("pet", pet);  
        return "petForm";  
   }
} 

@RequestBody
RequestBody 用来获取 Post 方法 json 形式提交的参数转化为 JavaBean

@ResponseBody
ResponseBody 作用在方法上,实现异步返回数据

@Autowired
Autowired 实现变量的注入,默认按照变量类型匹配,作用在字段上可以不写 getter、setter

@Resource
Resource 默认按照变量名匹配
当一个接口有两个实现类时:
@Resource(name=”baseDao”) <=====> @Autowired @Qualifier(“baseDao”)

@ModelAttribute
ModelAttribute 标记的方法将在处理器方法执行之前执行
在方法参数中使用 ModelAttribute 获取 @ModelAttribute 标记的方法执行之后的 model
当 @ModelAttribute 和 @RequestMapping 作用在同一方法上时,返回的是 model 的值

@Controller
@RequestMapping(value="model")
public class ModelAttributeTest {

    @ModelAttribute("pojo")
    public PojoTest init( PojoTest pojo)
    {
        pojo.setSex("男");
        return pojo;

    }

    @RequestMapping(value="modelTest.do")
    public String modelTest(@ModelAttribute("pojo") PojoTest pojo) 
    {
        pojo.setUserName("小明");
        return "modelTest";
    }

}

@Controller
public class HelloWorldController { 
    // 返回值为 void 的时候,使用 Model 返回 model
    @ModelAttribute
    public void populateModel(@RequestParam String abc, Model model) { 
        model.addAttribute("attributeName", abc); 
    } 
    @RequestMapping(value = "/helloWorld") 
    public String helloWorld() { 
        return "helloWorld"; 
    } 
}

@SessionAttributes
当请求结束以后 SessionAttributes 标记的方法或类中的 model 会存在 session 中

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值