spring web框架

Java Web框架篇之Spring 为什么要有Spring(IoC) Web发展的几个阶段 (1)初级阶段:使用Model1(JSP+JavaBean)/Model2(Jsp+Servlet+JavaBean)/三层模型(表示层(JSP/Servlet)+业务逻辑层+持久化层)进行开发; (2)中级阶段:使用EJB进行分布式应用开发,忍受重量级框架带来的种种麻烦; (3)高级阶段:使用Spring春天带给我们的美好,但是还要忍受很... 阅读详情

spring web框架


1.spring web mvc简介

2.DispatcherServlet

3.Controllers

4.请求映射处理

5.Views and resolving them

6.Spring's multipart (fileupload) support(文件上传的支持)

7.The Model - ModelMap (ModelAndView)

8.The View - RequestToViewNameTranslator


1.spring web mvc简介


Spring's Web MVC framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale and theme resolution as well as support for upload files. The default handler is a very simple Controller interface, just offering a ModelAndView handleRequest(request,response) method. This can already be used for application controllers, but you will prefer the included implementation hierarchy, consisting of, for example AbstractController, AbstractCommandController and SimpleFormController. Application controllers will typically be subclasses of those. Note that you can choose an appropriate base class: if you don't have a form, you don't need a form controller. This is a major difference to Struts.spring web的mvc框架是以DispatcherServle为中心的,同时存在对 view resolution, locale and theme,upload files的支持。


2.DispatcherServlet


spinrg mvc框架,同其他的开源的mvc web框架相似,采用是request-driven的方式。spring中提供了相对应的servlet- DispatcherServlet来实现这种request-driven的方式。下面就是spring处理一个request的过程:

The  DispatcherServlet   is   an actual  Servlet   (it inherits from the HttpServlet   base class), and as such is declared in the  web.xml   of your web application. Requests that you want the  DispatcherServlet   to handle will have to be mapped using a URL mapping in the same  web.xml   file. This is standard J2EE servlet configuration; an example of such a DispatcherServlet   declaration and mapping can be found below.

<web-app>
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>*.form</url-pattern>
</servlet-mapping>
</web-app>

each  DispatcherServlet   has its own  WebApplicationContext , which inherits all the beans already defined in the rootWebApplicationContext . These inherited beans defined can be overridden in the servlet-specific scope, and new scope-specific beans can be defined local to a given servlet instance。


The framework will, on initialization of a  DispatcherServlet ,  look for a file named  [servlet-name]-servlet.xml   in the  WEB-INF   directory of your web application and create the beans defined there (overriding the definitions of any beans defined with the same name in the global scope).

Consider the following  DispatcherServlet   servlet configuration (in the 'web.xml'   file.)

 

下面是一个process a request goes through when handled by a  DispatcherServlet :

  1. The  WebApplicationContext   is searched for and bound in the request as an attribute in order for the controller and other elements in the process to use. It is bound by default under the key DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE .

  2. The locale resolver is bound to the request to let elements in the process resolve the locale to use when processing the request (rendering the view, preparing data, etc.) If you don't use the resolver, it won't affect anything, so if you don't need locale resolving, you don't have to use it.

  3. The theme resolver is bound to the request to let elements such as views determine which theme to use. The theme resolver does not affect anything if you don't use it, so if you don't need themes you can just ignore it.

  4. If a multipart resolver is specified, the request is inspected for multiparts; if multiparts are found, the request is wrapped in aMultipartHttpServletRequest   for further processing by other elements in the process. (See the section entitled  Section 13.8.2, “Using the MultipartResolver”  for further information about multipart handling).

  5. An appropriate handler is searched for. If a handler is found, the execution chain associated with the handler (preprocessors, postprocessors, and controllers) will be executed in order to prepare a model (for rendering).

  6. If a model is returned, the view is rendered. If no model is returned (which could be due to a pre- or postprocessor intercepting the request, for example, for security reasons), no view is rendered, since the request could already have been fulfilled.

对于  last-modification-date  的支持:  The Spring  DispatcherServlet   also has support for returning the  last-modification-date , as specified by the Servlet API. The process of determining the last modification date for a specific request is straightforward: the  DispatcherServlet   will first lookup an appropriate handler mapping and test if the handler that is found  implements the interface LastModified   interface. If so, the value of the  long getLastModified(request)   method of the  LastModified   interface is returned to the client.

 

 


3.Controllers

 

Spring's basis for the controller architecture is the org.springframework.web.servlet.mvc.Controller interface, the source code for which is listed below.spring中提供的默认的controller的接口是比较简单的,接口定义如下:

public interface Controller {
/**

* Process the request and return a ModelAndView object which the DispatcherServlet

* will render.

*/

ModelAndView handleRequest(

HttpServletRequest request,

HttpServletResponse response) throws Exception;
}

interface is quite abstract, Spring offers a lot of  Controller   implementations out of the box that already contain a lot of the functionality you might need. The Controller   interface just defines the most basic responsibility required of every controller; namely handling a request and returning a model and a view.正如上面的接口所示的那样,十分的简单,spring提供了其他的功能更加强大的controller。

 

下面几个常见的controller的介绍:

3.1.AbstractController:a class offering caching support and, for example, the setting of the mimetype.

package samples;

public class SampleController extends AbstractController {

public ModelAndView handleRequestInternal(
HttpServletRequest request,
HttpServletResponse response) throws Exception {

ModelAndView mav = new ModelAndView("hello");
mav.addObject("message", "Hello World!");
return mav;
}
}

<bean id="sampleController" class="samples.SampleController">
<property name="cacheSeconds" value="120"/>
</bean>

3.2. ParameterizableViewController: remove the need to hard-code the viewname in the Java class

3.3 UrlFilenameViewController : inspects the URL and retrieves the filename of the file request and uses that as a viewname. For example, the filename of http://www.springframework.org/index.html   request is  index .

3.4MultiActionController:

Spring offers a multi-action controller with which you aggregate multiple actions into one controller, thus grouping functionality together.spring中提供了一个 multi-action controller。使用这个controller能够使你将功能相似的controller集合在一起。但是这里需要一个确定的函数签名:

// anyMeaningfulName can be replaced by any methodname


public [ModelAndView | Map | void] anyMeaningfulName(HttpServletRequest,
 HttpServletResponse [, Exception | AnyObject]);

另外这个来需要一个 MethodNameResolver 来 resolving method names based on the request coming in。

 

  • ParameterMethodNameResolver   - capable of resolving a request parameter and using that as the method name (http://www.sf.net/index.view?testParam=testIt   will result in a method  testIt(HttpServletRequest, HttpServletResponse)   being called). The  paramName   property specifies the request parameter that is to be inspected).

  • InternalPathMethodNameResolver   - retrieves the filename from the request path and uses that as the method name (http://www.sf.net/testing.view   will result in a method  testing(HttpServletRequest, HttpServletResponse)   being called).

  • PropertiesMethodNameResolver   - uses a user-defined properties object with request URLs mapped to method names. When the properties contain /index/welcome.html=doIt   and a request to  /index/welcome.html   comes in, the  doIt(HttpServletRequest, HttpServletResponse)   method is called. This method name resolver works with the  PathMatcher , so if the properties contained/**/welcom?.html , it would also have worked!

下面是一个简单的demo:

<bean id="paramResolver" class="org....mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="method"/>
</bean>

<bean id="paramMultiController" class="org....mvc.multiaction.MultiActionController">
<property name="methodNameResolver" ref="paramResolver"/>
</bean>


4.请求映射处理Handler mappings

Using a handler mapping you can map incoming web requests to appropriate handlers.When a request comes in, theDispatcherServlet   will hand it over to the handler mapping to let it inspect the request and come up with an appropriate HandlerExecutionChain. Then the  DispatcherServlet   will execute the handler and interceptors in the chain (if any).下面是常见的handler mappings

4.1BeanNameUrlHandlerMapping: which maps incoming HTTP requests to names of beans,下面是一个简单的demo: we could map the HTTP request with the URLhttp://samples.com/editaccount.form   to the appropriate form Controller   as follows:

<beans>
<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean name="/editaccount.form" class="org.springframework.web.servlet.mvc.SimpleFormController">
<property name="formView" value="account"/>
<property name="successView" value="account-created"/>
<property name="commandName" value="account"/>
<property name="commandClass" value="samples.Account"/>
</bean>
<beans>

<web-app>
...
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- maps the sample dispatcher to *.form -->
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>*.form</url-pattern>
</servlet-mapping>
...
</web-app>


4.2SimpleUrlHandlerMapping下面是一个简单的demo:

<web-app>
...
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- maps the sample dispatcher to *.form -->
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>*.form</url-pattern>
</servlet-mapping>

<!-- maps the sample dispatcher to *.html -->
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
...
</web-app>

<beans>

<!-- no 'id' required, HandlerMapping beans are automatically detected by the DispatcherServlet -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/*/account.form=editAccountFormController
/*/editaccount.form=editAccountFormController
/ex/view*.html=helpController
/**/help.html=helpController
</value>
</property>
</bean>

<bean id="helpController"
class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>

<bean id="editAccountFormController"
class="org.springframework.web.servlet.mvc.SimpleFormController">
<property name="formView" value="account"/>
<property name="successView" value="account-created"/>
<property name="commandName" value="Account"/>
<property name="commandClass" value="samples.Account"/>
</bean>
<beans>

This handler mapping routes requests for  'help.html'   in any directory to the 'helpController'。


5.Intercepting requests - the HandlerInterceptor interface

Spring's handler mapping mechanism has the notion of handler interceptors, that can be extremely useful when you want to apply specific functionality to certain requests, for example, checking for a principal.spring中存在请求拦截器的接口,能够实现对于特定的reqest的拦截。

Spring框架基础Web应用程序入门项目 本文还有配套的精品资源,点击获取 简介:base-webapp 是一个基础的 Spring Web 应用程序,为初学者和开发者提供了快速启动平台。它包括了核心的 Spring 组件和配置,比如 Spring Context、DispatcherServlet、MVC 配置、依赖注入和数据库连接等。通过这个项目,开发者可以学习 Spring MVC 架构,添加业务逻辑和安全控... 阅读详情

相关推荐

Spring框架与Web服务集成高级教程

本文还有配套的精品资源,点击获取 简介:本教程详细探讨了如何使用Spring框架与Web服务(特别是SOAP Web服务)集成的关键概念和技术。Spring是一个轻量级的Java开发框架,它支持依赖注入、面向切面编程等,并通过Spring-WS和CXF库提供Web服务的消费和实现。Spring-WS支持契约优先的开发,自动生成代码和代理类,并支持WS-Security等安全...

weixin_30021053的博客 770

Spring Web 框架简介

本文将介绍如何安装 Spring 框架 以及如何在 NetBeans IDE 中运行它。Spring 框架是一种流行的开源应用程序框架,该框架能够使 J2EE 的开发更为容易。它包括一个容器、一个用于管理组件的框架和一组针对 Web 用户界面、事务和持久性的管理单元(snap-in)服务。本教程将利用 Craig MacKay 所编写的 Spring NetBeans Module,并通过他所

Eleven's Blog 1483

别再只用LSTM了!用Temporal Fusion Transformer (TFT) 搞定多变量时序预测,附PyTorch实战代码

本文介绍了Temporal Fusion Transformer (TFT) 在多变量时序预测中的优势,并提供了PyTorch实战代码。TFT通过三重注意力机制解决了LSTM在特征交互、静态特征处理和解释性方面的不足,显著提升了预测精度和可解释性。文章详细讲解了TFT的核心组件构建、数据预处理、模型训练技巧及可解释性分析,帮助开发者快速掌握这一先进时序预测技术。

weixin_33712987的博客 279

Spring框架之Spring与WEB

Spring与WEB1>不使用监听器创建WEB2>使用监听器创建WEB2.1、 使用Spring监听器ContextLoaderListener(掌握)2.2、具体步骤 1>不使用监听器创建WEB 1、新建Maven项目 2、将 spring-mybatis 项目中以下内容复制到当前项目中: (1)Service 层、Dao 层全部代码 (2)配置文件 applicationContext.xml 及 jdbc.properties,mybatis.xml (3)pom.xml

oLengNuanZiZhi12的博客 220

箱式连续提升机

箱式连续提升机,是对具有高度差较大(>1500mm)的输送线之间进行连接,将箱式物资进行连续输送的提升(或下降)的输送设备,可根据输送线的具体输送要求作相应的参数设计。目前的箱式连续提升机种类比较多,常用的形式主要有Z型和C型,工作原理相同,只是入口和出口分别在两侧和同侧。

月生沧溟 1250

从CST到AD:高效PCB阵列设计与介质优化实战

本文详细解析了从CST电磁仿真软件到Altium Designer(AD)的PCB阵列设计全流程,重点解决毫米波天线等精密设计中模型导出、介质优化和阵列构建的核心难题。通过实战技巧,指导工程师高效处理DXF文件导入、复杂铜皮填充、空气介质实现等关键步骤,确保高精度设计从仿真到制造的顺利转换,提升工作效率与设计可靠性。

weixin_28256233的博客 442

使用Spring框架进行Web项目开发(初级)

Spring框架开发简单的web项目,监听器的作用,全局作用域,创建容器

weixin_44362089的博客 6440

告别OpenAI API调用:手把手在本地CPU/GPU上部署fast-whisper中文语音识别模型

本文详细指导如何在本地CPU/GPU上部署fast-whisper中文语音识别模型,摆脱对OpenAI API的依赖。从环境准备、模型获取与优化,到CPU/GPU环境的具体部署步骤和性能优化技巧,全面覆盖本地化语音识别的实现方案,帮助开发者提升效率并保障数据隐私。

weixin_29988907的博客 307

Spring框架与其他Web框架的集成指南

Spring框架与其他Web框架的集成指南 【免费下载链接】spring-framework 项目地址: https://gitcode.com/gh_mirrors/spr/spring-framework ...

gitblog_00980的博客 304

最新Python爬虫实战(入门爬虫篇)——案例3:爬取二次元cos图片批量下载到本地(附上完整爬虫代码)

本次案例通过Python爬虫技术实现了二次元图片的批量下载。代码采用模块化设计,函数实现cookies的自动解析,简化了配置流程,涵盖“请求列表页→提取详情页URL→请求详情页→提取图片链接→保存图片”的完整流程,与豆瓣案例的核心逻辑和格式完全对齐。学习重点包括:抓包工具定位多层级数据源、正则表达式匹配复杂链接、模拟请求头绕过反爬机制、文件名安全处理及自动化文件管理。需要注意控制请求频率并遵守网站使用规范,适用于入门爬虫实践,帮助学习者掌握多层级页面爬取的核心逻辑。

努力让自己发光,对的人才能迎着光而来 14万+

液态神经网络(Liquid Neural Networks)从线虫大脑到下一代 AI

液态神经网络(Liquid Neural Networks,LNN)是一类具有动态内部结构的神经网络,其灵感来源于生物神经系统,尤其是大脑中神经元如何以复杂、非线性的方式响应外部刺激。与传统神经网络不同,液态神经网络的每个神经元在处理输入时,不再是静态线性或非线性变换,而是基于一个微分方程模型进行响应。这种建模方式使神经元的行为具有时间连续性和动态适应性。换句话说,它不仅“记住”过去的信息,还能根据当前环境实时调整自身的动态特性,这对于感知、决策、预测等任务都非常关键。

hello.reader 7417

Spring集成其他Web框架

Spring集成其他Web框架 虽然Spring本身也提供了一个功能非常强大的MVC框架,并且和Spring的IoC容器无缝集成,非常便于使用。不过,在实际情况中,我们还不得不考虑众多采用第三方MVC框架的Web应用程序,例如,采用Strtus、WebWork等。如果要将这些应用程序的逻辑组件和持久化组件纳入Spring的IoC容器中进行管理,就必须考虑如何集成这些第三方的

xuzhe73的专栏 998

【深度学习|学习笔记】广义线性模型Generalized linear model(GLM)模型详解,附代码。

【深度学习|学习笔记】广义线性模型Generalized linear model(GLM)模型详解,附代码。

2401_89898861的博客 1876

在Web项目中创建使用Spring框架

在Web项目中创建使用Spring框架 1.Spring容器的创建 因为Spring容器在整个web项目中只需要创建一次,并且满足在项目开始时创建,在项目销毁时销毁,所以考虑Spring容器的创建/销毁时机和存放位置: 存放位置:ServletContext域 创建/销毁时机:利用ServletContext监听器在ServletContext创建时创建,在ServletContext销毁时销毁 1.1创建监听类 SpringContextListener public class SpringCont

GuessHat的博客 412

外向交货单批次赋值的标准姿势:用 BAPI_OUTB_DELIVERY_CHANGE 把 LIPS-CHARG 改得稳、改得干净

摘要: 外向交货单批次赋值涉及严格的业务约束,包括批次管理状态、单据状态、拆分规则等,直接更新表 LIPS 不可取。推荐使用 BAPI_OUTB_DELIVERY_CHANGE 接口,通过标准业务逻辑实现批次修改。关键点包括:区分单批次分配与批次拆分场景,正确设置 ITEM_CONTROL 控制结构,并确保单位/数量数据完整。附可直接执行的ABAP代码示例,实现安全可靠的批次更新流程。 (字数:149字)

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 397

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

Spring Web MVC框架(一) 搭建环境

学过Java的都知道SSH,也就是Struts、Spring和Hibernate。其中Struts是一个Web MVC框架,Hibernate是ORM框架,Spring是一组框架。不过由于Struts设计较早,其中有些设计已经过时了,框架漏洞也比较多。而且Struts的编写也不方便(例如控制器必须继承Controller类),所以现在Struts用的比较少了。现在更加常用的Web MVC框架是Spr

乐百川 7004

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Spring 在Web项目中整合spring框架(Spring监听器)

在Web项目中整合Spring框架 需求: 由于基于Spring思想来进行项目开发,将来好扩展,好维护。要基于Spring的思想来开发,关键点就在于让Spring来管理程序中的一些对象。 分析一下,对于一个项目来说,哪些对象适合让Spring来管理: 实体 domain 对象 不需要Spring来管理,因为程序中总会随时创建domain的实例 Dao层对象 , 需要。 Service层对象, 需要。 异常, 不需要。 util对象 , 不需要 serlvet , 不需要,轮不到你管 数据库连接对象

weixin_47371330的博客 371

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

Spring与Web框架(例如Spring MVC)漫谈——关于Spring对于多个Web框架的支持

  在看Spring MVC的官方文档时,最后一章是关于Spring对于其它Web框架的支持(如JSF,Apache Struts 2.x,Tapestry 5.x),当然Spring自己的MVC框架Spring MVC就不用多说了。   这里并不想讨论其它的Web框架,而是记录下这章开头提到的关于Spring为何还要支持其它Web框架。   Spring Framwork的一个核心价值...

weixin_30532973的博客 200

Web开发中应用Spring框架

在web 项目中 集成Spring框架 如果将          ApplicationContextapplicationContext = newClassPathXmlApplicationContext("applicationContext.xml");          HelloServicehelloService = (HelloService)applicationC

欢迎来到跃行者技术博客 1192

Web项目使用Spring框架的配置

导入文件包: spring框架jar包 配置web所需jar包 配置:在web.xml文件中配置 重点:<param-name>contextConfigLocation</param-name>这个配置名不能更改,否则监听器无法识别,配置将会无效 <!-- JAVAWEB 使用 Spring配置 配置上下文(环境)--> <context-param> <param-name>contextCon..

qq_51201135的博客 497

Spring Web 框架简介及NetBeans实践

  By Troy Giunipero 和 Craig MacKay, 1/13/08  

Eleven's Blog 1397

web框架—spring MVC介绍

Spring 的web框架是围绕分发器(DispatcherServlet)设计的,DispatcherServlet将请求分发到不同的处理器,框架还包括可配置的处理器映射,视图解析,本地化,主题解析,还支持文件上传。缺省的处理器是一个简单的控制器(Controller)接口,这个接口仅仅定义了ModelAndView handleRequest(request,response)方法。你可以实现

alaric's blog 4660
上一篇: css总结(一)
下一篇: oracle常用命令
niushoahan
博客等级 码龄17年 7粉丝 15原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值