springMVC初步学习(2)

来源:互联网 发布:三体 圣母 知乎 编辑:程序博客网 时间:2024/04/30 12:35

1.在springMVC中使用注解(Annotation)能够更好的使得项目解耦,并且比写xml文件更方便。web.xml文件无任何改变,而在springMVC-servlet中,增加关于注解的配置,如下:

<!-- 注解扫描包 --><context:component-scan base-package="com.study.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有关的内容了。

 2.注解的具体使用方法:

  

import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;@Controllerpublic class UserController  {@RequestMapping(value="/user/addUser",method=RequestMethod.GET)// http://localhost:8080/springMVC2/user/addUserpublic ModelAndView addUser(){return new ModelAndView("/welcome","result","addUser");}@RequestMapping(value="/user/delUser",method=RequestMethod.GET)// http://localhost:8080/springMVC2/user/delUserpublic ModelAndView delUser() {return new ModelAndView("/welcome","result","delUser");}}

原创粉丝点击