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. mysql case then 语句

  2. 【入门】创建express项目

    1.创建项目(图解) 2.访问http://localhost:3000/就看到熟悉的页面了 3.查看项目目录     参考文档:http://jingyan.baidu.com/article/92 ...

  3. 解决php函数json_encode转换后中文被编码为unicode

    大家都知道使用函数json_encode()可以方便快捷地将数组进行json编码转换,但是如果数组值存在着中文,json_encode会将中文转换为unicode编码,例如: <?PHP $ar ...

  4. 对Mybatis的理解

    首先Mybatis是一个对象关系映射(Object Relational Mapping,简称ORM)框架,是为了解决面向对象与关系数据库存在的互不匹配的现象.也就是说Mybatis的关注点在于对象与 ...

  5. poj1061(extendgcd)

    看完题目后,题目要求: 设时间为t (x+mt)%L = (y+nt)%L ( x-y + (m-n)*t )= k*L (k是整数,可为负) 然后就是经典的 xa+yb=c 求解x,y的经典题目了. ...

  6. 【BZOJ1007】[HNOI2008]水平可见直线 半平面交

    [BZOJ1007][HNOI2008]水平可见直线 Description 在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见 ...

  7. 九度OJ 1205:N阶楼梯上楼问题 (斐波那契数列)

    时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:3739 解决:1470 题目描述: N阶楼梯上楼问题:一次可以走两阶或一阶,问有多少种上楼方式.(要求采用非递归) 输入: 输入包括一个整 ...

  8. iptables的用例

    iptables书写思路顺序 1.协议 icmp 2.哪个功能和目标:过滤,拒绝 3.数据包流向:外到内 4.哪个链适合:越早越好,INPUT 5.源地址和目标地址 练习1.禁止某些主机或网络访问本机 ...

  9. Qt状态机框架(状态机就开始异步的运行了,也就是说,它成为了我们应用程序事件循环的一部分了)

    状态机框架 Qt中的状态机框架为我们提供了很多的API和类,使我们能更容易的在自己的应用程序中集成状态动画.这个框架是和Qt的元对象系统机密结合在一起的.比如,各个状态之间的转换是通过信号触发的,状态 ...

  10. go语言之接口一

    在Go语言中,一个类只需要实现了接口要求的所有函数,我们就说这个类实现了该接口 我们定义了一个File类,并实现有Read().Write().Seek().Close()等方法.设 想我们有如下接口 ...