spring mvc框架

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請求處理過程中的控制器,而ModelAndViewHttp請求過程中返回的模型(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類:

Java代碼
  1. package com.pb.entity;
  2. public class Account {
  3. private String cardNo;
  4. private String password;
  5. private float balance;
  6. public String getCardNo() {
  7. return cardNo;
  8. }
  9. public void setCardNo(String cardNo) {
  10. this.cardNo = cardNo;
  11. }
  12. public String getPassword() {
  13. return password;
  14. }
  15. public void setPassword(String password) {
  16. this.password = password;
  17. }
  18. public float getBalance() {
  19. return balance;
  20. }
  21. public void setBalance(float balance) {
  22. this.balance = balance;
  23. }
  24. }

LoginController類:

Java代碼
  1. package com.pb.web.controller;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import org.springframework.web.servlet.ModelAndView;
  7. import org.springframework.web.servlet.mvc.AbstractController;
  8. import com.pb.entity.Account;
  9. public class LoginController extends AbstractController {
  10. private String successView;
  11. private String failView;//這兩個參數是返回值傳給applicationContext.xml,進行頁面分配
  12. public String getSuccessView() {
  13. return successView;
  14. }
  15. public void setSuccessView(String successView) {
  16. this.successView = successView;
  17. }
  18. public String getFailView() {
  19. return failView;
  20. }
  21. public void setFailView(String failView) {
  22. this.failView = failView;
  23. }
  24. @Override
  25. protected ModelAndView handleRequestInternal(HttpServletRequest request,
  26. HttpServletResponse response) throws Exception {
  27. // TODO Auto-generated method stub
  28. String cardNo=request.getParameter("cardNo");
  29. String password=request.getParameter("password");
  30. Account account =getAccount(cardNo,password);
  31. Map<String ,Object> model=new HashMap<String,Object>();
  32. if(account !=null){
  33. model.put("account", account);
  34. return new ModelAndView(getSuccessView(),model);
  35. }else{
  36. model.put("error", "卡號和密碼不正確");
  37. return new ModelAndView(getFailView(),model);
  38. }
  39. }//本應該這個方法寫在模型層,這地方直接給放在了邏輯層這個地方偷懶了。
  40. public Account getAccount(String cardNo,String password){
  41. if(cardNo.equals("123")&&password.equals("123")){
  42. Account account =new Account();
  43. account.setCardNo(cardNo);
  44. account.setBalance(88.8f);
  45. return account;
  46. }else{
  47. return null;
  48. }
  49. }
  50. }
applicationContext.xml

Html代碼
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  9. <bean id="loginController" class="com.pb.web.controller.LoginController">
  10. <property name="successView" value="showAccount"></property>
  11. <property name="failView" value="login"></property>
  12. </bean>
  13. <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  14. <property name="mappings">
  15. <props>
  16. <prop key="/login.do">loginController</prop>
  17. </props>
  18. </property>
  19. </bean>
  20. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  21. <property name="prefix" value="/"></property>
  22. <property name="suffix" value=".jsp"></property>
  23. </bean>
  24. </beans>

Jsp頁面:

Html代碼
  1. <%@ page language="java" contentType="text/html; charset=GB18030"
  2. pageEncoding="GB18030"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <a href="login.jsp">進入</a>
  11. </body>
  12. </html>
login.jsp

Html代碼
  1. <%@ page language="java" contentType="text/html; charset=GB18030"
  2. pageEncoding="GB18030"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. ${error }
  11. <form action="login.do" method="post">
  12. 賬號登陸<br>
  13. <hr>
  14. 卡號:<input type="text" name="cardNo"><br>
  15. 密碼:<input type="text" name="password"><br>
  16. <input type="submit" value="登陸">
  17. </form>
  18. </body>
  19. </html>
showAccount.jsp

Html代碼
  1. <%@ page language="java" contentType="text/html; charset=GB18030"
  2. pageEncoding="GB18030"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. 賬戶信息<br>
  11. 卡號:${account.cardNo }<br>
  12. 密碼:${account.password }<br>
  13. 錢數:${account.balance }<br>
  14. </body>
  15. </html>
Web.xml
Html代碼
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/j2ee"
  4. xmlns:javaee="http://java.sun.com/xml/ns/javaee"
  5. 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">
  6. <welcome-file-list>
  7. <welcome-file>index.jsp</welcome-file>
  8. </welcome-file-list>
  9. <servlet>
  10. <servlet-name>Dispatcher</servlet-name>
  11. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  12. <init-param>
  13. <param-name>contextConfigLocation</param-name>
  14. <param-value>classpath:applicationContext.xml</param-value>
  15. </init-param>
  16. </servlet>
  17. <servlet-mapping>
  18. <servlet-name>Dispatcher</servlet-name>
  19. <url-pattern>*.do</url-pattern>
  20. </servlet-mapping>
  21. </web-app>  
發佈了184 篇原創文章 · 獲贊 62 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章