您的位置:首页 > 编程语言 > Java开发

springMVC学习笔记---注解启用基本配置

2015-08-06 14:11 471 查看
springMVC最方便是使用注解,减少各个模块之间的耦合性,xml文件规范比较多,使用不方便

Spring的注解是在2.5版本以后,3.0以后完善

启用注解是在spring的配置文件中

扫描包context:component-scan这个是自动扫描这个包下边的所有controller,给每一个注解分配一个URL,如果扫描到controller,自动加到注解中

配置启用注解的bean

在org.springframework.web.servlet.mvc.annotation包中的

DefaultAnnotationHandlerMapping.class,这个类是通过URL找到使用注解的类

AnnotationMethodHandlerAdapter.class这个类是通过类找到使用注解的方法

<context:component-scan base-package="com.web.controller.annotation"></context:component-scan>
<!-- 开启注解扫描包 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>


由于粗心,在这里犯了一个错误,将包找错了,这里注意一下包。

@Controller类的注解

@RequestMapping方法的注解value是请求的URL地址,method是请求的方式,Get方法使用get请求,post方法使用post请求

@Controller
public class UserController {

@RequestMapping(value="/user/addUser",method=RequestMethod.GET)
public ModelAndView addUser() {
String result = "----add----";
return new ModelAndView("/annotation","result",result);
}

@RequestMapping(value="/user/delUser",method=RequestMethod.GET)
public ModelAndView delUser() {
String result = "----del----";
return new ModelAndView("/annotation","result",result);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: