之前的原生的springmvc的开发还是比较的麻烦,现在使用注解进行开发。

web.xml里面还是不变,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 ">
    <!--配置注解扫描-->
    <context:component-scan base-package="com.controller"></context:component-scan>
    <!--配置注解解析器   写了 这个相当于写了处理器和适配器-->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!--配置静态资源放行-->
    <mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
    <mvc:resources mapping="/upload/**" location="/upload/"></mvc:resources>
    <mvc:resources mapping="/images/**" location="/images/"></mvc:resources>

    <!--视图解析器:DispatcherServlet给他的ModelAndView-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>
    <!--配置上传解析bean:给DispatcherServlet使用,调用该bean对象完成request对象中的上传数据的解析-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>

</beans>

controller层使用注解写

package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class mycontroller {
   
    @RequestMapping("/hello")
    public  String hello(Model model){
   
//        封装数据
        model.addAttribute("msg","ddd");
        return "hello";  //会被视图解析器解析
    }
}

解释:

之前的处理器配置,适配器的配置,现在使用一句代码代替了
之前需要在springmvc.xml里面配置controller层的bean对象,现在直接使用注解@controller
之前controller层需要实现controller接口,现在直接使用注解@controller
之前在springMVC里面写路径,现在直接在controller层写 @RequestMapping("/hello")
之前方法的返回是ModelAndView ,现在是String
之前要创建ModelAndView对象,现在直接作为参数
之前返回ModelAndView,现在直接页面名字