mvc配置注解映射器和适配器。

 

 

<!-- 使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置

mvc:annotation-driven默认加载很多的参数绑定方法,

比如json转换解析器就默认加载了,如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMappingRequestMappingHandlerAdapter

实际开发时使用mvc:annotation-driven

 -->

1.springmvc.xml配置

<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    
    <!-- 添加注解模式 可以扫描controller、service、...
    这里让扫描controller,指定controller的包-->
    <context:component-scan base-package="controller"/>
    <!-- 配置注解处理器映射器,功能:寻找执行类Controller -->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!-- 配置sprigmvc视图解析器:解析逻辑视图 后台返回逻辑视图:index 视图解析器解析出真正物理视图:
    前缀+逻辑试图+后缀====/WEB-INF/jsps/index.jsp -->
    <bean        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsps/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

2.controller层控制类编写

package controller;

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

@Controller
public class TestController {

    @RequestMapping("/test.do")
    public ModelAndView handleRequest() {
        ModelAndView mv=new ModelAndView();
        mv.addObject("hello","Welcome To MVC !");
        mv.setViewName("first");
        return mv;
    }
}

3.访问

 

posted @ 2019-10-22 14:17  Cool_Yang  阅读(315)  评论(0编辑  收藏  举报