三、使用注解形式开发 Spring MVC程序

文章目录

  • 一、环境准备
  • [二、配置 web.xml](#二、配置 web.xml)
  • [三、配置 SpringMVC-Servlet.xml ,这里不再使用之前那种写法,直接采用注解配置,引入注解支持,配置视图解析器](#三、配置 SpringMVC-Servlet.xml ,这里不再使用之前那种写法,直接采用注解配置,引入注解支持,配置视图解析器)
  • [四、编写 Controller](#四、编写 Controller)
  • 五、编写要跳转的jsp页面,显示ModelandView存放的数据

一、环境准备

  • 1、创建 maven 项目,添加框架支持

  • 2、添加依赖,因为 maven 可以有一些资源过滤的问题,这里直接将资源过滤配置补全

    xml 复制代码
    <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>
    
        <!-- 导入servlet 和 jsp 的 jar 依赖 -->
        <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>
    
    <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>
  • 3、检查项目结构中是否有 lib 夹以及 jar 是否成功导入了,并配置 tomcat


二、配置 web.xml

xml 复制代码
	<?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.注册servlet-->
	    <servlet>
	        <servlet-name>SpringMVC</servlet-name>
	        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	        <!--通过初始化参数指定SpringMVC配置文件的位置,进行关联-->
	        <init-param>
	            <param-name>contextConfigLocation</param-name>
	            <param-value>classpath:SpringMVC-Servlet.xml</param-value>
	        </init-param>
	        <!-- 启动顺序,数字越小,启动越早 -->
	        <load-on-startup>1</load-on-startup>
	    </servlet>
	
	    <!--所有请求都会被springmvc拦截 -->
	    <servlet-mapping>
	        <servlet-name>SpringMVC</servlet-name>
	        <url-pattern>/</url-pattern>
	    </servlet-mapping>
	
	</web-app>

三、配置 SpringMVC-Servlet.xml ,这里不再使用之前那种写法,直接采用注解配置,引入注解支持,配置视图解析器

  • 这里配置完基本上就不用在动了,统一使用注解来解决

    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
           http://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="com.sys.controller"/>
        <!-- 让Spring MVC不处理静态资源:HTML,JS,CSS,图片,视频等 -->
        <mvc:default-servlet-handler/>
    
        <!-- 支持mvc注解驱动
               在spring中一般采用@RequestMapping注解来完成映射关系
               要想使@RequestMapping注解生效
               必须向上下文中注册DefaultAnnotationHandlerMapping
               和一个AnnotationMethodHandlerAdapter实例
               这两个实例分别在类级别和方法级别处理。
               而annotation-driven配置帮助我们自动完成上述两个实例的注入。
        -->
        <mvc:annotation-driven />
    
        <!-- 视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
              id="internalResourceViewResolver">
            <!-- 前缀 -->
            <property name="prefix" value="/WEB-INF/jsp/" />
            <!-- 后缀 -->
            <property name="suffix" value=".jsp" />
        </bean>
    
    </beans>

四、编写 Controller

  • @Controller:

    @Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象(控制器)。分发处理器将会扫描使用了该注解的类的方法,并检测该方法是否使用了@RequestMapping 注解。@Controller 只是定义了一个控制器类,而使用@RequestMapping 注解的方法才是真正处理请求的处理器。

    • 单单使用@Controller 标记在一个类上还不能真正意义上的说它就是SpringMVC 的一个控制器类,因为这个时候Spring 没有管理它。需要把这个控制器类交给Spring 来管理。

      这个时候有两种方式可以把我们自己的 Conroller 交给Spring 管理,好让它能够识别我们标记的@Controller 。

      第一种方式是在SpringMVC 的配置文件中定义 Conroller 的bean 对象。

      < bean class="com.sys.controller.MyController"/ >

      第二种方式是在SpringMVC 的配置文件中告诉Spring 该到哪里去找标记为@Controller 的Controller 控制器:自动扫描包

      <context:component-scan base-package="com.sys.controller"/>

  • @RequestMapping:

    用来将 URL 映射到控制器中,匹配对应的类或者方法。可以标记在类上,也可以标记在方法上。标记在类上时,层级相当于标记在方法上的父级。

    • 代码示例:

      java 复制代码
      // 使用注解开发
      @Controller
      @RequestMapping("/hello")
      public class HelloController {
      
          /*
          * 当类上标记了@RequestMapping 那么匹配的URL就是:127.0.0.1://8080/hello/h1
          * 如果未标记,那么直接匹配方法上的即可:127.0.0.1://8080/h1
          * */
      
          @RequestMapping("h1")
          public String hello(Model model){
              // 数据封装
              model.addAttribute("msg","Hello Spring MVC!");
              // 返回视图
              return "hello";
          }
      
      }

五、编写要跳转的jsp页面,显示ModelandView存放的数据

html 复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

${msg}

</body>
</html>
  • 运行网址:127.0.0.1://8080/hello/h1
相关推荐
这孩子叫逆9 分钟前
Spring Boot项目的创建与使用
java·spring boot·后端
星星法术嗲人13 分钟前
【Java】—— 集合框架:Collections工具类的使用
java·开发语言
一丝晨光31 分钟前
C++、Ruby和JavaScript
java·开发语言·javascript·c++·python·c·ruby
天上掉下来个程小白34 分钟前
Stream流的中间方法
java·开发语言·windows
xujinwei_gingko1 小时前
JAVA基础面试题汇总(持续更新)
java·开发语言
liuyang-neu1 小时前
力扣 简单 110.平衡二叉树
java·算法·leetcode·深度优先
一丝晨光1 小时前
Java、PHP、ASP、JSP、Kotlin、.NET、Go
java·kotlin·go·php·.net·jsp·asp
罗曼蒂克在消亡1 小时前
2.3MyBatis——插件机制
java·mybatis·源码学习
_GR1 小时前
每日OJ题_牛客_牛牛冲钻五_模拟_C++_Java
java·数据结构·c++·算法·动态规划
无限大.1 小时前
c语言200例 067
java·c语言·开发语言