• springMVC的使用方式


    1.通过maven引入相关的jar包依赖

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.2.6.RELEASE</version>
        </dependency>

    2.在web.xml中配置DispatcherServlet

      <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>    <!--配置spring配置文件的路径-->
          <param-value>classpath:springMVC.xml</param-value>
        </init-param>
      </servlet>
      <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>

    3.配置spring配置文件

    <?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 https://www.springframework.org/schema/context/spring-context.xsd">
    
    <!--开启包扫描-->
        <context:component-scan base-package="com.yzy"></context:component-scan>
    
    <!--配置视图解析器 找到对应的jsp页面-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <!--配置前缀匹配符        -->
            <property name="prefix" value="/"></property>
    <!--配置后缀匹配符        -->
            <property name="suffix" value=".jsp"></property>
        </bean>
    </beans>

    4.根据需求编写对应的handler类

    package com.yzy.controller;
    
    
    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("testHandler")
    public class testHandler {
    
    //    通过Model来实现数据封装传输,最后以字符串的形式返回要将数据返回至对应的页面(view)
        @RequestMapping("/ModelTest")
        public String ModelTest(Model model){
            //此操作相当于在servlet中request.setAttribute("name","model"),下面方法也是如此
            model.addAttribute("name","model");   
            return "index";
        }
        //    通过Map来实现数据封装传输,最后以字符串的形式返回要将数据返回至对应的页面(view)
        @RequestMapping("/MapTest")
        public String MapTest(Map<String,String> map){
            map.put("name","map");
            return "index";
        }
        //    通过ModelAndView来实现数据封装传输,将数据返回至对应的页面(view)同时也封装在此对象中的viewName中
        @RequestMapping("/ModelAndViewTest")
        public ModelAndView ModelAndViewTest(){
            ModelAndView modelAndView=new ModelAndView();
            modelAndView.addObject("name","modelAndView");
            modelAndView.setViewName("index");
            return modelAndView;
        }
    
    
    }

    注意:在web.xml中配置DispatcherServlet时,会使css的样式文件也进入到此servlet来,导致无法正常显示样式,应如下在web.xml中添加设置,使css进入默认的servlet

      <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
      </servlet-mapping>

    在web.xml中配置DispatcherServlet

  • 相关阅读:
    Web用户的身份验证及WebApi权限验证流程的设计和实现
    开源工作流引擎CCFlow 学习专区
    Jquery Ajax方法传值到action
    再谈Jquery Ajax方法传递到action
    Windows下安装GTK+
    Tex使用
    配置Texmaker中文支持
    软件推荐列表(Recommand Software)
    CAD操作
    Package inputenc Error: Unicode char u8: not set up for use with LaTeX.
  • 原文地址:https://www.cnblogs.com/shouyaya/p/13028752.html
Copyright © 2020-2023  润新知