SpringMvc 04 基于注解的映射器与适配器配置

SpringMvc的两种基于注解的映射器与适配器配置:
1.通过显式的配置映射器与适配器,并通过自动扫描标签去加载Controller类。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
  xmlns:p="http://www.springframework.org/schema/p"  
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  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-4.2.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-4.2.xsd  
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
        
        <!-- spring可以自动去烧苗base-package下的包或者子包下的Java文件,如果扫描到有Spring的相关注解的类则会把这些类注册为Spring的bean -->
	<context:component-scan base-package="com.xiaohui.controller" />        
        
        <!-- 配置注解类型的处理映射器 -->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
        
        <!--配置annotation类型的处理器适配器  -->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
		
	<!-- 配置视图渲染器 -->
	<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>

</beans>

通过component-scan 标签可以扫描加有Controller、Service等注解的类进行创建。
2.通过context:annotation-config 标签配置
通过context:annotation-config 标签配置后则可以省略1中的两条显式的配置映射器与适配器。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
  xmlns:p="http://www.springframework.org/schema/p"  
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  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-4.2.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-4.2.xsd  
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
        
        <!-- spring可以自动去烧苗base-package下的包或者子包下的Java文件,如果扫描到有Spring的相关注解的类则会把这些类注册为Spring的bean -->
		<context:component-scan base-package="com.xiaohui.controller" />        
        
		 <!-- context:annotation-config 配置该标签后 则不用配置上面的注解映射器和注解是适配器  -->
		<context:annotation-config/>
		<!-- 配置视图渲染器 -->
		<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>

</beans>


3.基于注解的Controller配置

 

 

 

package com.xiaohui.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {

	@RequestMapping("/hello")
	public ModelAndView hello(){
		
		ModelAndView modelAndView = new ModelAndView();
		//添加数据模型
		modelAndView.addObject("msg", "hello springmvc");
		//设置逻辑试图名,视图解析器会根据改名字解析到具体的视图页面
		modelAndView.setViewName("/WEB-INF/jsp/hello.jsp");
		
		return modelAndView;
	}
	
}

 


controller类需要添加@Controller注解以被Spring配置component-scan标签中扫描加载,@RequestMapping中默认的字符串对应映射的请求地址。

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值