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. erlang 爬虫——爬取网页图片

    说起爬虫,大家第一印象就是想到了python来做爬虫.其实,服务端语言好些都可以来实现这个东东. 在我们日常上网浏览网页的时候,经常会看到一些好看的图片,我们就希望把这些图片保存下载,或者用户用来做桌 ...

  2. ObjC消息机制

    深入浅出ObjC之消息    罗朝辉(http://blog.csdn.net/kesalin) 在入门级别的ObjC 教程中,我们常对从C++或Java 或其他面向对象语言转过来的程序员说,ObjC ...

  3. Call to static DateFormat

    Bug: Call to method of static java.text.DateFormatPattern id: STCAL_INVOKE_ON_STATIC_DATE_FORMAT_INS ...

  4. PerconaXtraBackup-2.2.8手册翻译

    1.1.2 Percona Xtrabackup特性 * 不停机创建Innodb数据库热备 * 对Mysql数据库创建增量备份 * 压缩数据流方式备份到异地服务器 * 更加便捷创建新的mysql从库 ...

  5. 划分Linux分区

    / Swap 这二个分区是必须有的. /usr linux系统都在 /usr 中 /home  用户信息都在 /home 下 /var 保持所有服务器的登录文件,且Web默认的路径在 /var中 可以 ...

  6. poj2367

    Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4420   Accepted: 2933 ...

  7. 【python】-- 函数、无参/有参参数、全局变量/局部变量

    函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也可以自己创建函 ...

  8. Django 基于Ajax & form 简单实现文件上传

    前端实现 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="U ...

  9. Cordova+FrameWork7开发简单教程

    1: 环境要有:(一个不会搭建环境的程序员,要么学,要么退出编程 ) 环境这里我只说需要什么: 1>AndroidStudio 3.0 (2.几的版本总会出问题.我喜欢用新版本) 2>co ...

  10. linux shell 中数组使用方法介绍

    linux shell在编程方面比windows 批处理强大太多,不管是在循环.运算.已经数据类型方面都是不能比較的. 以下是个人在使用时候,对它在数组方面一些操作进行的总结. 1.数组定义 [che ...