zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    【乱侃】How do they look them ?
    【softeware】Messy code,some bug of Youdao notebook in EN win7
    【随谈】designing the login page of our project
    【web】Ad in security code, making good use of resource
    SQL数据库内存设置篇
    关系数据库的查询优化策略
    利用SQL未公开的存储过程实现分页
    sql语句总结
    sql中使用cmd命令注销登录用户
    SQLServer 分页存储过程
  • 原文地址:https://www.cnblogs.com/shouyaya/p/13028752.html
Copyright © 2011-2022 走看看