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视图。
spring MVC basic的更多相关文章
- springboot Serving Web Content with Spring MVC
Serving Web Content with Spring MVC This guide walks you through the process of creating a "hel ...
- Http请求中Content-Type讲解以及在Spring MVC中的应用
引言: 在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值 ...
- 基于spring mvc的注解DEMO完整例子
弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件.本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mv ...
- IntelliJ IDEA:Getting Started with Spring MVC, Hibernate and JSON实践
原文:IntelliJ IDEA:Getting Started with Spring MVC, Hibernate and JSON实践 最近把编辑器换成IntelliJ IDEA,主要是Ecli ...
- spring mvc 介绍
Spring MVC Tutorial tag. * * If you do not want to deal with the intricities of the noscript * secti ...
- spring mvc DispatcherServlet详解之interceptor和filter的区别
首先我们看一下spring mvc Interceptor的功能及实现: http://wenku.baidu.com/link?url=Mw3GaUhCRMhUFjU8iIDhObQpDcbmmRy ...
- spring3 jsp页面使用<form:form modelAttribute="xxxx" action="xxxx">报错,附连接数据库的spring MVC annotation 案例
在写一个使用spring3 的form标签的例子时,一直报错,错误信息为:java.lang.IllegalStateException: Neither BindingResult nor plai ...
- spring mvc 简单搭建
文中用的框架版本:spring 3,hibernate 3,没有的,自己上网下. web.xml配置: </load-on-startup> </servlet> ...
- 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' ...
随机推荐
- saltstack内置执行模块groupadd
groupadd模块用于命令行管理用户组 salt.modules.groupadd.add(name, gid=None, system=False) 添加一个用户到指定GID 例:salt '*' ...
- proxool连接池 异常
这是第二次整理这个文章: 首先说明proxool连接池有两种配置方式: 第一种:采用jdbc.properties的方式 第二种:采用proxool.xml的配置方 后面在完善这两种配置方式(在上班哦 ...
- 【BZOJ4785】[Zjoi2017]树状数组 树套树(二维线段树)
[BZOJ4785][Zjoi2017]树状数组 Description 漆黑的晚上,九条可怜躺在床上辗转反侧.难以入眠的她想起了若干年前她的一次悲惨的OI 比赛经历.那是一道基础的树状数组题.给出一 ...
- EasyPlayer RTSP播放器运行出现: Unable to load DLL 找不到指定的模块。exception from HRESULT 0x8007007E 解决方案
最近有EasyPlayer RTSP播放器的开发者反馈,在一台新装的Windows Server 2008的操作系统上运行EasyPlayer RTSP播放器出现"Unable to loa ...
- POJ 2965 The Pilots Brothers' refrigerator【枚举+dfs】
题目:http://poj.org/problem?id=2965 来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26732#pro ...
- SQLite支持的并发访问数
SQLite objects created in a thread can only be used in that same thread.The object was created in th ...
- L2 范数 L1 范数 出租车范数
https://en.wikipedia.org/wiki/Norm_(mathematics) http://cs231n.github.io/classification/
- PAT 1061. 判断题(15)
判断题的评判很简单,本题就要求你写个简单的程序帮助老师判题并统计学生们判断题的得分. 输入格式: 输入在第一行给出两个不超过100的正整数N和M,分别是学生人数和判断题数量.第二行给出M个不超过5的正 ...
- linux nginx完全卸载
Nginx虽然好用,但是一旦关键配置文件被修改,想要卸载重装却是相当困难.本人因为采用apt-get方式安装后又源码安装了Nginx,结果出现冲 突,卸载不了,安装不上,很是蛋疼.主要的问题还 ...
- LDA中的先验知识
LDA涉及到的先验知识有:二项分布.Gamma函数.Beta分布.多项分布.Dirichlet分布.马尔科夫链.MCMC.Gibbs Sampling.EM算法等. 二项分布 二项分布是N重伯努利分布 ...