zoukankan      html  css  js  c++  java
  • spring MVC basic

    1、MVC&&Spring MVC

    .mvc的就核心思想是业务数据抽取同业务数据呈现相分离

    .View,视图层,为用户提供UI,重点关注数据的呈现

    .model,业务数据的信息表示,关注支撑业务信息构成(对象类),通常是多个业务实体的组合

    .controller,调用业务逻辑产生合适的数据(model),传递数据给视图层用于呈现

    Mvc是一种架构模式,程序分层,分工合作

    spirng mvc 概念:

    DispatcherServlet(前端控制器)

    浏览器的请求通过DispacherServlet的分发到达一个合理的Controller,来生成业务数据model,再通过DispatcherServlet进行传递到View层

    DispatcherServlet使用HandlerAdapter适配器.适配到相应的Controller

    HandlerIntercaptor,拦截器,afterCompletion/postHandle/preHandle

    HandlerMapping:

    1.help DispatcherServlet to get the right cotroller

    2.Wrap controller with HandlerInterceptor

    HandlerExecutionChain:

    preHandle-->Controoler method-->postHandle-->afterCompletion

    ModelAndView

    ViewResovle视图解析器

     导入jar包

    配置spring mvc 核心过滤器web.xml

    <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:application_spring_mvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>

     apllication.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:tx="http://www.springframework.org/schema/tx"
            xmlns:context="http://www.springframework.org/schema/context"
            xmlns:mvc="http://www.springframework.org/schema/mvc"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    
        <context:component-scan base-package="com.nyan"/>
        <mvc:annotation-driven/>
        <!-- 視圖解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        </bean>
    
        <!-- 对静态资源的访问 -->
        <mvc:resources mapping="/images/**" location="/WEB-INF/images" cache-period="31556926"/>
    </beans>

    Controller层:

    package com.nyan.action;
    
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;
    
    /**
     * Created by Administrator on 2017/3/11 0011.
     */
    @Controller
    @Scope("prototype")
    @RequestMapping("/user")
    public class UserAction {
    
        @RequestMapping(value = "/save",method = RequestMethod.GET)
        public ModelAndView save(String name,String password){
            System.out.println("後台處理數據:"+name);
            ModelAndView modelAndView = new ModelAndView();
         //返回saveUserSuccess.jsp页面 modelAndView.setViewName(
    "saveUserSuccess"); modelAndView.addObject("msg","save successfully"); return modelAndView; } }

    view层:

    <%--
      Created by IntelliJ IDEA.
      User: Administrator
      Date: 2017/3/11 0011
      Time: 12:05
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
      <head>
        <title>$Title$</title>
      </head>
      <body>
      <form action="./user/save" method="get">
        <input type="text" name="name" value="nyan"/>
        <input type="password" name="password" value="passw0rd"/>
        <input type="submit" value="submit"/>
      </form>
      </body>
    </html>

     submit后调用./user/save,通过RequestMapping找到对应的action,进行业务逻辑处理返回一个modelAndView,通过视图解析器解析返回的modelAndView对象返回对应Dev视图。

  • 相关阅读:
    Halcon学习笔记之缺陷检测(二)
    tensorflow常用函数库
    luogu P2765 魔术球问题 (最小路径覆盖)
    luogu P2423 [HEOI2012]朋友圈 (最大团)
    poj 2226 Muddy Fields (二分图)
    匈牙利算法板子
    二分图
    AC日记——【模板】树链剖分 洛谷 P3384
    AC日记——[ZJOI2008]树的统计Count bzoj 1036
    去重排序
  • 原文地址:https://www.cnblogs.com/Nyan-Workflow-FC/p/6418993.html
Copyright © 2011-2022 走看看