SpringMVC介绍&入门(1)

SpringMVC学习笔记(一)

1、SpringMVC概念

SpringMVC叫做Spring web mvc。它是Spring内置的一个MVC框架,在Spring3.0后发布。SpringMVC框架解决了web开发中比较简单的常见的问题。比如说参数的接收、文件上传、表单验证等内容。

SpringMVCSpring相互连接,形成一个集成。它支持RESTful风格的URL请求。采用了松散耦合可插拔组件结构,比其他 MVC 框架更具扩展性和灵活性。

1.1 SpringMVC原理

在没有使用SpringMVC框架时,使用的是ServletWeb开发。但是在使用Servlet、开发时,像上面说的数据共享,页面跳转等操作较为复杂。servletjava进行web开发的标准。

SpringMVC是对servlet的封装,其底层就是servlet,是对servlet进行深层次的封装

1.2 SpringMVC的优势

  • 基于MVC架构,功能分工明确。它能解决页面代码和后台代码的分离。
  • 简单易用。SpringMVC属于轻量级的,jar很小。它不依赖特定的接口和类就可以开发一个注解的SpringMVC项目
  • 框架的一部分,能够使用springiocaop。能够方便整合其他的框架。
  • springMVC注解强大易用

2、传统的MVC方式

模型1:`jsp+javabean`模型---在`jsp`页面中嵌入大量的`java`代码 
模型2:`jsp+servlet+javabean`模型---`jsp`页面将请求发送给`servlet`,由`servlet`调用`javabean`,再由`servlet`将制定`jsp`页面响应给用户。

模型2一般就是现在的MVC模式,在使用SpringMVC之前一般使用的都是它。

Model-View-Controller:模型–视图–控制器

  • Model:模型层javaBean负责数据访问和业务处理 dao service pojo
  • View:视图 JSP技术负责收集和展示数据
  • Controller:控制器servlet技术中间调度

3、简单创建入门

3.1 创建maven项目(略)

3.2 pom.xml文件添加依赖和插件

<!--web项目-->
<packaging>war</packaging>
    <dependencies>
    <!--spring-webmvc依赖-->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.13.RELEASE</version>
    </dependency>
    <!--springmvc底层还是servlet,所以必须添加serlvet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope><!--插件运行的时候没有范围插件启动会失败-->
</dependency>
</dependencies>
<build>
<plugins>
<!-- 编码和编译和JDK版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!--tomcat插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<port>8080</port>
</configuration>
</plugin>
</plugins>
</build>

3.3 创建SpringSpringMVC配置文件

我们一般将除了把Controller 之外的所有 Bean 注册到 Spring 容器中,而将 Controller 注册到 SpringMVC 容器中。所以我们在 resources 目录下添加 applicationContext.xml 作为 spring 的配置, 添加springmvc.xml作为springmvc的配置文件。

3.3.1 创建spring配置文件
<?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"
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.xsd
">
<!--spring的配置文件:除了控制器之外的bean对象都在这里扫描-->
<context:component-scan base-package="com.kkb.dao,com.kkb.service"/>
</beans>
3.3.2 创建SpringMVC的配置文件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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!--springmvc的配置文件:控制器的bean对象都在这里扫描-->
<context:component-scan base-package="com.kkb.controller"/>
</beans>

3.4 在web.xml中进行SpringSpringMVC配置

1.配置文件搞定
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--spring配置-->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!--springMVC核心控制器-->
<servlet>
	<servlet-name>dispatcherServlet</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 创建前端控制器的时候读取springmvc配置文件启动ioc容器 -->
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:springmvc.xml</param-value>
	</init-param>
	<!-- Tomcat启动就创建此对象 -->
	<load-on-startup>1</load-on-startup>
</servlet>

<!-- 配置拦截路径url,所有以.do结尾的请求都会被前端控制器拦截处理 -->
<servlet-mapping>
	<servlet-name>dispatcherServlet</servlet-name>
	<url-pattern>*.do</url-pattern>
</servlet-mapping>

load-on-startup:标记是否在web服务器。

3.5 创建控制器

@Controller
public class TeamController {
@Autowired
	private TeamService teamService;
	@RequestMapping("hello.do")
	public ModelAndView add(){
		System.out.println("TeamController----add---");
		teamService.add();
		ModelAndView mv=new ModelAndView();
         mv.addObject("teamName","湖人");//相当于
		request.setAttrubuite("teanName","湖人");
		mv.setViewName("index");
//未来经过springmvc的视图解析器处理,转换成物理资源路径,相当于request.getRequestDispatcher("index.jsp").forward();
//经过InternalResourceViewResolver对象的处理之后加上前后缀就变为了/jsp/index.jsp
		return mv;
	}
}

@Service
public class TeamService {
	public void add(){
		System.out.println("TeamService---- add-----");
	}
}

3.6 配置视图解析器

springmvc.xml配置文件中添加视图解析器的配置。

<!--视图解析器-->
<bean id="internalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driven/>

3.7 编写index.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
	<title>index</title>
</head>
<body>
	<h1>index---------------${teamName}</h1>
</body>
</html>

最后运行如下:

在这里插入图片描述

以上就是对springmvc的基本学习和简单的应用。谢谢大家阅读收看。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Xiao艾扶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值