基于注解的SpringMVC框架的开发的步骤

  • 1)新建项目,选择maven-archetype-webapp模板
  • 2)修改目录,添加test,java,resources(俩套),并修改目录属性
  • 3)修改pom.xml文件,添加SpringMVC的依赖,添加Servlet的依赖
  • 4)添加springMVC.xml配置文件,指定包扫描,添加视图解析器。
  • 5)删除web.xml文件,新建web.xml文件
  • 6)在web.xml注册SpringMVC框架(所有的web请求都是基于servlet的)
  • 7)删除index.jsp文件,并新建,发送请求给服务器
  • 8)开发控制器(Servlet),它是一个普通的类。
  • 9)添加tomcat进行测试功能

新建webapp项目+目录修改+pom.xml文件修改

  1. new Project->Maven -> 勾选Create from archetype -> 选择maven-archetype-webapp ->项目名,包名->finish

新建好的工程下面只有

项目名
|_src
|__main
|___webapp
|____WEB-INF
|_____web.xml
|____index.jsp
|_pom.xml

我们修改目录

项目名
|_src
|__main
|___java
|___resources
|___webapp
|____WEB-INF
|_____web.xml
|____index.jsp
|__test
|___java
|___resources
|_pom.xml

我们通过File->Project Structrue->Modules快速的修改目录的属性

  • main/java:Sources
  • main/resources:Resources
  • test/java:Tests
  • test/resources: Test Resources

在java包下创建你想要取名的包名.controller

修改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>com.chentianyu</groupId>
  <artifactId>LearnSpringMvcOne_One</artifactId>
  <version>1.0-SNAPSHOT</version>
<!--  <packaging>war</packaging>打包的方式是war包-->
  <packaging>jar</packaging>


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

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!--添加springMVC的依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.16</version>
    </dependency>
    <!--添加servlet依赖-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
    </dependency>
  </dependencies>

  <build>
    <!--指定资源文件-->
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/.properties</include>
        </includes>
      </resource>

      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/.properties</include>
        </includes>
      </resource>
    </resources>
  </build>
</project>

添加springmvc.xml配置文件+main.jsp页面

resources -右键-> new -> XML Configuration File -> Spring Config ->springmvc.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 https://www.springframework.org/schema/context/spring-context.xsd">
    <!--添加包扫描-->
    <context:component-scan base-package="com.chentianyu.controller" />
    <!--添加视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--配置前缀-->
        <property name="prefix" value="/admin/"></property>
        <!--配置后缀-->
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

在webapp目录下删除index.jsp文件,新创建/admin/main.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>main.jsp目录下</title>
</head>
<body>
    <p>main页面</p>
</body>
</html>

这个视图解析器就是来指定跳转到哪个地方的