一、什么是SpringMvc
SpringMvc是基于MVC设计模式的web框架,属于SpringFrameWork的后续产品。
二、从发起请求到收到请求,内部发生了什么?
(1)首先要有一个概念,Tomcat服务器 = Tomcat引擎 + web应用。
(2)一个请求过来,Tomcat引擎先做以下处理:
*1)接收客户端请求,解析请求
*2)封装代表请求的req和代表回复的resp
*3)调用请求资源(即调用web应用)
(3)web应用里分共有行为和特有行为(POJO)。Tomcat引擎会先调用共有行为,接着共有行为去调用特有行为。
共有行为很复杂,那么谁去充当这一块的内容呢?框架给出了答案,由前端控制器(SpringMVC核心)来充当共有行为。
三、SpringMVC的开发步骤
由上面知道,SpringMVC充当了前端控制器帮我们把共有行为处理了,那么,我们具体应该怎么开发呢?
(1)导入SpringMVC包
(2)配置Servlet,即配置SpringMVC核心控制器DispathcerServlet
(3)编写Controller和视图页面
(4)将Controller使用注解配置到Spring容器中,并且映射地址(@Controller)
(5)配置spring-mvc.xml(配置主键扫描)
(6)测试
四、SpringMVC入门代码实现
目录:
UserDao
package com.xupeng.dao;
public interface UserDao {
public void save();
}
UserDaoImpl
package com.xupeng.dao.impl;
import com.xupeng.dao.UserDao;
public class UserDaoImpl implements UserDao {
public void save() {
System.out.println("save running...");
}
}
UserService
package com.xupeng.service;
public interface UserService {
public void save();
}
UserServiceImpl
package com.xupeng.service.impl;
import com.xupeng.dao.UserDao;
import com.xupeng.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save() {
userDao.save();
}
}
UserServlet:
package com.xupeng.web;
import com.xupeng.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
// ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
UserService userService = (UserService) app.getBean("userService");
userService.save();
}
}
ContextLoaderListener:
package com.xupeng.listener;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ContextLoaderListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
ServletContext servletContext = servletContextEvent.getServletContext();
//读取web.xml中的全局参数
String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
ApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);
//将spring的应用上下文对象存储到servletContext域中
servletContext.setAttribute("app",app);
System.out.println("spring创建成功");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
WebApplicationContextUtils:
package com.xupeng.listener;
import org.springframework.context.ApplicationContext;
import javax.servlet.ServletContext;
public class WebApplicationContextUtils {
public static ApplicationContext getWebApplicationContext(ServletContext servletContext){
return (ApplicationContext)servletContext.getAttribute("app");
}
}
UserController:
package com.xupeng.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@RequestMapping("/quick")
public String save(){
System.out.println("Controller save running...");
return "success.jsp";
}
}
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="userDao" class="com.xupeng.dao.impl.UserDaoImpl"></bean>
<bean id="userService" class="com.xupeng.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/xupeng?serverTimezone=UTC
jdbc.username=root
jdbc.password=root
spring-mvc.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"
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">
<context:component-scan base-package="com.xupeng.controller"></context:component-scan>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置SpringMVC的前端控制器-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--全局初始化参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.xupeng.web.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/userServlet</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
success
</body>
</html>
结果:
五、代码角度解析SpringMVC
(1)我们的url如下
quick之前是tomcat服务器的启动配置(每个人不一样),并且因为是从web前端过来的,因此需要先找到Tomcat的前端控制器。
(2)前端控制器
来到后台之后,想要先找到前端控制器,从哪开始找呢?web项目所有的入口都是web.xml,所以会先找到web.xml。
然后发现web.xml里有一个前端控制器:
截止现在,我们已经找到了前端控制器了。也就是说,url里的http://localhost:8180/spring_mvc_war_exploded这一共有部分被我们找到了。但是后面还有一个quick啊,咋找?
(3)寻找quick(特定部分)
这个quick因为是我们自己配的,每个人可以配不同的名字,所以这算是特定部分。
*1)前端控制器找quick肯定不能漫无目的的找,他也需要一个配置文件,也就是上面的<init-param>里的contextConfigLocation,根据这个他找到了spring-mvc.xml。于是他进入了spring-mvc.xml。(这就跟问路一样,先问web.xml,然后web.xml告诉你去问问spring-mvc.xml看看,于是你来到spring-mvc.xml这里)
*2)结果spring-mvc.xml告诉你,想要找到quick?去com.xupeng.controller目录下面看看吧,为什么去这个目录呢?因为你要找的所有的东西我都放在这个目录下面了(base-package)。
(好的,这时候你会发现,这些人全是大忽悠,就不能爽快点?但是没办法,线索只有一个,只能埋头继续找。)
*3)于是你进入了com.xupeng.controller下面
然后你还看到了quick!!!这就找到了!!!
于是你打印出了那段Controller save running...,接着你根据他的提示,回到了success.jsp这个文件里
*4)打印success.jsp
然后你发现success.jsp是这样的,啊!原来只是打印一个success啊。于是你打印出了success
历经九九八十一难,佛祖(客户)的任务终于完成!
共有条评论 网友评论