`
紫_色
  • 浏览: 142783 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Spring mvc系列三之 开启注解

阅读更多

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的注解进行介绍.

分享到:
评论
3 楼 tony.tian 2016-01-13  
游其是你 写道
最后的访问路径是http://localhost/Springmvc/userController/addUser吧?你似乎少加控制器的名称,求回复。



@Controller   这样既可
2 楼 游其是你 2014-03-28  
游其是你 写道
最后的访问路径是http://localhost/Springmvc/userController/addUser吧?你似乎少加控制器的名称,求回复。

博主的文章没有问题,看了下节才知道我错了,
1 楼 游其是你 2014-03-28  
最后的访问路径是http://localhost/Springmvc/userController/addUser吧?你似乎少加控制器的名称,求回复。

相关推荐

Global site tag (gtag.js) - Google Analytics