spring mvc 基于注解的使用,相当于配置文件的使用简单的多.下面讲一下spring mvc 注解的使用
先首确保已经把spring mvc的环境搭配好.这里可以看我的前一篇文章Spring mvc系列一之 Spring mvc简单配置.
先看一下再未使用注解前,spring mvc的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 声明一个Controller中使用多个方法 -->
<bean id="parameterMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<!-- 传参数时用这个作为名称 -->
<property name="paramName" value="action"></property>
</bean>
<!-- 声明DispatcherServlet不要拦截下面声明的目录 -->
<mvc:resources location="/images/" mapping="/images/**"/>
</beans>
上面我们声明在一个控制器中使用多个方法.
再看看spring mvc使用注解配置文件的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 开启注解扫描功能 -->
<context:component-scan base-package="gd.hz.springmvc.controller"></context:component-scan>
<!-- 开启注解 DefaultAnnotationHandlerMapping:映射相应的类,DefaultAnnotationHandlerMapping相应的类方法 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
上面我们开启了注解扫描,注入了AnnotationMethodHandlerAdapter作用是对有RequestMapping注解的控制器进行HTTP路径、HTTP方法和请求参数解析.DefaultAnnotationHandlerMapping作用是映射处理程序方法级别的HTTP路径.在spring 3.1之后由RequestMappingHandlerAdapter和RequestMappingHandlerMapping代替.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 开启注解扫描功能 -->
<context:component-scan base-package="gd.hz.springmvc.controller"></context:component-scan>
<!-- 开启注解 DefaultAnnotationHandlerMapping:映射相应的类,DefaultAnnotationHandlerMapping相应的类方法 -->
<!--
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
-->
<!-- spring 3.1之后由RequestMappingHandlerAdapter和RequestMappingHandlerMapping代替 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 声明DispatcherServlet不要拦截下面声明的目录 -->
<mvc:resources location="/images/" mapping="/images/**"/>
</beans>
上面的两个注解也可以用mvc标签表示,spring 3.1之后默认注入的是RequestMappingHandlerAdapter和RequestMappingHandlerMapping:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 开启注解扫描功能 -->
<context:component-scan base-package="gd.hz.springmvc.controller"></context:component-scan>
<mvc:annotation-driven/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
接下来新建一个控制器:
package gd.hz.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller("userController")
public class UserController {
@RequestMapping("addUser")
public ModelAndView addUser()
{
return new ModelAndView("hello");
}
}
注解 @Controller标志该类为控制器.注解@RequestMapping,映射相应的路径.
我们可以这样访问:http://localhost/Springmvc/addUser , 其中 addUser 就是@RequestMapping映射的名称,名字随意.下一章我们将对spring mvc的注解进行介绍.
本文详细介绍了SpringMVC中使用注解进行配置的方法,包括注解的基本配置步骤及控制器类的创建,并对比了使用注解配置与传统XML配置的区别。
1156

被折叠的 条评论
为什么被折叠?



