spring MVC配置

从url:http://localhost:8080/graduate/showView.do开始

进入到web.xml

wem.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    
    <!--加载springmvc的核心配置文件-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--采用这种的话则springmvc.xml必须放在web-inf文件下,若是采用initparam配置的话则可以放在其他地方--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>

通过<servlet-mapping>拦截*.do,转交给<servlet>-->DispatcherServlet找到sprinmvc-servlet.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: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.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">


    <mvc:annotation-driven />
    
    <mvc:default-servlet-handler />
    <context:component-scan base-package="controller" />



</beans>

进入到spring.xml,通过<context:component-scan base-package="controller" />扫描controller包,扫描所有带@controller的类,然后通过扫描@requestMapping绑定用户请求,然后执行代码,响应请求

package controller;

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

@Controller

public class MyController {
    @RequestMapping ( "showView" )
    public ModelAndView showView() {
       ModelAndView modelAndView = new ModelAndView();
       modelAndView.addObject( "msg" , " hello world " );
       modelAndView.setViewName( "MyJsp.jsp" );
      
       return modelAndView;
    }

}

以上就是springMVC的简单配置,以及。。。一个大概的流程???算吗???emmm。。。不知道算不算。。。

不知道我觉得这里讲的https://www.bilibili.com/video/BV1r4411k721?p=4应该还好理解

posted @ 2020-04-14 22:12  筱菜鸟  阅读(246)  评论(0编辑  收藏  举报