SpringMVC学习笔记之---深入使用

SpringMVC深入使用

(一)基于XML配置的使用

(1)配置

1.SpringMVC基础配置

2.XML配置Controller,HandlerMapping组件映射

3.XML配置ViewResolver组件映射

(2)步骤

1.在pom.xml中导入jar包

<dependency>
  <groupId>junitgroupId>
  <artifactId>junitartifactId>
  <version>4.11version>
dependency>

<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-webmvcartifactId>
  <version>5.1.3.RELEASEversion>
dependency>
  <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>3.1.0version>
  dependency>

 

2.在web.xml中配置前置控制器

<servlet>

  <servlet-name>SpringMVCservlet-name>

  <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>

  <init-param>

    <param-name>contextConfigLocationparam-name>

    <param-value>classpath:springmvc.xmlparam-value>

  init-param>

servlet>

<servlet-mapping>

  <servlet-name>SpringMVCservlet-name>

  <url-pattern>/url-pattern>

servlet-mapping>

 

3.创建一个MyHandler类,并继承Controller

package handle;



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 MyHandle implements Controller {

    @Override

    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        //装载模型数据和逻辑视图

        ModelAndView modelAndView=new ModelAndView();

        //添加模型数据

        modelAndView.addObject("name","lili");

        //添加逻辑视图

        modelAndView.setViewName("index");

        return modelAndView;

    }

}

 

4.创建springmvc.xml,并在springmvc中配置HandlerMapping,Handler和视图解析器

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 id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

        

        <property name="mappings">

            <props>

                

                <prop key="/test">testHandlerprop>

            props>

        property>

    bean>



    

    <bean id="testHandler" class="handle.MyHandle">bean>



    

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

        

        <property name="prefix" value="/">property>

        

        <property name="suffix" value=".jsp">property>

    bean>

beans>

 

5.在index.jsp页面上显示

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@page isELIgnored="false" %>

<html>

<head>

    <title>Titletitle>

head>

<body>

${name}

body>

html>

 

(二)基于注解方式的使用

(1)配置

1.SpringMVC基础配置

2. Controller,HandlerMapping通过注解进行映射

3.XML配置ViewResolver组件映射

(2)步骤

1.在pom.xml中导入jar包

<dependency>
  <groupId>junitgroupId>
  <artifactId>junitartifactId>
  <version>4.11version>
dependency>

<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-webmvcartifactId>
  <version>5.1.3.RELEASEversion>
dependency>
  <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>3.1.0version>
  dependency>

 

2.在web.xml中配置前置控制器

<servlet>

  <servlet-name>SpringMVCservlet-name>

  <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>

  <init-param>

    <param-name>contextConfigLocationparam-name>

    <param-value>classpath:springmvc.xmlparam-value>

  init-param>

servlet>

<servlet-mapping>

  <servlet-name>SpringMVCservlet-name>

  <url-pattern>/url-pattern>

servlet-mapping>

 

3.创建一个AnnotationHandler类

package handle;



import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;



import java.util.Map;



//让容器知道这个类是以控制器存在的

@Controller

public class AnnotationHandler {

    /**

     * 业务方法1:ModelAndView完成数据的传递,视图的解析

     */

    @RequestMapping("/ModelAndViewTest")

    public ModelAndView ModelAndViewTest(){

        //创建ModelAndView对象

        ModelAndView modelAndView=new ModelAndView();

        //填充模型数据

        modelAndView.addObject("name","Tom");

        //设置逻辑视图

        modelAndView.setViewName("index");

        return modelAndView;

    }

    /**

     * 业务方法2:Model传值,String进行视图解析

     */

    @RequestMapping("/ModelTest")

    public String ModelTest(Model model){

        //填充模型数据

        model.addAttribute("name","Lulu");

        //设置逻辑视图

        return "index";

    }

    /**

     * 业务方法3:Map传值,String进行视图解析

     */

    @RequestMapping("/MapTest")

    public String MapTest(Map map){

        //填充模型数据

        map.put("name","Lisa");

        //设置逻辑视图

        return "index";

    }

}

 

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



    <context:component-scan base-package="handle">context:component-scan>

    

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

        

        <property name="prefix" value="/">property>

        

        <property name="suffix" value=".jsp">property>

    bean>

beans>

 

5.在index.jsp页面上显示

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@page isELIgnored="false" %>

<html>

<head>

    <title>Titletitle>

head>

<body>

${name}

body>

html>

 

你可能感兴趣的:(SpringMVC学习笔记之---深入使用)