2021-02-07

SpringMVC的配置与使用

文章目录

    • SpringMVC的配置与使用
  • 一、添加SpringMVC 的依赖
  • 二、配置web.xml , 注册DispatcherServlet
  • 三、编写SpringMVC 的 配置文件!名称:springmvc-servlet.xml
  • 四、编写我们要操作业务Controller ,要么实现Controller接口,要么增加注解;需要返回一个 ModelAndView,装数据,封视图
  • 五、将自己的类交给SpringIOC容器,注册bean
  • 六、写要跳转的jsp页面,显示ModelandView存放的数据,以及我们的正常页面


一、添加SpringMVC 的依赖

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>3.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>
</dependencies>

二、配置web.xml , 注册DispatcherServlet

<?xml version="1.0" encoding="UTF-8"?>
  <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">
      <!--1.注册DispatcherServlet-->
      <servlet>
          <servlet-name>springmvc</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <!--关联一个springmvc的配置文件:【servlet-name】-servlet.xml-->
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:springmvc-servlet.xml</param-value>
          </init-param>
          <!--启动级别-1-->
          <load-on-startup>1</load-on-startup>
      </servlet>
      <!--/ 匹配所有的请求;(不包括.jsp)--> <!--/* 匹配所有的请求;(包括.jsp)-->
      
          springmvc
          /
      
  

三、编写SpringMVC 的 配置文件!名称:springmvc-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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--添加 处理映射器-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!--    添加 处理器适配器-->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!--    添加 视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>
<bean id="/hello" class="com.JYH.controller.HelloController"></bean>
</beans>

四、编写我们要操作业务Controller ,要么实现Controller接口,要么增加注解;需要返回一个 ModelAndView,装数据,封视图

package com.liu.controller;
  import org.springframework.web.servlet.ModelAndView;
  import org.springframework.web.servlet.mvc.Controller;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  public class HelloController implements Controller {
     
      public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
     
          //ModelAndView 模型和视图
          ModelAndView mv = new ModelAndView();
          //封装对象,放在ModelAndView中。Model
          mv.addObject("msg", "HelloSpringMVC!");
          // 封装要跳转的视图,放在ModelAndView中
          mv.setViewName("hello");
          return mv;
      }
  }

五、将自己的类交给SpringIOC容器,注册bean

<!--Handler-->
  <bean id="/hello" class="com.liu.controller.HelloController"/>

六、写要跳转的jsp页面,显示ModelandView存放的数据,以及我们的正常页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
  <html>
  <head>
      <title>Title</title>
  </head>
  <body>
  ${
     msg}
  </body>
  </html>

你可能感兴趣的:(spring,mvc,servlet,jsp,java)