SpringWeb MVC 3.0注解实战与解析

需积分: 10 1 下载量 31 浏览量 更新于2024-07-27 收藏 335KB DOC 举报
"本文将介绍Spring Web MVC 3.0中的注解使用,通过实例解析其工作原理,并提供配置示例。" Spring Web MVC 3.0引入了大量的注解,极大地简化了Web应用的开发,使得开发者可以避免过多的XML配置。在Spring MVC中,注解主要分为两大类:类型级别的注解和方法级别的注解。类型级别的注解用于控制器类,而方法级别的注解则用于处理特定HTTP请求的方法。 1. 类型级别注解 - `@Controller`:标记一个类作为Spring MVC的控制器。它定义了一个处理HTTP请求的组件,可以包含多个处理方法。 - `@RequestMapping`:用于映射URL到控制器类。它可以放在类级别上,表示类中的所有方法都处理特定的URL模式。例如,`@RequestMapping("/petclinic")`表示类处理与/petclinic相关的请求。 2. 方法级别注解 - `@RequestMapping`:也可以用于方法级别,此时它会精确地映射某个URL到处理方法。比如,`@RequestMapping(value = "/addPet", method = RequestMethod.POST)`表示当用户发送POST请求到/addPet时,该方法会被执行。 - `@GetMapping`、`@PostMapping`、`@PutMapping`、`@DeleteMapping`:这些是Spring MVC 4.0引入的简化版注解,分别对应HTTP的GET、POST、PUT、DELETE方法,替代了`@RequestMapping`的`method`属性。 3. 参数绑定注解 - `@RequestParam`:用于从请求参数中获取值,例如`@RequestParam("petName") String petName`会将请求参数petName的值赋给petName变量。 - `@PathVariable`:用于从URL模板变量中提取值,如`@GetMapping("/owners/{ownerId}")`,`@PathVariable("ownerId") Long ownerId`会将URL中的{ownerId}部分赋值给ownerId。 - `@RequestBody` 和 `@ResponseBody`:前者用于将请求体内容转换为Java对象,后者将方法返回的对象直接写入响应体,通常与JSON序列化库结合使用。 4. 视图解析注解 - `@ModelAttribute`:用于将方法返回的对象添加到模型中,也可以从请求中绑定请求参数到模型对象。 - `@ResponseBody`:前面提到过,它将方法的返回值直接转换成HTTP响应体,常用于RESTful API开发。 5. 其他注解 - `@InitBinder`:用于初始化数据绑定行为,可以自定义数据转换器或校验规则。 - `@SessionAttributes`:定义哪些模型属性需要存储在session中,以便跨请求共享数据。 配置Spring MVC以支持注解通常涉及以下几个步骤: 1. 启用Spring注解扫描:通过`<context:component-scan>`标签,指定基础包名,Spring会自动扫描并管理带有注解的组件。 ```xml <context:component-scan base-package="com" /> ``` 2. 配置MVC注解处理器:`<mvc:annotation-driven>`标签用于启用Spring MVC的注解处理。 ```xml <mvc:annotation-driven /> ``` 3. 如果需要自定义处理策略,可以显式声明`DefaultAnnotationHandlerMapping`和`AnnotationMethodHandlerAdapter`。 ```xml <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <bean class="org.springframework.web.servlet.mvc.method.annotation.AnnotationMethodHandlerAdapter" /> ``` 实例中的PetClinic项目是一个典型的Spring MVC应用,展示了如何使用注解进行控制器、视图和数据绑定。另一个示例imagedb则强调了无状态多动作控制器以及文件上传的处理。 总结来说,Spring Web MVC 3.0的注解极大提高了开发效率和代码可读性,使得开发者能够更专注于业务逻辑,而不是繁琐的配置。通过理解并熟练运用这些注解,可以构建出高效、灵活的Web应用。

Field error in object 'admin' on field 'id': rejected value [null]; codes [typeMismatch.admin.id,typeMismatch.id,typeMismatch.java.lang.Integer,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [admin.id,id]; arguments []; default message [id]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer' for property 'id'; nested exception is java.lang.NumberFormatException: For input string: "null"] at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:157) ~[spring-web-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:124) ~[spring-web-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161) [spring-web-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:131) [spring-web-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) [spring-webmvc-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:871) [spring-webmvc-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:777) [spring-webmvc-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) [spring-webmvc-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) [spring-webmvc-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) [spring-webmvc-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978) [spring-webmvc-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:881) [spring-webmvc-5.0.2.RELEASE.jar:5.0.2.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:652) [servlet-api.jar:4.0.FR] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855) [spring-webmvc-5.0.2.RELEASE.jar:5.0.2.RELEASE]

2023-05-31 上传