Spring Mvc 用Demo去学习
1:首先大体知道 SpringMVC 框架的 运行原理(图片来自网络 )

2:SpringMVC 是依照DispatcherServlet 展开的
这里可以约Structs2对比,structs2 是依照过滤器展开的; 下面就用一个 Demo来 搭建一个 SpringMVC,框架;
3:搭建的过程如下:
a:首先将需要的jar包 导入:lib下:

b:接下来配置 SpringMVC的 核心DispatcherServlet
在项目web.xml 下配置:
(这里注意的是:servlet-name [SpMvcDemo]这个命名, 在后面 配置 控制器xml文件时需要 用到!)
c:接下里来创建 控制器xml 文件;
在项目WebContent下 创建xml 文件 ;(重要的是:该文件的 命名规则:[servlet-name]-servlet.xml) 例如这里的项目控制xml 配置命名:SpMvcDemo-servlet.xml
d:接下来在创建的控制xml文件中 (结尾提供源码)

配置 HandlerMapping 按照 benaname 来指定 Contorller
创建 bean name=SpmvcDemo.do 映射到 Hellocontroller (处理请求类)
d(1):创建发出请求的 jsp 页面 我用的Demo.jsp 简单的 <form>表单提交数据请求:

d(2):注意这里的 action =“” 在Demo调试的时候 通过 action 来将请求分发到 那个 Controller类中处理请求,同时也由该类做response;
e:接下来创建 Controller 控制类:(此次Demo的Controller是 Hellocontroller )
创建要求:继承AbstractController重写handleRequestInternal方法(创建的时候 自动重写)
e(1):在handleRequestInternal方法中:ModelAndView 做response 和 数据的传递:

f:接下来,创建Demo_to.jsp 用来测试 数据的回现(EL 表达式)

g:最后配置 ModelAndView 前缀后缀 配置试图解析器!!(在 控制xml 中)

实质上就是在为访问的 jsp 页面拼接url,/Demopath/ 在项目中 是个文件夹;方便管理项目;
h:完成 框架的搭建 调试项目吧;
附上源码:
SpMvcDemo-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 配置 handlerMapping 直接引入一个 类 -->
<bean
class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"></bean> <!-- 配置 hanlder 分发 Contorller -->
<bean name="/SpmvcDemo.do" class="demo_controller.Hellocontroller"></bean>
<!-- 配置 ModelAndView 前缀后缀 -->
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/Demopath/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringMVC_demo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SpMvcDemo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SpMvcDemo</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
Hellocontroller.java
package demo_controller; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController; import sun.misc.Request; public class Hellocontroller extends AbstractController { @Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub String hello = arg0.getParameter("hello");
System.out.println("----:"+hello);
ModelAndView mav = new ModelAndView("Demo_to");
mav.addObject("helloword", "SpringMvc "+hello);
return mav; } }
不喜勿碰,技术有限 ,谢谢点评;一起加油!
Spring Mvc 用Demo去学习的更多相关文章
- ContextLoaderListener和Spring MVC中的DispatcherServlet学习
DispatcherServlet介绍 DispatcherServlet是Spring前端控制器的实现,提供Spring Web MVC的集中访问点,并且负责职责的分派,与Spring IoC容器无 ...
- Spring mvc 中 DispatcherServlet 的学习和理解
上图表示当客户请求来到时,spring架构作出响应的流程,可以从图中看到看到请求分发的中心就是 DispatcherServlet 类,DispatcherServlet的任务是将请求发送给Sprin ...
- Spring MVC入门Demo
1 参考http://blog.csdn.net/haishu_zheng/article/details/51490299,用第二种方法创建一个名为springmvcdemo的Maven工程. 2 ...
- Spring Mvc 入门Demo
1.web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns=" ...
- ContextLoaderListener和Spring MVC中的DispatcherServlet学习 随手记
Servlet上下文关系 DispatcherServlet的上下文是通过配置servlet的contextConfigLocation来加载的,默认实现是XmlWebApplicationConte ...
- 用intellj 建一个spring mvc 项目DEMO
spring的起初可能经常碰壁,因为网上的资料都是混乱的xml堆成的,混乱难以理解,我这个也是,阿哈哈哈哈! 新建一个Maven->create from archetype->org.j ...
- Spring MVC Mock demo
package com.niwodai.mem.web.controller; import com.alibaba.fastjson.JSON; import org.junit.Before; i ...
- Spring MVC 单元测试Demo
@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations={" ...
- spring mvc velocity多视图
1.ViewResolverUrlBasedViewResolver 这个东西是根据url 进行路由的.网上搜了 1.order 排序,同名出现各种问题 2.XmlViewResolver,BeanN ...
随机推荐
- 在ElasticSearch中使用 IK 中文分词插件
我这里集成好了一个自带IK的版本,下载即用, https://github.com/xlb378917466/elasticsearch5.2.include_IK 添加了IK插件意味着你可以使用ik ...
- Excel import
Case Study: Reading cell content from excel template for COM variant type VT_R4 or VT_R8 is always l ...
- C++实现的控制台-贪吃蛇
周六终于可以抽出一整段时间了 想了想就写个贪吃蛇吧 第一次写 差不多下了140行 也不算太多吧 以后ACM比赛是在做不来就自己打个贪吃蛇玩 ps:本来想写个项目的 但是为了方便你们阅读 就写在 ...
- 使用PCA + KNN对MNIST数据集进行手写数字识别
首先引入需要的包 %matplotlib inline import numpy as np import scipy as sp import pandas as pd import matplot ...
- URL传中文参数导致乱码的解决方案之encodeURI
通过URL传中文参数时,在服务端后台获取到的值往往会出现乱码问题,解决方案有很多种,本文主要介绍如何通过encodeURI来解决中文乱码问题: first:前端传递参数的时候需要对中文参数进行两次en ...
- CF #284 div1 D. Traffic Jams in the Land 线段树
大意是有n段路,每一段路有个值a,通过每一端路需要1s,如果通过这一段路时刻t为a的倍数,则需要等待1s再走,也就是需要2s通过. 比较头疼的就是相邻两个数之间会因为数字不同制约,一开始想a的范围是2 ...
- endsWith is not a function解决方案
在写javascript脚本时,用某些方法,有时候会碰到"XXX is not a function"之类的报错. 出现这种情况,主要是因为某些方法在低版本浏览器上不支持.比如说& ...
- IO和socket编程
五一假期结束了,突然想到3周前去上班的路上看到槐花开的正好.放假也没能采些做槐花糕,到下周肯定就老了.一年就开一次的东西,比如牡丹,花期也就一周.而花开之时,玫瑰和月季无法与之相比.明日黄花蝶也愁.想 ...
- poj3061尺取法
A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...
- poj3320尺取法
Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is co ...