SpringMVC常用注解

SpringMVC常用注解

⒈@RequestParam

@RequestMapping("/hello") public ModelAndView hello
(@RequestParam(value="name",required=true,defaultValue="qingsof t") String name) 

①如果传递的参数名和处理方法的参数不一致,必须通过@RequestParam 注解来指定参数 名;
②可以通过 defaultValue 指定默认值;
③另外可以通过 required 属性指定参数是否必须的,默认值为 true,如果是必须的,但是 没有传入参数,则会报异常:

⒉@SessionAttributes

★ 一般不采用该方法,用session代替
① 在默认情况下,ModelAndView、ModelMap、Model 中的属性作用域是 request 级别, 当本次请求结束后,ModelMap 中的属性将销毁。如果希望在多个请求中共 享 ModelMap 中的属性,必须将其属性转存到 session 中,这样 ModelMap 的属性才 可以被跨请求访问。通过在类上添加 @SessionAttributes 注解可以实现该功能。

@Controller 
@RequestMapping("/configType") 
@SessionAttributes(value="configTypes") 
//@SessionAttributes("configTypes") 会把同名的属性在放到ModelAndView ModelMap Model中时会同时放到session中一份
public class ConfigTypeController {

@Resource 
private ConfigTypeDAO configTypeDAO; 

@RequestMapping("/list") 
public String findAll(ModelMap model) { 
List<ConfigType> configTypes = configTypeDAO.findAll(); 
model.put("configTypes", configTypes); 
return "configType/list"; } 

② @SessionAttributes 还可以通过属性类型指定要 session 化的 ModelMap 属性,以上例子 改为:@SessionAttributes(types=ArrayList.class)也可以。
另外可以指定多个属性名或者类型,使用{},多个之间使用逗号分割,也可以同时指 定属性名和类型。

⒊@ModelAttribute

① 绑定请求参数到指定对象 ,并且该绑定的命令对象以“configType”为名称添加到 模型对象中供视图页面展示使用

public String find(@ModelAttribute("configType") ConfigType configType)

在转发时,会把命令对象传到页面显示,不加该注解,命令对象也会传 递到页面,属性名同参数名,如果加上该注解可以设置属性名

② 暴露@RequestMapping方法返回值为模型数据

	//public @ModelAttribute("questionTypes") List<QuestionType> list()
   @GetMapping("list")
   @ModelAttribute("questionTypes")
   public  List<QuestionType> list()
   {
       return questionTypeService.findAll();
   }

返回模型的属性名为questionTypes,值为方法的返回值及为List。 返回视图名会自动计算,为list.jsp

⒋controllerAdvice 与 ExceptionHandler

@org.springframework.web.bind.annotation.ControllerAdvice
public class ControllerAdvice {

    @ExceptionHandler(NumberFormatException.class)
    public String processNumberFormatException(Exception e)
    {
        System.out.println("数字转换异常");
        return "errornumberformat";
    }



    @InitBinder
    public void initBinder(WebDataBinder binder)
    {
        //针对java.util.Date类型的日期属性注入
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
        binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat,false));
        //针对java.sql.Timestamp类型的日期属性注入
        binder.registerCustomEditor(Timestamp.class,new TimestampEditor("yyyy-MM-dd"));

    }
}

⒌Model

 @GetMapping("list")
    public void list(Model model)
    {
        model.addAttribute("questionTypes",questionTypeService.findAll());
    }

⒍ModelMap

@GetMapping(value = "list/**")
    public void list(ModelMap map)
    {
        //map.addAttribute("questionTypes",questionTypeService.findAll());
        map.put("questionTypes",questionTypeService.findAll());
    }

⒎@RequestMapping

① params属性

 * @RequestMapping(value = "/init", params = {"id=myValue"}) 
 * 只有存在了请求参数id=myValue  /init.action?id=myValue 才会被initData处理 
 * @RequestMapping(value = "/init", params = {"name=kobe", "number=23"})
 *  /init.action?name=kobe&&number=23 否则 404错误 
@RequestMapping(params=“create”, method=RequestMethod.GET) :
表示请求中有 “create”的参数名且请求方法为“GET”即可匹配,
如“http://×××/parameter1?create”
@RequestMapping(params="!create", method=RequestMethod.GET):
表示请求中没有 “create”参数名且请求方法为“GET”即可匹配 
请求数据中指定参数名=值 
@RequestMapping(params="submitFlag=create",method=RequestMethod.GET) 
@RequestMapping(params={"test1", "test2=create"}):
表示请求中的有“test1”参数 名 且 有“test2=create”参数即可匹配 

② produces属性
注解RequestMapping中produces属性可以设置返回数据的类型以及编码,可以是json或者xml:

@RequestMapping(value="/xxx",produces = {"application/json;charset=UTF-8"})
或
@RequestMapping(value="/xxx",produces = {"application/xml;charset=UTF-8"})

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

@Controller  
@RequestMapping(value = "/pets", method = RequestMethod.POST, consumes="application/json")  
public void addPet(@RequestBody Pet pet, Model model) {      
    // implementation omitted  
}  

⒏普通 URL 路径映射,多个路径映射{" “,” "}

 @GetMapping({"list","findAll"})
    public ModelAndView list(Integer pageNum) {

        if(pageNum==null) pageNum=1;
        ModelAndView mv = new ModelAndView();
        PageInfo<Question> pageInfo = questionService.findAll(pageNum);
        mv.addObject("pageInfo",pageInfo);
        return mv;
    }

⒐ant风格的url

ANT 通配符

① ?匹配任何单字符

@RequestMapping(value="/news?“) 
可匹配“/news1”或“/newsa”,但不匹配“/news”或“/newsabc”;

② *匹配 0 或者任意数量的字符

@RequestMapping(value="/news*") 
可匹配“/newsabc”或“/news”或者“/news1” 
@RequestMapping(value="/news/*“)
 可匹配“/news/abc”;

③ **匹配 0 或者更多的目录

@RequestMapping(value="/news/**") 
可以匹配“/news/1/abc”,但“/news/123”将会被【URI 模板模式映射中的“/news/{newsId}” 模式优先映射到】
@RequestMapping(value="/news/**/{newsId}“) 
可匹配“/news/abc/abc/123”或“/news/123”,也就是 Ant 风格和 URI 模板变量风格可 混用。

⒑ RESTful风格的url

REST,即 Representational StateTransfer(“表现层状态转化”)的缩写,如果一个架构 符合 REST 原则,就称它为 RESTful 架构。

① 通过@PathVariable 可以提取 URI 模板模式中的变量

@RequestMapping(value="/news/list/{articleTypeId}/{pageNo}")
public ModelAndView findAll(@PathVariable Integer articleTypeId,
@PathVariable Integer pageNo) 
请求/news/list/12/1,则 articleTypeId 为 12,pageNo 为 1 

② 正则表达式风格的 URL 路径映射
从 Spring3.0 开始支持,格式为{变量名:正则表达式}

@RequestMapping("/list/{configTypeId:\\d+}/{pageNo:\\d+}") 
public String list(@PathVariable Integer configTypeId,
@PathVariable Integer pageNo) 
{ 
	System.out.println("configTypeId="+configTypeId); 
	System.out.println("pageNo="+pageNo);
	return "config/list"; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值