springmvc


依赖导入


            org.mybatis
            mybatis
            3.5.7
        
        
            junit
            junit
            4.12
            test
        
        
            mysql
            mysql-connector-java
            5.1.46
        
        
            log4j
            log4j
            1.2.17
        
        
            org.springframework
            spring-context
            5.2.10.RELEASE
        
        
            org.springframework
            spring-jdbc
            5.2.10.RELEASE
        
        
            org.mybatis
            mybatis-spring
            2.0.7
        
        
        
            org.springframework
            spring-tx
            5.2.5.RELEASE
        
        
            org.aspectj
            aspectjweaver
            1.8.13
        
        
            org.springframework
            spring-webmvc
            5.2.10.RELEASE
        
        
            com.alibaba
            druid
            1.1.16
        

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    
    
        springmvc
        class>org.springframework.web.servlet.DispatcherServletclass>

        
            contextConfigLocation
            classpath:springmvc-servlet.xml
        

        
        1
    
    
        springmvc
        
        /
    

对应的springmvc-servlet.xml


       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">

    


    class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

    class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        
        
        
    

    class="com.smu.controller.HelloController"/>















不使用注解时

public class HelloController implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","HelloSpringMVC");
        mv.setViewName("hello");
        return mv;
    }
}

当使用注解时的对应的springmvc-servlet.xml


       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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 https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    package="com.smu.controller"/>
    
    default-servlet-handler/>
    
    

    
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        
        
        
    




注解时的类
@Controller
public class HelloController {
    @RequestMapping("/hello")
    public String helloController(Model model){
        //封装数据
        model.addAttribute("msg","hello controller annotation");
        return "hello";
    }
}


 RestFul风格

视图解析器存在的时候,会自动拼接为URL

 

 解决编码问题,插入在web.xml中


    
        encoding
        class>org.springframework.web.filter.CharacterEncodingFilterclass>
        
            encoding
            utf-8
        
    
    
        encoding
        /*