学习目的:
1.掌握spring MVC开发步骤
2.理解实现原理
3.理解框架运行时原理
一.原理小结
主要由DispatcherServlet、处理器映射、处理器、视图解析器、视图组成
1.DispatcherServlet接收到一个HTTP请求,根据对应配置文件中的处理机映射,找到处理器(Handler)
- <property name="mappings">
- <props>
- <prop key="login.do">loginControler</prop>
- <prop key="userFormcontroler.do">userFormcontroler</prop>
- </props>
- </property>
2.调用Handler中的方法,处理该请求,处理完后返回一个ModelAndView类型的数据给DispatcherServlet
- public class UserFormControler extends AbstractCommandController {
- private String result;
- // 相当于Action中的execute
- protected ModelAndView handle(HttpServletRequest request,
- HttpServletResponse response, Object obj,
- BindException bindexception) throws Exception {
- // 将页面返回的数据丢到obj中来
- UserBean user = (UserBean) obj;
- System.out.println(user.getName());
- // 将数据封装成一个map丢到V层去
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("user", user);
- return new ModelAndView(this.result, map);
- }
- }
其中包含了处理结果的视图和视图中要使用的数据。
3.DispatcherServlet根据得到的ModelAndView中的视图对象,找到一个合适的ViewResolver(视图解析器),根据视图解析器的配置,DispatcherServlet将视图要显示的数据传给对应的视图,最后给浏览器构造一个HTTP响应。
DispatcherServlet是整个Spring MVC的核心。它负责接收HTTP请求组织协调Spring MVC的各个组成部分。其主要工作有以下三项:
1)截获符合特定格式的URL请求。
2)初始化DispatcherServlet上下文对应的WebApplicationContext,并将其与业务层、持久化层的WebApplicationContext建立关联。
3)初始化Spring MVC的各个组成组件,并装配到DispatcherServlet中。
二.简单应用
Spring MVC 一般步骤:
1、创建webproject,加入Spring MVC(spring中 web jar)
2、编写jsp login.jsp welcome.jsp
3、编写Controller(命令 handler、form onsubmit)
a、接收页面的数据
b、调用service代码
c、将处理结果传给view
4、编写View Object对象 Percesitence Oject
UserBean
5、配置
a、Controller和Service 配置Bean
b、配置handlerMapping(请求--》controller)
c、配置ViewResolver(实现页面的转向)
6、jsp《-》CommandClass之间的写法____________传值问题:页面标签名==javaBean属性名
7、jsp中怎么从ModelAndView中获取值___________map.put("user", user); //此处值可在页面显示 ${user.userName }欢迎你来了
1)第一个demo实现
1.index.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>My JSP 'index.jsp' starting page</title>
- </head>
-
- <body>
- <form action="login.do" method="post">
- 用户名:<input type="text" name="userName"/>
- <input type="submit" value="提交"/>
- </form>
- </body>
- </html>
2.login.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>My JSP 'login.jsp' starting page</title>
- </head>
-
- <body>
- <br>
- ${user.userName }欢迎你来了
- </body>
- </html>
3.LoginControler.java 登陆的controler
注意: 1.extends AbstractController
2.protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response)
该方法相当于action中的excute方法
3.return new ModelAndView("login"); //其中login为页面名称,在applicationContext.xml中实用视图与视图解析器拼凑为完整页面访问URL
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
- package cn.com.sxt.controler;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.web.servlet.ModelAndView;
- import org.springframework.web.servlet.mvc.AbstractController;
- public class LoginControler extends AbstractController {
- protected ModelAndView handleRequestInternal(HttpServletRequest request,
- HttpServletResponse response) throws Exception {
- System.out.println("我来了啊");
- return new ModelAndView("login");
- }
- }
4.UserFormControler.java
- package cn.com.sxt.controler;
- import java.util.HashMap;
- import java.util.Map;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.validation.BindException;
- import org.springframework.web.servlet.ModelAndView;
- import org.springframework.web.servlet.mvc.AbstractCommandController;
- import cn.com.bean.UserBean;
- public class UserFormControler extends AbstractCommandController {
- private String result;
- // 相当于Action中的execute
- protected ModelAndView handle(HttpServletRequest request,
- HttpServletResponse response, Object obj,
- BindException bindexception) throws Exception {
- // 将页面返回的数据丢到obj中来
- UserBean user = (UserBean) obj;
- System.out.println(user.getName());
- // 将数据封装成一个map丢到V层去
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("user", user);
- return new ModelAndView(this.result, map);
- }
- }
5.UserBean.java 编写View Object对象 Percesitence Oject
- package cn.com.bean;
- public class UserBean {
- private String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
-
- }
6.配置applicationContext.xml
a、Controller和Service 配置Bean
- <bean id="loginControler" class="cn.com.sxt.controler.LoginControler">
- </bean>
- <bean id="userFormcontroler" class="cn.com.sxt.controler.UserFormControler">
- <property name="commandClass" value="cn.com.sxt.bean.User"></property>
- <property name="formView" value="index"></property>
- <property name="successView" value="login"></property>
- </bean>
b、配置handlerMapping(请求--》controller)
- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <property name="mappings">
- <props>
- <prop key="login.do">loginControler</prop>
- <prop key="userFormcontroler.do">userFormcontroler</prop>
- </props>
- </property>
- </bean>
c、配置ViewResolver(实现页面的转向)
- <bean
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <!-- 制定前缀后缀 jsp/login.jsp-->
- <property name="prefix" value="jsp/"></property>
- <property name="suffix" value=".jsp"></property>
- </bean>
阅读(18116) | 评论(0) | 转发(0) |