• 【Java后台】从零开始的Java后台开发(二)


    1. SpringMVC简单使用

    1.1 引入SpringMVC依赖

    pom.xml

     
    
    <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.0modelVersion>
    
      <groupId>org.examplegroupId>
      <artifactId>maven_testartifactId>
      <version>1.0-SNAPSHOTversion>
      <packaging>warpackaging>
    
      <name>maven_test Maven Webappname>
      
      <url>http://www.example.comurl>
    
      <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.7maven.compiler.source>
        <maven.compiler.target>1.7maven.compiler.target>
        <spring.version>4.2.6.RELEASEspring.version>
      properties>
    
      <dependencies>
      
        <dependency>
          <groupId>junitgroupId>
          <artifactId>junitartifactId>
          <version>4.11version>
          <scope>testscope>
        dependency>
        
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-contextartifactId>
          <version>5.2.5.RELEASEversion>
        dependency>
    
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-webmvcartifactId>
          <version>${spring.version}version>
        dependency>
        <dependency>
          <groupId>javax.servletgroupId>
          <artifactId>javax.servlet-apiartifactId>
          <version>4.0.1version>
          
          <scope>providedscope>
        dependency>
        <dependency>
          <groupId>javax.servlet.jspgroupId>
          <artifactId>javax.servlet.jsp-apiartifactId>
          <version>2.3.3version>
          
          <scope>providedscope>
        dependency>
        <dependency>
          <groupId>javax.servletgroupId>
          <artifactId>jstlartifactId>
          <version>1.2version>
        dependency>
    
      dependencies>
    
    
      <build>
        <finalName>maven_testfinalName>
        <pluginManagement>
          <plugins>
            <plugin>
              <artifactId>maven-clean-pluginartifactId>
              <version>3.1.0version>
            plugin>
            
            <plugin>
              <artifactId>maven-resources-pluginartifactId>
              <version>3.0.2version>
            plugin>
            <plugin>
              <artifactId>maven-compiler-pluginartifactId>
              <version>3.8.0version>
            plugin>
            <plugin>
              <artifactId>maven-surefire-pluginartifactId>
              <version>2.22.1version>
            plugin>
            <plugin>
              <artifactId>maven-war-pluginartifactId>
              <version>3.2.2version>
            plugin>
            <plugin>
              <artifactId>maven-install-pluginartifactId>
              <version>2.5.2version>
            plugin>
            <plugin>
              <artifactId>maven-deploy-pluginartifactId>
              <version>2.8.2version>
            plugin>
          plugins>
        pluginManagement>
      build>
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103

    在上面的依赖中,需要对servlet-api和jsp-api加上scope,因为tomcat的依赖包含这两个,如果不加,运行时会报错。这个scope 只能作用在编译和测试时,同时没有传递性。
    在这里插入图片描述

    1.2 创建mvc-dispatcher-servlet.xml

    在这里插入图片描述

    
    
    <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 http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
           
        <context:component-scan base-package="com.crystallake.demo"/>
    	
        <mvc:default-servlet-handler/>
        
        <mvc:annotation-driven/>
        <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <property name="prefix" value="/WEB-INF/pages/"/>
            <property name="suffix" value=".jsp"/>
        bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    InternalResourceViewResolver会将视图名解析为JSP文 件。另外,如果在你的JSP页面中使用了JSP标准标签库 (JavaServer Pages Standard Tag Library,JSTL)的 话,InternalResourceViewResolver能够将视图名解析为 JstlView形式的JSP文件,从而将JSTL本地化和资源bundle变量暴 露给JSTL的格式化(formatting)和信息(message)标签。

    • prefix:前缀,请求的接口带上/WEB-INF/pages/前缀
    • suffix:后缀,请求的接口后面带上.jsp
      这两个参数来自于InternalResourceViewResolver的父类UrlBasedViewResolver,如图所示:
      在这里插入图片描述

    1.3 配置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
            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_3_1.xsd"
            version="3.1">
      <display-name>Archetype Created Web Applicationdisplay-name>
    
      <servlet>
        
        <servlet-name>mvc-dispatcherservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
          <param-name>contextConfigLocationparam-name>
          <param-value>classpath:mvc-dispatcher-servlet.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
      servlet>
    
      <servlet-mapping>
        
        <servlet-name>mvc-dispatcherservlet-name>
        <url-pattern>/url-pattern>
      servlet-mapping>
    
    web-app>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 如果不写任何参数配置(也就是没有servlet和servlet-mapping配置),默认的是在/WEB-INF/applicationContext.xml。
    • 如果指定了要加载的文件(就是配置了init-param),则会去加载相应的xml,而不会去加载/WEB-INF/下的applicationContext.xml。如果没有指定的话,默认会去/WEB-INF/下加载applicationContext.xml。
    • 如果想要自定义文件名,需要在web.xml中加入contextConfigLocation这个context参数并指定classpath,这里默认加载resources文件夹下的xml文件
      在这里插入图片描述

    1.4 MainController创建

    package com.crystallake.demo;
    
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    @Controller
    public class MainController {
    
        @RequestMapping(value = "/",method = RequestMethod.GET)
        public String test(){
            return "main";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    注意,webapp下的index.jsp要删除,不然它默认加载该jsp页面。
    在这里插入图片描述
    maven->clean->package->run
    在这里插入图片描述
    在这里插入图片描述
    http://localhost:8083/maven_test_war_exploded/baseUrl,因为在mvc-dispatcher-servlet.xml里面配置了前缀和后缀,所以它默认会加上前缀/WEB-INF/pages/,然后调用MainControllertest方法返回main,然后自动加上后缀.jsp,所以最终的请求为
    http://localhost:8083/maven_test_war_exploded/WEB-INF/pages/main.jsp

  • 相关阅读:
    SpringMVC项目请求(JSON数据传输参数)
    VSCode 配置 Spring Boot 项目开发环境
    Mesh快连
    Linux 命令(188)—— runlevel 命令
    LVS集群
    基于图像识别的迁移学习之二
    Splunk tag 的利用场景
    Docker 配置阿里云镜像加速器
    博客系统(java,MySQL,HTML)
    使用 Grafana 使用JSON API 请求本地接口 报错 bad gateway(502)解决
  • 原文地址:https://blog.csdn.net/u013293125/article/details/126115690