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. 在Ubuntu下利用Eclipse开发FFmpeg配置小结

    首先需要编译FFmpeg得到头文件和lib文件,参见:在Ubuntu下编译FFmpeg 选择File-New-C Project 选择Executable下的Empty Project,右侧选择Lin ...

  3. oracle10g卸载问题

    oracle10g卸载是一个比较麻烦的事,一般要完全卸载有以下几个步骤: 实现方法:1.开始->设置->控制面板->管理工具->服务停止所有Oracle服务:2.开始-> ...

  4. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第11章节--为Office和SP解决方式开发集成Apps Office新的App模型

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第11章节--为Office和SP解决方式开发集成Apps  Office新的App模型         Office 2 ...

  5. Netty实战

    一.Netty异步和事件驱动1.Java网络编程回顾socket.accept 阻塞socket.setsockopt /非阻塞2.NIO异步非阻塞a).nio 非阻塞的关键时使用选择器(java.n ...

  6. LR测试HTTPS

    从浏览器里导出cer证书 保存好后, 下载openssl-1.0.1s安装好openssl之后,进入openssl目录: 输入openssl命令,即进入命令模式: 先将要转换的cer证书也放到open ...

  7. 【BZOJ4010】[HNOI2015]菜肴制作 拓扑排序

    [BZOJ4010][HNOI2015]菜肴制作 Description 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜肴预估的质量从高 ...

  8. 【BZOJ4128】Matrix BSGS+hash

    [BZOJ4128]Matrix Description 给定矩阵A,B和模数p,求最小的x满足 A^x = B (mod p) Input 第一行两个整数n和p,表示矩阵的阶和模数,接下来一个n * ...

  9. 【BZOJ3640】JC的小苹果 概率DP+高斯消元

    [BZOJ3640]JC的小苹果 Description 让我们继续JC和DZY的故事. “你是我的小丫小苹果,怎么爱你都不嫌多!” “点亮我生命的火,火火火火火!” 话说JC历经艰辛来到了城市B,但 ...

  10. 我的Android进阶之旅------>Android颜色值(RGB)所支持的四种常见形式

    Android中颜色值是通过红(Red).绿(Green).蓝(Blue)三原色,以及一个透明度(Alpha)值来表示的,颜色值总是以井号(#)开头,接下来就是Alpha-Red-Green-Blue ...