spring mvc框架
来源:互联网 发布:coc双王升级黑水数据 编辑:程序博客网 时间:2024/06/17 19:00
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;
- publicclass Account {
- private String cardNo;
- private String password;
- privatefloat balance;
- public String getCardNo() {
- return cardNo;
- }
- publicvoid setCardNo(String cardNo) {
- this.cardNo = cardNo;
- }
- public String getPassword() {
- return password;
- }
- publicvoid setPassword(String password) {
- this.password = password;
- }
- publicfloat getBalance() {
- return balance;
- }
- publicvoid 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;
- publicclass LoginController extends AbstractController {
- private String successView;
- private String failView;//这两个参数是返回值传给applicationContext.xml,进行页面分配
- public String getSuccessView() {
- return successView;
- }
- publicvoid setSuccessView(String successView) {
- this.successView = successView;
- }
- public String getFailView() {
- return failView;
- }
- publicvoid 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);
- returnnew ModelAndView(getSuccessView(),model);
- }else{
- model.put("error", "卡号和密码不正确");
- returnnew 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{
- returnnull;
- }
- }
- }
- <?xmlversion="1.0" encoding="UTF-8"?>
- <beansxmlns="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">
- <beanid="loginController"class="com.pb.web.controller.LoginController">
- <propertyname="successView"value="showAccount"></property>
- <propertyname="failView" value="login"></property>
- </bean>
- <beanid="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <propertyname="mappings">
- <props>
- <propkey="/login.do">loginController</prop>
- </props>
- </property>
- </bean>
- <beanid="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <propertyname="prefix" value="/"></property>
- <propertyname="suffix" value=".jsp"></property>
- </bean>
- </beans>
Jsp页面:
- <%@ pagelanguage="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>
- <metahttp-equiv="Content-Type"content="text/html; charset=GB18030">
- <title>Insert title here</title>
- </head>
- <body>
- <ahref="login.jsp">进入</a>
- </body>
- </html>
- <%@ pagelanguage="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>
- <metahttp-equiv="Content-Type"content="text/html; charset=GB18030">
- <title>Insert title here</title>
- </head>
- <body>
- ${error }
- <formaction="login.do" method="post">
- 账号登陆<br>
- <hr>
- 卡号:<inputtype="text" name="cardNo"><br>
- 密码:<inputtype="text" name="password"><br>
- <inputtype="submit" value="登陆">
- </form>
- </body>
- </html>
- <%@ pagelanguage="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>
- <metahttp-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>
- <?xmlversion="1.0" encoding="UTF-8"?>
- <web-appxmlns: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>
- Spring MVC (mvc框架)
- 框架:Spring、Spring MVC
- Spring MVC 框架分析
- Spring MVC 框架
- Spring 的MVC框架
- Spring MVC框架原理
- spring MVC框架介绍
- Spring MVC框架
- Spring MVC 框架简介
- Spring mvc框架入门
- spring MVC框架
- Spring MVC框架
- Spring mvc 框架搭建
- Spring MVC框架
- Spring MVC 框架
- Spring MVC框架
- spring mvc 框架搭建
- Spring MVC框架
- spring bean的生命周期
- C语言-常用代码总结
- 电商项目实战——第一天(环境部署)6版本控制器git安装
- Python 源码理解: '+=' 和 'xx = xx + xx' 的区别
- SpringMVC的请求流程图
- spring mvc框架
- java单点登录
- ≪统计学习精要(The Elements of Statistical Learning)≫课堂笔记(一)
- rhel6.5 网卡ping不通 报错connect: network is unreachable
- 构造注入和集合
- Mybatis 工具类
- python3 常用数据类型转换
- Http 协议 ----- 片段
- 京东-笔试题