zoukankan      html  css  js  c++  java
  • SpringMVC基础 -- 注解开发

    SpringMVC

    ssm: mybatis + Spring + SpringMVC MVC三层架构

     

    使用注解 IDEA 开发基础的 SpringMVC 项目

    第一步: --(配置基础的环境)

    • 创建一个 Maven 项目

    • 导入 依赖包 在pom.xml

      • 主要有 Spring 框架核心库, Spring MVC , Servlet, JSTL 等

      •  <!--导入依赖-->
            <dependencies>
                <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>4.12</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-webmvc</artifactId>
                    <version>5.1.9.RELEASE</version>
                </dependency>
                <dependency>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                    <version>2.5</version>
                </dependency>
                <dependency>
                    <groupId>javax.servlet.jsp</groupId>
                    <artifactId>jsp-api</artifactId>
                    <version>2.2</version>
                </dependency>
                <dependency>
                    <groupId>javax.servlet</groupId>
                    <artifactId>jstl</artifactId>
                    <version>1.2</version>
                </dependency>
            </dependencies>

       

         <!--由于Maven可能存在资源过滤的问题,我们将配置完善-->
        <build>
            <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
                <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
            </resources>
        </build>
       

     

    第二步 --(代码正文)

    • 配置 web.xml

      • 注意点

        • 注意web.xml版本问题, 要最新版! version=4.0

        • 注册 DispatcherServlet

        • 关联 SpringMVC 的配置文件

        • 启动级别为 1

        • 映射路径为 / [不要用/*,会404]

           <?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">
           
               <!--配置DispatchServlet:这个是SpringMVC 的核心: 也叫请求分发器 官方:叫前端控制器-->
               <servlet>
                   <servlet-name>spring-02-annotation</servlet-name>
                   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                   <!--DispatchServlet 要绑定 Spring 的配置文件-->
                   <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>
           
               <!--
                   在springmvc中 / 和 /* 的区别
                   /: 会只匹配所有的请求 不会去匹配jsp页面
                   /*: 匹配所有的请求, 包括jsp页面
               -->
               <!--所有请求都会被springmvc拦截-->
               <servlet-mapping>
                   <servlet-name>spring-02-annotation</servlet-name>
                   <url-pattern>/</url-pattern>
               </servlet-mapping>
           
           </web-app>
    • 配置 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"
              xmlns:context="http://www.springframework.org/schema/context"
              xmlns:mvc="http://www.springframework.org/schema/mvc"
              xsi:schemaLocation="http://www.springframework.org/schema/beans
               https://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">
       
       
           <!-- 自动扫描包,让指定包下的注解生效,由IOC容器统一管理-->
           <context:component-scan base-package="cn.itcast.controller"/>
           <!-- 让 SpringMVC 不处理静态资源 默认过滤 .css .js .html .mp3 .mp4类似的文件-->
           <mvc:default-servlet-handler/>
           <!--
           支持mvc注解驱动
               在 spring 中一般采用@RequestMapping注解来完成映射关系
               要想使@RequestMapping注解生效
               必须向上下文注册DefaultAnnotationHandlerMapping
               和一个 AnnotationMethodHandlerAdapter 实例
               这两个实例分别在类级别和方法级别处理
               而annotation-driven配置帮助我们自动完成上述的两个实例注入
           -->
           <mvc:annotation-driven/>
       
           <!--视图解析器: 模板引擎 Thymeleaf Freemarker-->
           <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
               <!--前缀-->
               <property name="prefix" value="/WEB-INF/jsp/"/>
               <!--后缀-->
               <property name="suffix" value=".jsp"/>
           </bean>
       
       </beans>
      • 在视图解析器中我们把所有的视图都存在/WEB-INF/目录下,这样可以保证视图安全,因为这个目录下的文件,客户端不能直接访问

    创建 Controller

    • 编写一个 java 控制类: cn.itcast.controller.HelloController

       package cn.itcast.controller;
       
       import org.springframework.stereotype.Controller;
       import org.springframework.ui.Model;
       import org.springframework.web.bind.annotation.RequestMapping;
       
       @Controller
       @RequestMapping("/HelloController")
       public class HelloController {
       
           // localhost:8080//HelloController/hello1 --(真正访问地址)
           @RequestMapping("/hello1")
           public String hello1(Model model){
               //封装数据
               model.addAttribute("msg","Hello,SpringMVCAnntation");
       
               return "hello";   //会被视图解析器处理
          }
       
           //多个书写方法
           /*@RequestMapping("/hello2")
           public String hello2(Model model){
               //封装数据
               model.addAttribute("msg","Hello,SpringMVCAnntation");
       
               return "hello";   //会被视图解析器处理
           }*/
       }
       
    • 编写一个 hello.jsp 用于跳转显示

       <%@ page contentType="text/html;charset=UTF-8" language="java" %>
       <html>
       <head>
           <title>Title</title>
       </head>
       <body>
       ${msg}  <!--接收msg参数数据-->
       </body>
       </html>
       
    • 使用 springmvc 必须配置的三大件

      • 处理器映射器 , 处理器适配器 , 视图解析器

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

  • 相关阅读:
    Spring Cloud Eureka(七):DiscoveryClient 源码分析
    Spring Cloud Eureka(六):Eureka Client 如何注册到Eureka Server
    Centos 查看CPU个数、核心数等信息
    Spring Cloud Eureka(五):Eureka Server 启动流程分析
    GlusterFS常用命令
    修改内核参数ip_local_reserved_ports避免tomcat端口占用
    TTM模块安装
    查看磁盘raid信息
    Kubernetes中的PodIP、ClusterIP和外部IP
    ubuntu 14.04.5 kern numa bug
  • 原文地址:https://www.cnblogs.com/lszbk/p/12780360.html
Copyright © 2011-2022 走看看