springMVC project configuration

springMVC project configuration


Tip: The following is the content of this article, the following cases are for reference

1 springMVC configuration

1.1 Create Maven webapp project

Insert picture description here

1.2 Import dependencies and plug-ins, complete the file directory

Import spring-webmvc, junit dependency and tomcat plugin in pom.xml

<?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>springmvc20210223</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>springmvc20210223 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>
    <!--导入spring框架包,包含spring框架和springMVC框架-->
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.11.RELEASE</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>springmvc20210223</finalName>
    <!--<pluginManagement>--><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- 导入tomcat插件-->
        <plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat7-maven-plugin</artifactId>
          <version>2.2</version>
          <configuration>
            <port>8066</port>
            <path>/</path>
          </configuration>
        </plugin>
        <!--<plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        &lt;!&ndash; see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging &ndash;&gt;
        <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>

Complete file directory
Insert picture description here

1.3 Placement springmvc.xml

Create new springmvc.xml in the resources directory

1.3.1 Configure annotation driver

Configuration in the beans tag of springmvc.xml

<!-- 1、配置注解驱动:识别注解标志
    spring提供的注解比如@Component @Service @Controller @Repository
    在SpringMVC中能够将带有该注解的类扫描给Spring容器进行管理
    但是要想SpringMVC实现更加完成的功能,SpringMVC又封装自身的注解比如@RequestMapping @GetMapping .....
    -->
    <mvc:annotation-driven></mvc:annotation-driven>

1.3.2 Configure package scanning

The
Insert picture description here
configuration of the beans tag of the newly created package com.qst.controller springmvc.xml in java

<!-- 2、配置包扫描
    将带有@Component @Service @Controller @Repository这些注解的类交给Spring容器进行管理
        base-package:基于哪个包进行扫描,扫描根包所有子包都会被扫描
    -->
    <context:component-scan
            base-package="com.qst"></context:component-scan>

1.3.3 Configure internal resource view resolver

Create a new jsp directory under the WEB-INF directory, create a new login.jsp
Insert picture description here
springmvc.xml bean tag configuration

<!-- 3、内部资源视图解析器
        最终实现把页面的正确路径进行拼接
        /WEB-INF/jsp/   login(返回的视图名)   .jsp
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
         

1.3.4 The complete springmvc.xml file

<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc
						http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
						http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
						http://www.springframework.org/schema/context
          				http://www.springframework.org/schema/context/spring-context-4.0.xsd">


    <!-- 1、配置注解驱动:识别注解标志
    spring提供的注解比如@Component @Service @Controller @Repository
    在SpringMVC中能够将带有该注解的类扫描给Spring容器进行管理
    但是要想SpringMVC实现更加完成的功能,SpringMVC又封装自身的注解比如@RequestMapping @GetMapping .....
    -->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 2、配置包扫描
    将带有@Component @Service @Controller @Repository这些注解的类交给Spring容器进行管理
        base-package:基于哪个包进行扫描,扫描根包所有子包都会被扫描
    -->
    <context:component-scan
            base-package="com.qst"></context:component-scan>

    <!-- 3、内部资源视图解析器
        最终实现把页面的正确路径进行拼接
        /WEB-INF/jsp/   login(返回的视图名)   .jsp
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>


</beans>

1.4 Create a new jsp page

Login.jsp in the WEB-INF/jsp directory

<%--
  Created by IntelliJ IDEA.
  User: 16338
  Date: 2021/2/23
  Time: 13:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>spring配置成功</h1>
</body>
</html>

1.5 New UserController.java

Create a new UserController.java in the package com.qst.controller

package com.qst.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

//当前类被spring容器管理
//当前类是Controller控制器层
@Controller
public class UserController {
    
    
    //当浏览器发起一个http://localhost:8066/login这个请求返回一个登陆页面给浏览器

    @RequestMapping("/login")//请求路径
    public  String toLoginPage(){
    
    
        //暂时不处理任何请求
        return "login";//通过springmvc.xml里配置的内部资源视图解析器返回视图名称,页面要存在,否则返回404

    }
}

1.6 Configure web.xml

First visit web.xml and then springmvc.xml,
so configure web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">

	<!-- 整合springmvc -->
	<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-config.xml</param-value>
		</init-param>
		
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		
		<!-- 拦截所有请求 -->
		<url-pattern>/</url-pattern>
	</servlet-mapping>


</web-app>

2 Run

Run with the tomcat plug-
Insert picture description here
in, the address appears,
Insert picture description here
add /login to the address and
enter the browser http://localhost:8066/login The
configuration is successful
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43881663/article/details/113979587