springmvc系列教程一开始springmvc(史上最贴心,原创,亲测)

springmvc系列教程一开始springmvc(史上最贴心,原创,亲测)

我是Kay_Ge,联系qq一起进步早日做构架师

必读
springmvc基于框架的程序要运行成功,对于tomcat版本,jdk版本,配置文件,jar包的版本,有着严格的要求苛刻,但凡有一个地方出错了,都会导致框架运行失败,## 如果这是你第一次学习本框架,请严格按照教程照做,全部一样,直到看见成功在根据不懂的地方做出改变,这样可以大大节省效率,请勿一来就改变配置

步骤1 . 先运行,看效果,在学习深入

项目环境说明:软件eclipse,jdk1.8,tomcat8,springmvc3系列
下面是整个项目
jar包以放在项目中需要可自行提取
链接:https://pan.baidu.com/s/1Sp8mxp3ZMlCao0TsG-M4zg
提取码:2022
点击上方链接下载项目,解压后导入到eclipse中,启动Tomcat,观察是否正常运行。确定可以运行,确定教程是可以跑得起来的,再学习下面的内容。
运行成功画面
在这里插入图片描述
最基础版先了解如何运行所以并没写网页
jar包截图
在这里插入图片描述

步骤 2 . 模仿和排错

在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。

步骤 3 . 整体流程

游览器设置servlet把服务交给springmvc中dispatcherServlet(springmvc框架驱动)全权处理事务,核心控制器使用controller层中类的方法
在这里插入图片描述

步骤4 . 创建项目bird_3_19_springMVC

在eclipse中新建项目springmvc,使用dynamic web project的方式。 不熟悉这种的请自行搜索eclipse怎么创建dynamic web project项目
在这里插入图片描述

步骤5 . 导入jar包

jar包的导入请自行不要更换版本,换了后期不兼容很麻烦,如需配换请自行查看相应适配版本
放入WebContent下web-inf下lib中
在这里插入图片描述

步骤6.编写web.xml

web.xml位置位于WebContent下web-inf下(在WebContent上右键new下order下xml file下取name)
在这里插入图片描述
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>bird_3_19_springMVC</display-name>
  <!--tomcat启动加载路径-->
  <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>
  <!--配置springMVC的核心控制器类 --> <!--下面开始配置服务器  -->
 <servlet>
 <!--name自取   servlet-class不能变-->
 	<servlet-name>dispatcherServlet</servlet-name>
 	<servlet-class>
 			org.springframework.web.servlet.DispatcherServlet
 	</servlet-class>
 	<!--把web.xml与springmvc配置文件xml关联起来  --><!-- 一定不要忘记加classpath-->
 	<init-param>
 			<!--param-name不要动固定写法-->
 			<param-name>contextConfigLocation</param-name>
 			<!-- 一定不要忘记加classpath-->
 			<param-value>classpath:springmvc.xml</param-value>
 	</init-param> 
 </servlet>
 <servlet-mapping>
 <!--servlet-name与上面servlet节点下servlet-name保持一致-->
 	<servlet-name>dispatcherServlet</servlet-name>
 	<!--url-pattern什么路径会扫描  /*是所有文件都扫描-->
 	<url-pattern>/*</url-pattern>
 </servlet-mapping>
</web-app>

步骤7 创建springmvc.xml

右键项目创建源文件夹(项目启动时会把源文件夹的类容加在环境中)右键项目后new找到resource取名就叫resource

在这里插入图片描述
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:aop="http://www.springframework.org/schema/aop" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop   
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
http://www.springframework.org/schema/tx        
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
     <!-- 开启扫描扫描包下面的注解-->
     <context:component-scan base-package="cn.java.controller"></context:component-scan>
     <!-- 加入springmvc特有注解驱动 -->
     <mvc:annotation-driven></mvc:annotation-driven>
     </beans>

步骤8 创建controller层

路径如下 src下创建包cn.java.controller, 包下创建FrontServlet类

package cn.java.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
//下面注解等价于@Component 知识点在于spring的依赖注入把这个类交给spring管理
@Controller
public class FrontServlet {
//以往的方式是得到repuest和reponse对象而现在springmvc
//通过RequestMapping这个注解的方式实现了原来的getpost方法,login.html为当页面为login.html时会访问Login()方法
	@RequestMapping("/login.html")
	public void Login(){
		System.out.println("登录方法");
	}
	//值可以写多个里面的值但凡有下面符合条件的页面都会访问regist()方法
	 
	@RequestMapping({"/a.html","/b.html","/c.html"})
	public void regist(){
		System.out.println("登录方法");
	}
	
	//下面是怎么获取从前端传来有参数的值
	/**
	 * 以前我们通过getparemater来获取参数
	 * 现在我们直接通过形参来接收前端传过来的值
	 * 注意形参要跟前端传过来的参数名一样
	 * http://localhost:8080/bird_3_19_springMVC/h.html?name=123
	 * 也能在地址写参数而形参写实体类 springmvc自动封装到实体类
	 */
	@RequestMapping("h.html")
	public void can(String name){
		System.out.println("can"+name);
	}
}

步骤9 运行项目

部署在Tomcat中,重启tomcat,然后访问地址,观察效果
注意访问地址运行后会报404
原因:我们并没有写任何前端页面,我们要访问
http://localhost:8080/bird_3_19_springMVC/login.html
在这里插入图片描述
可以看见我们虽然没有login.html页面但还是进去了FrontServlet中的Login()方法,证明springmvc在帮我们在一访问login.html不管有没有时都会执行Login()方法

10项目路径完整截图

在这里插入图片描述
后续教程请继续观看springmvc系列教程

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值