SpringMVC基本使用

SpringMVC基本使用_第1张图片

SpringMVC基本使用

1.导入相关jar包
    <dependencies>
        
        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>testscope>
        dependency>
        
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>5.3.5version>
        dependency>
    dependencies>
2.编写web.xml, 注册DispatcherServlet

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         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">

    <servlet>
        <servlet-name>dispatcherServletservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:application.xmlparam-value>
        init-param>
        
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServletservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
web-app>

/ 与 /* 的区别 :
/ : 匹配所有请求, 但不会匹配jsp请求, 当有jsp请求时, 会直接返回jsp页面
/* : 匹配所有请求, 会匹配jsp请求, 当有jsp请求时, 按普通请求处理, 再次添加jsp后缀

3.编写spring配置文件

<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.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <context:component-scan base-package="com.ml.controller"/>

    <mvc:default-servlet-handler/>

    <mvc:annotation-driven/>


    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    bean>
beans>

使用springMVC必须配置的三大件 :

  • 处理器映射器 : BeanNameUrlHandlerMapping
  • 处理器你适配器 : SimpleControllerHandlerAdapter
  • 资源视图解析器 : InternalResourceViewResolver

通常我们只需要手动配置视图解析器, 因为处理器映射器处理器适配器只需要开启注解驱动即可, 而省去了xml配置

4.创建对应控制类
@Controller
public class HelloController {
     

    @RequestMapping("/hello")
    public String hello(Model model){
     
        model.addAttribute("msg","欢迎学习springmvc");
        return "hello";
    }
}
5.完善前端视图和controller之间的对应
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    hello


${msg}


你可能感兴趣的:(springMVC,spring,java,mvc,web.xml)