目录

默认只有src/main/webapp,其他没有,需手动添加缺失的test和java,并设置目录属性(目录变色)——>resources(两套)

java目录下,再创建一个controller包用于存放控制器——>servlet
(1)添加SpringMVC的依赖
(2)添加Servlet的依赖
(3)添加资源扫描resources
-
- <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>com.dhrj.java.zsitkinggroupId>
- <artifactId>SpringMVCDemo01artifactId>
- <version>1.0version>
- <packaging>warpackaging>
-
- <name>SpringMVCDemo01 Maven Webappname>
-
-
- <properties>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
- <maven.compiler.source>1.8maven.compiler.source>
- <maven.compiler.target>1.8maven.compiler.target>
- properties>
-
- <dependencies>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.13.2version>
- <scope>testscope>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>5.3.21version>
- dependency>
-
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>javax.servlet-apiartifactId>
- <version>4.0.1version>
- <scope>providedscope>
- dependency>
- dependencies>
-
- <build>
-
- <resources>
- <resource>
- <directory>src/main/javadirectory>
- <includes>
- <include>**/*.xmlinclude>
- <include>**/*.propertiesinclude>
- includes>
- resource>
- <resource>
- <directory>src/main/resourcesdirectory>
- <includes>
- <include>**/*.xmlinclude>
- <include>**/*.propertiesinclude>
- includes>
- resource>
- resources>
- build>
- project>

webapp目录下创建用于测试控制器跳转到资源,目录为“admin”,文件为“main.jsp” ,当核心控制器响应请求成功后,跳转到main.jsp,在 页面上显示文本信息“main........”
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Titletitle>
- head>
- <body>
- main.........
- body>
- html>
resources目录下,创建springmvc.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"
- 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.dhrj.java.zsitking.controller"/>
-
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-
- <property name="prefix" value="/admin/"/>
- <property name="suffix" value=".jsp"/>
- bean>
- beans>
删除默认旧的web.xml(版本太低),创建新的web.xml


先改个新名字,否则无法新建成功,会与旧名称重叠。——>先将生成的新web1.xml,覆盖掉旧的web.xml,再将web1.xml重名(shirt+F6)为web.xml

在web.xml文件中注册springMVC框架——>所有的web请求都是基于servlet
(1)注册SpringMVC核心处理器——>中央控制器
(2)引用SpringMVC核心配置文件
(3)通过别名关联匹配Servlet控制器
- <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">
-
- <servlet>
- <servlet-name>springmvcservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
- <init-param>
-
- <param-name>contextConfigLocationparam-name>
-
- <param-value>classpath:springmvc.xmlparam-value>
- init-param>
- servlet>
- <servlet-mapping>
- <servlet-name>springmvcservlet-name>
-
- <url-pattern>*.actionurl-pattern>
- servlet-mapping>
- web-app>
删除默认旧的index.jsp,重新创建index.jsp并修改。
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Titletitle>
- head>
- <body>
- <a href=${pageContext.request.contextPath}/demo.action>访问Tomcat服务器a>
- body>
- html>
在页面中给定一个超链接,服务器请求,指向Servlet控制器webg.xml中,url-pattern设置匹配API接口后缀为action,若想交由SpringMVC的中央控制器处理的话,必须满足后缀规范的条件。
一个普通的类,使用@Controller交由Spring容器IOC创建管理对象。
因SpringMVC的中央控制器处理的是一个普通的类,也就是被@Controller标识的普通类,且最终调用是一个普通方法,也就是普通类中的普通方法,此普通类的返回值即为响应服务器请求的数据————>综上所述,普通方法即为API接口,而普通类中,可以有多个普通方法,也就意味着可以开发多个API接口,全部都是通过中央控制器“DispatcherServlet
”来进行调度。
普通方法的规范:
(1)访问权限是public
(2)方法的返回值任意
(3)方法名称任意
(4)方法可以没有参数,如果有可是任意类型
(5)要使用@RequestMapping注解来声明一个访问的路径
- @Controller //交给Spring去创建对象
- public class DemoAction {
- @RequestMapping(value = "/demo.action")
- public String demo() {
- System.out.println("demo()执行......");
- return "main";
- }
- }
重点注意:@RequestMapping(value = "/demo.action"),必须要带上,本人亲测,不带上出现404错误,网上参考资料有的说不用带,web.xml中设置过“
*.action ”,可是我带上就能正常访问,不带就404。目前不清楚是Spring版本问题,还是Tomcat版本问题。
重点说明:千万不要使用Tomcat10+版本,因为Spring框架最高支持Tomcat9及以下版本。


验证结论:当Tomcat10提供jakarta.servlet与Spring支持javax.servlet类的包路径冲突时,会发生DispatcherServlet相关的500异常错误。请务必使用Tomcat 9及其以下版本。





仅自己学习记录,如有错误,敬请谅解~,谢谢~~~