SpringMVC框架介绍
Spring框架提供了构造Web应用程序的全能MVC模块。Spring
MVC分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行制定。是一个标准的MVC框架。
那你猜一猜哪一部分应该是哪一部分?
SpringMVC框架图
SpringMVC接口解释
DispatcherServlet接口:
Spring提供的前端控制器,所有的请求都有经过它来统一分发。在DispatcherServlet将请求分发给Spring
Controller之前,需要借助于Spring提供的HandlerMapping定位到具体的Controller。
HandlerMapping接口:
能够完成客户请求到Controller映射。
Controller接口:
需要为并发用户处理上述请求,因此实现Controller接口时,必须保证线程安全并且可重用。Controller将处理用户请求,这和Struts
Action扮演的角色是一致的。一旦Controller处理完用户请求,则返回ModelAndView对象给DispatcherServlet前端控制器,ModelAndView中包含了模型(Model)和视图(View)。从宏观角度考虑,DispatcherServlet是整个Web应用的控制器;从微观考虑,Controller是单个Http请求处理过程中的控制器,而ModelAndView是Http请求过程中返回的模型(Model)和视图(View)。
ViewResolver接口:
Spring提供的视图解析器(ViewResolver)在Web应用中查找View对象,从而将相应结果渲染给客户。
SpringMVC运行原理
1.客户端请求提交到DispatcherServlet
2.由DispatcherServlet控制器查询一个或多个HandlerMapping,找到处理请求的Controller
3.DispatcherServlet将请求提交到Controller
4.Controller调用业务逻辑处理后,返回ModelAndView
5.DispatcherServlet查询一个或多个ViewResoler视图解析器,找到ModelAndView指定的视图
6.视图负责将结果显示到客户端
SpringMVC运行实例
Account类:
package com.pb.entity;
public class Account {
private String cardNo;
private String password;
private float balance;
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public float getBalance() {
return balance;
}
public void setBalance(float balance) {
this.balance = balance;
}
}
LoginController类:
package com.pb.web.controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import com.pb.entity.Account;
public class LoginController extends AbstractController {
private String successView;
private String failView;//这两个参数是返回值传给applicationContext.xml,进行页面分配
public String getSuccessView() {
return successView;
}
public void setSuccessView(String successView) {
this.successView = successView;
}
public String getFailView() {
return failView;
}
public void setFailView(String failView) {
this.failView = failView;
}
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
String cardNo=request.getParameter("cardNo");
String password=request.getParameter("password");
Account account =getAccount(cardNo,password);
Map<String ,Object> model=new HashMap<String,Object>();
if(account !=null){
model.put("account", account);
return new ModelAndView(getSuccessView(),model);
}else{
model.put("error", "卡号和密码不正确");
return new ModelAndView(getFailView(),model);
}
}//本应该这个方法写在模型层,这地方直接给放在了逻辑层这个地方偷懒了。
public Account getAccount(String cardNo,String password){
if(cardNo.equals("123")&&password.equals("123")){
Account account =new Account();
account.setCardNo(cardNo);
account.setBalance(88.8f);
return account;
}else{
return null;
}
}
}
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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="loginController" class="com.pb.web.controller.LoginController">
<property name="successView" value="showAccount"></property>
<property name="failView" value="login"></property>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/login.do">loginController</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
Jsp页面:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<a href="login.jsp">进入</a>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
${error }
<form action="login.do" method="post">
账号登陆<br>
<hr>
卡号:<input type="text" name="cardNo"><br>
密码:<input type="text" name="password"><br>
<input type="submit" value="登陆">
</form>
</body>
</html>
showAccount.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
账户信息<br>
卡号:${account.cardNo }<br>
密码:${account.password }<br>
钱数:${account.balance }<br>
</body>
</html>
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:javaee="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
工程原码:原码
SpringMVC总结
以上就是我理解的Spring MVC可能不够深刻。其实对于任何的框架来说,一个框架是一个可复用设计,框架的最大的好处就是复用。每个框架都有存在的理由,那Spring
MVC的理由是什么呢?
只有各个框架之间彼此了解他们之间的优缺点,使用场合,使用原理,才能让我们的更快的成长。
要想看各个框架之间的PK敬请关注:
常用WEB框架Struts1、Struts2和Spring
MVC大PK(一)
常用WEB框架Struts1、Struts2和Spring
MVC大PK(二)
分享到:
相关推荐
3天学会SpringMVC框架教程,视频课程由浅入深逐步讲解,让你能很快的上手,轻松掌握SpringMVC框架。
初上手,直接套json数据进去,然后设置分页方式为client’,很快表格就做出来,但是一般项目中都是使用后台来进行分页的,不可能一下从数据库从捞出成千上万条数据,先不说流量的问题,客户端的渲染也吃力。...
基础教程与练习项目:从基础知识讲起,结合实际练习项目,让您轻松上手SSM开发。 进阶技术与案例分析:针对进阶开发者,提供深入的技术探讨和案例分析,助您深入理解SSM的高级特性。 实战项目与经验分享:通过实际...
为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;...
基础教程与练习项目:从基础知识讲起,结合实际练习项目,让您轻松上手SSM开发。 进阶技术与案例分析:针对进阶开发者,提供深入的技术探讨和案例分析,助您深入理解SSM的高级特性。 实战项目与经验分享:通过实际...
基础教程与练习项目:从基础知识讲起,结合实际练习项目,让您轻松上手SSM开发。 进阶技术与案例分析:针对进阶开发者,提供深入的技术探讨和案例分析,助您深入理解SSM的高级特性。 实战项目与经验分享:通过实际...
基础教程与练习项目:从基础知识讲起,结合实际练习项目,让您轻松上手SSM开发。 进阶技术与案例分析:针对进阶开发者,提供深入的技术探讨和案例分析,助您深入理解SSM的高级特性。 实战项目与经验分享:通过实际...
基础教程与练习项目:从基础知识讲起,结合实际练习项目,让您轻松上手SSM开发。 进阶技术与案例分析:针对进阶开发者,提供深入的技术探讨和案例分析,助您深入理解SSM的高级特性。 实战项目与经验分享:通过实际...
基础教程与练习项目:从基础知识讲起,结合实际练习项目,让您轻松上手SSM开发。 进阶技术与案例分析:针对进阶开发者,提供深入的技术探讨和案例分析,助您深入理解SSM的高级特性。 实战项目与经验分享:通过实际...
为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;...
基础教程与练习项目:从基础知识讲起,结合实际练习项目,让您轻松上手SSM开发。 进阶技术与案例分析:针对进阶开发者,提供深入的技术探讨和案例分析,助您深入理解SSM的高级特性。 实战项目与经验分享:通过实际...
作为一款无需下载安装即可使用的应用,【小程序名称】依托于微信庞大的生态系统,让用户在微信内就能轻松实现各种功能操作。 【小程序名称】的核心功能主要集中在【具体服务领域】,例如在线购物、本地生活服务、...
作为一款无需下载安装即可使用的应用,【小程序名称】依托于微信庞大的生态系统,让用户在微信内就能轻松实现各种功能操作。 【小程序名称】的核心功能主要集中在【具体服务领域】,例如在线购物、本地生活服务、...
作为一款无需下载安装即可使用的应用,【小程序名称】依托于微信庞大的生态系统,让用户在微信内就能轻松实现各种功能操作。 【小程序名称】的核心功能主要集中在【具体服务领域】,例如在线购物、本地生活服务、...
5. **用户体验**:系统提供了清晰直观的用户界面,使得用户能够轻松上手,快速完成记账和查看财务报告。 6. **安全性和可扩展性**:系统在用户认证、权限控制等方面进行了严格的设计,以确保数据的安全。同时,系统...
5. **用户体验**:系统提供了清晰直观的用户界面,使得用户能够轻松上手,快速完成航班查询和预订操作。 6. **安全性和可扩展性**:系统在用户认证、权限控制等方面进行了严格的设计,以确保数据的安全。同时,系统...