SpringMVC demo

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重启。

运行一下服务器
在这里插入图片描述
输入后缀网址后显示页面成功
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值