SpringMVC的hello的详细教程

SpringMVC基础

1.SpringMVC概述

• Spring 为展现层提供的基于 MVC 设计理念的优秀的
Web 框架,是目前最主流的 MVC 框架之一
• Spring3.0 后全面超越 Struts2,成为最优秀的 MVC 框架
• Spring MVC 通过一套 MVC 注解,让 POJO 成为处理请
求的控制器,而无须实现任何接口。
• 支持 REST 风格的 URL 请求 • 采用了松散耦合可插拔组件结构,比其他 MVC 框架更具
扩展性和灵活性

编写第一个helloWorld
步骤一:导入核心jar包,jar包的作用我就不具体描述,以后再补充。

SpringMVC核心jar包
步骤二:在web.xml中配置DispatcherServlet,打开web.xml添加以下代码

<!-- 配置DispatcherServlet -->
  
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 配置DispatcherServlet的一个初始化参数:配置springmvc的配置文件的名称和路径 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<!-- 作用是让web应用加载的时候这个servlet就被创建而不是第一次请求的时候创建 -->
		<!-- 实际上不用刻意去下面的初始化参数,它有一个默认的配置文件:在WEB-INF/<servlet-name>-servlet.xml ,			    <servlet-name>springDispatcherServlet</servlet-name>的值是可以自定义的-->
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<!-- /表示可以应答所有请求 -->
		<url-pattern>/</url-pattern>
	</servlet-mapping>

步骤三:新建一个springmvc.xml配置文件;
新建一个源文件夹resources然后在其中添加配置文件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"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
		<!-- 配置自动扫描Controller注解的包	 -->
		<context:component-scan base-package="com.springmvc"></context:component-scan>
	
</beans>

步骤四:编写Controller
在这里插入图片描述
1)在src源文件夹下面新建一个包用来存放Controller
2)我们新建一个类随便取一个名字叫HelloWolrd,在class上面加上注解@Controller 。
3) 写一个应答请求的方法,方法上面加上一个注解@RequestMapping(“helloworld”)

package com.sunnylong.springmvcstady.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWord {
 

 @RequestMapping("helloworld")
 public String hello() {
 	return "success";
 }
 
}


步骤五,
(1 . 既然上面加了注解,我们就需要在springmvc.xml中配置注解扫描器,这样注解才会起作用
在springmvc.xml中加入以下代码:

<!-- 配置要扫描的注解所在的包 -->
<context:component-scan base-package="com.sunnylong.springmvcstady"></context:component-scan>

(2.我们看到控制类里的hello方法返回了一个success字符串,那他的作用是什么呢,下面我们再在springmvc.xml中配置一个视图解析器你就应该懂了

<!-- InternalResourceViewResolver视图解析器将返回的字符串由前缀+字符串+后缀组合在一起,解析为一个对应的物理视图,例如/WEB-INF/jsp/success.jsp 然后通过 springDispatcherServlet转发 -->
,例如/WEB-INF/jsp/success.jsp -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

(3.顺着上面,我们是不是应该在WEB-INF下面建一个文件夹jsp,然后在里面新建success.jsp
(4.然后最后我们在webcontent下面新建一个index.jsp 在其中这样写:


<body>
<a href="helloworld">helloworld</a>
</body>

在这里插入图片描述
点击helloworld,于是跳转到
在这里插入图片描述

恭喜你成功了!

想继续学习Springmvc 的同学 可以继续关注我,我会继续在我的CSDN里面不定期更新关于SpringMVC的入门教程!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值