原创不易,转载请注明出处:SpringMVC入门教程及其原理讲解和实例代码下载
代码下载地址:http://www.zuidaima.com/share/1751859714182144.htm
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类:
01 | package com.pb.entity; |
03 | public class Account { |
04 | private String cardNo; |
05 | private String password; |
06 | private float balance; |
07 | public String getCardNo() { |
10 | public void setCardNo(String cardNo) { |
13 | public String getPassword() { |
16 | public void setPassword(String password) { |
17 | this .password = password; |
19 | public float getBalance() { |
22 | public void setBalance( float balance) { |
23 | this .balance = balance; |
LoginController类:
01 | package com.pb.web.controller; |
03 | import java.util.HashMap; |
06 | import javax.servlet.http.HttpServletRequest; |
07 | import javax.servlet.http.HttpServletResponse; |
09 | import org.springframework.web.servlet.ModelAndView; |
10 | import org.springframework.web.servlet.mvc.AbstractController; |
12 | import com.pb.entity.Account; |
14 | public class LoginController extends AbstractController { |
15 | private String successView; |
16 | private String failView; |
18 | public String getSuccessView() { |
21 | public void setSuccessView(String successView) { |
22 | this .successView = successView; |
24 | public String getFailView() { |
27 | public void setFailView(String failView) { |
28 | this .failView = failView; |
31 | protected ModelAndView handleRequestInternal(HttpServletRequest request, |
32 | HttpServletResponse response) throws Exception { |
34 | String cardNo=request.getParameter( "cardNo" ); |
35 | String password=request.getParameter( "password" ); |
36 | Account account =getAccount(cardNo,password); |
37 | Map<String ,Object> model= new HashMap<String,Object>(); |
39 | model.put( "account" , account); |
40 | return new ModelAndView(getSuccessView(),model); |
42 | model.put( "error" , "卡号和密码不正确" ); |
43 | return new ModelAndView(getFailView(),model); |
46 | public Account getAccount(String cardNo,String password){ |
47 | if (cardNo.equals( "123" )&&password.equals( "123" )){ |
48 | Account account = new Account(); |
49 | account.setCardNo(cardNo); |
50 | account.setBalance( 88 .8f); |
applicationContext.xml
01 | <?xml version= "1.0" encoding= "UTF-8" ?> |
07 | xsi:schemaLocation="http: |
10 | <bean id= "loginController" class = "com.pb.web.controller.LoginController" > |
11 | <property name= "successView" value= "showAccount" ></property> |
12 | <property name= "failView" value= "login" ></property> |
14 | <bean id= "urlMapping" class = "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" > |
15 | <property name= "mappings" > |
17 | <prop key= "/login.do" >loginController</prop> |
21 | <bean id= "viewResolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > |
22 | <property name= "prefix" value= "/" ></property> |
23 | <property name= "suffix" value= ".jsp" ></property> |
Jsp页面:
01 | <%@ page language= "java" contentType= "text/html; charset=GB18030" |
02 | pageEncoding= "GB18030" %> |
06 | <meta http-equiv= "Content-Type" content= "text/html; charset=GB18030" > |
07 | <title>Insert title here</title> |
10 | <a href= "login.jsp" >进入</a> |
login.jsp
01 | <%@ page language= "java" contentType= "text/html; charset=GB18030" |
02 | pageEncoding= "GB18030" %> |
06 | <meta http-equiv= "Content-Type" content= "text/html; charset=GB18030" > |
07 | <title>Insert title here</title> |
11 | <form action= "login.do" method= "post" > |
14 | 卡号:<input type= "text" name= "cardNo" ><br> |
15 | 密码:<input type= "text" name= "password" ><br> |
16 | <input type= "submit" value= "登陆" > |
showAccount.jsp
01 | <%@ page language= "java" contentType= "text/html; charset=GB18030" |
02 | pageEncoding= "GB18030" %> |
06 | <meta http-equiv= "Content-Type" content= "text/html; charset=GB18030" > |
07 | <title>Insert title here</title> |
11 | 卡号:${account.cardNo }<br> |
12 | 密码:${account.password }<br> |
13 | 钱数:${account.balance }<br> |
Web.xml
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
07 | < welcome-file >index.jsp</ welcome-file > |
10 | < servlet-name >Dispatcher</ servlet-name > |
11 | < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > |
13 | < param-name >contextConfigLocation</ param-name > |
14 | < param-value >classpath:applicationContext.xml</ param-value > |
18 | < servlet-name >Dispatcher</ servlet-name > |
19 | < url-pattern >*.do</ url-pattern > |
工程原码:原码
SpringMVC总结
以上就是我理解的Spring MVC可能不够深刻。其实对于任何的框架来说,一个框架是一个可复用设计,框架的最大的好处就是复用。每个框架都有存在的理由,那Spring MVC的理由是什么呢?
只有各个框架之间彼此了解他们之间的优缺点,使用场合,使用原理,才能让我们的更快的成长。