读书人

Spring mvc 小编(引文)

发布时间: 2012-12-28 10:29:05 作者: rapoo

Spring mvc 小编(注解)

前不久做web 项目用过spring mvc 觉着这玩意挺不错的,当时也做了很多研究,现在打开自己做的test项目已经很生疏了,所以在此作一篇小记,以便以后使用方便,同时也贡献给大家参考。

?

首先介绍一下Spring 中常用的几个注解标示:

?

1. @Resource? 这个标注是最为常见的,用于注入你想要的instance,提供给当前类使用,使用时有两种方式:

a.在定义成员变量之前 写上 eg:@Resource private JdbcTemplate template;

b.写在set 方法之前

2. @Repository?这个标注是标与注数据库映射的,也就是所谓的Dao 吧

3. @Service? 顾名思义 就是传说中的Service层用的标注。

4. @Controller?说明是一个Controller 也就是就是一个Action

5.@RequestMapping?是Controller中用来配置请求路径的配置在类前面和方法前面

a.配置在类前面就会给所有的该类所有的请求前面加上配置的属性

b.配置在方法前面就是当前请求由这个注解的方法执行并答复

在这个注解中两个常用的配置就是Value 和method? 前面是配置请求路径的,后面的用来配置请求的方式(POST/GET)

6.@ResponseBody?用在执行方法返回值类型前面,注解说明返回作为Response 的body 部分。

7.@PathVariable? 用在执行方法的参数前面,说明该参数是在URL 中包含的。

8.@Valid? 这个是用来说明该参数要组装成该注解的类型?,也是用在执行方法的参数前面,下面就有一个例子:

?

执行的方法:

@RequestMapping(value="/image/{dir}/{name}/dealAreaImage.action",method=RequestMethod.POST)public @ResponseBody Object uploadImage(@PathVariable String dir,@PathVariable String name,@Valid ImageAreaVO imageArea,BindingResult bindingResult) throws IOException, Exception{if(bindingResult.hasErrors()){return bindingResult.getAllErrors();}int width=75,height=75;ByteArrayOutputStream bout=new ByteArrayOutputStream(); ftpService.loadFile(dir, name,bout);byte[] buf = bout.toByteArray();buf=ImgTool.cutArea(buf, imageArea.getX1(),imageArea.getX2(), imageArea.getY1(),imageArea.getY2(),imageArea.getWidth(),imageArea.getHeight(),width,height,imageArea.getBaseW(),imageArea.getBaseH());String filename=System.currentTimeMillis()+ name.substring(name.lastIndexOf('.'));ByteArrayInputStream in=new ByteArrayInputStream(buf);ftpService.saveFile("image", filename,in);Image img=new Image();img.setWidth(width);img.setHeight(height);SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");img.setAddTime(System.currentTimeMillis());//format.parse(image.getAddTime()).getTime());img.setUrl("image/"+filename);img.setNote("区域截图");imageService.saveImage(img);return "success";}

?

?注解成的对象类型:

?

?

package com.my.springmvc.web.vo;public class ImageAreaVO {private Integer width;private Integer height;private Integer x1;private Integer x2;private Integer y1;private Integer y2;private Integer toW;private Integer toH;private Integer baseW;private Integer baseH;public final Integer getToW() {return toW;}public final void setToW(Integer toW) {this.toW = toW;}public final Integer getToH() {return toH;}public final void setToH(Integer toH) {this.toH = toH;}public final Integer getBaseW() {return baseW;}public final void setBaseW(Integer baseW) {this.baseW = baseW;}public final Integer getBaseH() {return baseH;}public final void setBaseH(Integer baseH) {this.baseH = baseH;}public final Integer getWidth() {return width;}public final void setWidth(Integer width) {this.width = width;}public final Integer getHeight() {return height;}public final void setHeight(Integer height) {this.height = height;}public final Integer getX1() {return x1;}public final void setX1(Integer x1) {this.x1 = x1;}public final Integer getX2() {return x2;}public final void setX2(Integer x2) {this.x2 = x2;}public final Integer getY1() {return y1;}public final void setY1(Integer y1) {this.y1 = y1;}public final Integer getY2() {return y2;}public final void setY2(Integer y2) {this.y2 = y2;}}

?好了 常用的注解标记就解说到这。在下篇《Spring mvc 小编(配置)》中讲述Spring mvc 的配置和一些小的问题

读书人网 >VC/MFC

热点推荐