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视图。

spring MVC basic的更多相关文章

  1. springboot Serving Web Content with Spring MVC

    Serving Web Content with Spring MVC This guide walks you through the process of creating a "hel ...

  2. Http请求中Content-Type讲解以及在Spring MVC中的应用

    引言: 在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值 ...

  3. 基于spring mvc的注解DEMO完整例子

    弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件.本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mv ...

  4. IntelliJ IDEA:Getting Started with Spring MVC, Hibernate and JSON实践

    原文:IntelliJ IDEA:Getting Started with Spring MVC, Hibernate and JSON实践 最近把编辑器换成IntelliJ IDEA,主要是Ecli ...

  5. spring mvc 介绍

    Spring MVC Tutorial tag. * * If you do not want to deal with the intricities of the noscript * secti ...

  6. spring mvc DispatcherServlet详解之interceptor和filter的区别

    首先我们看一下spring mvc Interceptor的功能及实现: http://wenku.baidu.com/link?url=Mw3GaUhCRMhUFjU8iIDhObQpDcbmmRy ...

  7. spring3 jsp页面使用<form:form modelAttribute="xxxx" action="xxxx">报错,附连接数据库的spring MVC annotation 案例

    在写一个使用spring3 的form标签的例子时,一直报错,错误信息为:java.lang.IllegalStateException: Neither BindingResult nor plai ...

  8. spring mvc 简单搭建

    文中用的框架版本:spring 3,hibernate 3,没有的,自己上网下. web.xml配置: </load-on-startup>     </servlet>    ...

  9. No mapping found for HTTP request with URI [/HelloWeb/] in DispatcherServlet with name 'HelloWeb' Spring MVC

    I'm learning the Spring Framework, and I'm doing the HelloWeb tutorial on tutorialspoint, and I can' ...

随机推荐

  1. 每天学习30分钟新知识之html教程1

    版本 年份 HTML 1991 HTML+ 1993 HTML 2.0 1995 HTML 3.2 1997 HTML 4.01 1999 XHTML 1.0 2000 HTML5 2012 XHTM ...

  2. XML5632 : Only one root element is allowed. Line: 1, Column 1

    奇葩啊, 最后查出来是因为有一个svg文件名对不上...

  3. jsp安全性问题

    jsp项目不同jsp之间假设只通过超链接进行跳转,安全性太低,不能满足现实生活中对安全性的要求! 为了提高安全性.能够通过Servlet进行跳转,进行跳转的时候为了进一步实现其安全性,能够通过间jsp ...

  4. 用javascript简单写的判断电话号码

    在很多网站注册的时候,需要我们填写电话号码,本来想糊弄一下,但是还不行,一直提示不正确,我去网上搜了很多,正则表达式,发现有很多不对的, 最后写了一个简单的,但是比较实用的 首先是html部分的内容 ...

  5. USB-HID鼠标、键盘通讯格式(转) 与本人实际测试结果

    内容为网络转载,如有版权问题请联系删除 USB鼠标键盘协议介绍. 鼠标发送给PC的数据每次4个字节:BYTE1 BYTE2 BYTE3 BYTE4.定义分别是:BYTE1 -- |--bit7:    ...

  6. swift 一句代码补全tableView分割线

    1.swift实现分割线补全 swift一个大进步,只要设置tableView.separatorInset = UIEdgeInsets.zero即可补全分割线, 2.OC实现分割线补全 而在OC中 ...

  7. python基础-第九篇-9.2线程与多线程

    单线程 import time beginTime = time.time() for a in range(10): print(a) time.sleep(1) shijian = time.ti ...

  8. Redis的主从同步手动执行故障切换

    1.准备三个redis配置文件,通过端口的区分,启动三个redis数据库实例,然后配置主从复制. # a6371.conf port 6371 daemonize yes pidfile /data/ ...

  9. 在函数中如何获取 线程对象、线程唯一ID

    threading.current_thread() threading.current_thread().ident

  10. Symfony 如何使用ckeditor

    首先: 1)加载以下两个bundle "egeloen/ckeditor-bundle": "^4.0","helios-ag/fm-elfinder ...