1、创建工程Dynamic Web Project
2、导入相关包
3、导入测试网页文件
4、创建和编写配置文件
打开上图显示的web.xml文件,编写内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>web19_ssm_springmvc</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 拦截器 -->
<url-pattern>*.zzz</url-pattern>
</servlet-mapping>
</web-app>
上面内容中的DispatcherServlet路径可以通过下图方法获取:
在src目录下新建springmvc.xml文件
选择Spring Config Editor,前提是先安装SpringIDE(安装详细请看前面文章)
勾选需要的头部声明命名空间
编写springmvc内容,这里的com.zzz是我的根包目录,各位根据自己的项目调整
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 使用注解扫描开发 -->
<context:component-scan base-package="com.zzz"></context:component-scan>
</beans>
5、编写测试文件
先创建文件
编写内容如下:
package com.zzz.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* @ClassName: ItemController
* @Description: 接受请求,显示页面
* @author Administrator
* @date: 2022年3月19日 下午4:42:01
* @Copyright:
*/
@Controller //表示web层,显示页面
public class ItemController {
@RequestMapping(value="list.zzz") //请求网址的后缀路径
public ModelAndView list() {
ModelAndView mav = new ModelAndView();
mav.setViewName("/item_list.jsp");
return mav;
}
}
6、运行测试
小bug,为了使用头部声明,需要先导入相应的dtd文件
关闭springmvc.xml重启。
运行一下服务器
输入后缀网址后显示页面成功