springMVC框架学习 前端控制器的测试、scl监听器的使用

跋山涉水,今天五节课我有四节,很认真的消化老师讲到的知识,所以今天的进度慢了一些,其实今天大部分的自学时间都在解决Tomcat报出来的一个异常,整迷了我,还好最后敏感的察觉到哪里出问题了,跟着黑马程序员的视频走的话,有时候个性化 的代码就会出现一些难以察觉的错误。一定要细心,不然真的水时间没效率。

今天进度来到了springMVC(AOP后面再学,跟着黑马走),spring框架核心技术中的IOC通过自学掌握几种bean的实例化方法与依赖注入过程。却是很方便,spring容器利用反射原理可以做到实例化bean对象的功能,其中xml文件起到的中间作用特别重要。springMVC最好的一点就是它将Tomcat里web应用过程分为两块,spring框架封装好的前端控制器DispatcherServlet,以及我们自己设置的特定类与接口等等,大大降低了工作量。具体的大家看代码吧!注释可能会有问题,大佬发现麻烦拉我一把,起步的自学水鸟表示非常感激!

1、spring框架学习时没有补上的接口家族

2、spring开发步骤图

 3、com.hlc.controller.UserController.java

package com.hlc.controller;/*
    User:黄林春
    Date:2022/03/07
    Time:18:10
    Project_Name:
    */

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
//springMVC利用注解来设置bean的方法比较快捷,而分层也是MVC最大的亮点,关键在web.xml文件里的配置才是最重要的
@Controller
public class UserController {
//这个注解的作用是请求映射,设置的/hlc是一个自己设置的静态路由,
// 当在端口访问时可以执行这个路由下的方法
    @RequestMapping("/hlc")
    public String jump(){
//  返回一个jump.jsp页面试一下静态页面的跳转
        return "jump.jsp";
    }
}

4、com.hlc.webServlet.webServlet.java

package com.hlc.webServlet;/*
    User:黄林春
    Date:2022/03/07
    Time:18:12
    Project_Name:
    */

import com.hlc.controller.UserController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//这里继承HttpServlet父类的作用很明显要实现去获取ServletContext域中监听器的xml文件资源
public class webServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//  获取上下文,也可以采用this.getServletContext();的方法
        ServletContext servletContext= req.getServletContext();
//  spring框架封装好了监听器,这里能得到这个上下文和web.xml文件里的配置脱不了关系
        WebApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserController u1 = app.getBean(UserController.class);

    }
}

5、spring-mvc.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命名空间去扫描组件以及读取资源文件的方法-->
    <context:component-scan base-package="com.hlc.controller"/>
</beans>

6、web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
<!--  这里对spring-mvc.xml文件进行监听器可获取的专属变量设置,给出类路径读取-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-mvc.xml</param-value>
  </context-param>

<!-- 设置前端控制器DispatcherServet,让servlet服务里的公有行为去帮助我们访问其他的特定行为-->
  <servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
<!--同样的要想实现在前端控制器里对特定行为的访问,必须要将特定行为的资源读取-->
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
<!-- 映射,和servlet成对出现-->
  <servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

<!--使用spring框架提供的监听器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

</web-app>

7、index.jsp

<html>
<body>
<%--这里跳转的是我们在UserController.java中设置好的静态路由/hlc--%>
<a href="/hlc">Hello World!</a>
</body>
</html>

8、jump.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>jumping....</h1>
</body>
</html>

9、pom文件

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>springMVC01</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>springMVC01 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.16</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.3.3</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.3.16</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.16</version>
    </dependency>
    <dependency>
      <groupId>org.mortbay.jetty</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5-20081211</version>
    </dependency>

  </dependencies>

  <build>
    <finalName>springMVC01</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

10、到底是哪里出问题了呢?其实就是在web.xml文件里的spring-mvc.xml文件读取的问题,我没有将这个文件设置为监听器可以拿到的变量,所以导致识别不了总是报出WEB-INF里的xml文件初始化失败的异常。在代码里有标识,哪怕现在不明白也要记住,时间长了就知道原理了。

其实这个就是一个雏形,如果再加上数据库操作是不是就完美了呢?我打算先不着急,进度得慢一些了,太快的话会少很多基础知识的积累。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ForestSpringH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值