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中默认的字符串对应映射的请求地址。