abstractcontroller java中的作用_Spring MVC 框架学习之AbstractFormController以及AbstractFormControll...

在看完BaseCommandController和AbstractCommandController之后,我们再看BaseCommandController的另一个实现AbstractFormController,以及AbstractFormController的具体实现SimpleFormController。

先看看AbstractFormController对handleRequestInternal的实现:

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)

throws Exception {

// Form submission or new form to show?

if (isFormSubmission(request)) {

// Fetch form object from HTTP session, bind, validate, process submission.

try {

Object command = getCommand(request);

ServletRequestDataBinder binder = bindAndValidate(request, command);

BindException errors = new BindException(binder.getBindingResult());

return processFormSubmission(request, response, command, errors);

}

catch (HttpSessionRequiredException ex) {

// Cannot submit a session form if no form object is in the session.

if (logger.isDebugEnabled()) {

logger.debug("Invalid submit detected: " + ex.getMessage());

}

return handleInvalidSubmit(request, response);

}

}

else {

// New form to show: render form view.

return showNewForm(request, response);

}

}

这个方法,首先判断是不是Form提交,判断方法是:

protected boolean isFormSubmission(HttpServletRequest request) {

return "POST".equals(request.getMethod());

}

如果是form提交的话,系统首先创建一个Command,然后对数据进行绑定和验证,之后调用processFormSubmission方法。showNewForm则调用showForm。

在AbstractFormController中里面有两个抽象方法:

protected abstract ModelAndView processFormSubmission(

HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)

throws Exception;

protected abstract ModelAndView showForm(

HttpServletRequest request, HttpServletResponse response, BindException errors)

throws Exception;

好了,看完AbstractFormController之后,再看看SimpleFormController是如何实现:

protected ModelAndView processFormSubmission(

HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)

throws Exception {

if (errors.hasErrors()) {

if (logger.isDebugEnabled()) {

logger.debug("Data binding errors: " + errors.getErrorCount());

}

return showForm(request, response, errors);

}

else if (isFormChangeRequest(request, command)) {

logger.debug("Detected form change request -> routing request to onFormChange");

onFormChange(request, response, command, errors);

return showForm(request, response, errors);

}

else {

logger.debug("No errors -> processing submit");

return onSubmit(request, response, command, errors);

}

}

在上面的方法中,如果有错误,调用showForm,来显示form。没有错误的话,则调用onSubmit方法。

protected final ModelAndView showForm(

HttpServletRequest request, BindException errors, String viewName, Map controlModel)

throws Exception {

// In session form mode, re-expose form object as HTTP session attribute.

// Re-binding is necessary for proper state handling in a cluster,

// to notify other nodes of changes in the form object.

if (isSessionForm()) {

String formAttrName = getFormSessionAttributeName(request);

if (logger.isDebugEnabled()) {

logger.debug("Setting form session attribute [" + formAttrName + "] to: " + errors.getTarget());

}

request.getSession().setAttribute(formAttrName, errors.getTarget());

}

// Fetch errors model as starting point, containing form object under

// "commandName", and corresponding Errors instance under internal key.

Map model = errors.getModel();

// Merge reference data into model, if any.

Map referenceData = referenceData(request, errors.getTarget(), errors);

if (referenceData != null) {

model.putAll(referenceData);

}

// Merge control attributes into model, if any.

if (controlModel != null) {

model.putAll(controlModel);

}

// Trigger rendering of the specified view, using the final model.

return new ModelAndView(viewName, model);

}

在showForm中,设置属性,放在model中,然后在viewName进行设置。

FormController就是上面的过程。具体的执行过程和详细信息会在以后的博客中具体介绍。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目录 前言 1. 简介 1.1. 概览 1.2. 使用场景 2. Spring 2.0 的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 更简单的XML配置 2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.4. 间层 2.4.1. 在XML里更为简单的声明性事务配置 2.4.2. JPA 2.4.3. 异步的JMS 2.4.4. JDBC 2.5. Web层 2.5.1. Spring MVC的表单标签库 2.5.2. Spring MVC合理的默认值 2.5.3. Portlet 框架 2.6. 其他特性 2.6.1. 动态语言支持 2.6.2. JMX 2.6.3. 任务规划 2.6.4. 对Java 5(Tiger)的支持 2.7. 移植到Spring 2.0 2.7.1. 一些变化 2.8. 更新的样例应用 2.9. 改进的文档 I. 核心技术 3. 控制反转容器 3.1. 简介 3.2. 容器和bean的基本原理 3.2.1. 容器 3.2.2. 实例化容器 3.2.3. 多种bean 3.2.4. 使用容器 3.3. 依赖 3.3.1. 注入依赖 3.3.2. 构造器参数的解析 3.3.3. bean属性及构造器参数详解 3.3.4. 使用depends-on 3.3.5. 延迟初始化bean 3.3.6. 自动装配(autowire)协作者 3.3.7. 依赖检查 3.3.8. 方法注入 3.4. bean的作用域 3.4.1. Singleton作用域 3.4.2. Prototype作用域 3.4.3. 其他作用域 3.4.4. 自定义作用域 3.5. 定制bean特性 3.5.1. Lifecycle接口 3.5.2. 了解自己 3.6. bean定义的继承 3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.3. 使用FactoryBean定制实例化逻辑 3.8. ApplicationContext 3.8.1. 利用MessageSource实现国际化 3.8.2. 事件 3.8.3. 底层资源的访问 3.8.4. ApplicationContext在WEB应用的实例化 3.9. 粘合代码和可怕的singleton 3.9.1. 使用Singleton-helper类 4. 资源 4.1. 简介 4.2. Resource 接口 4.3. 内置 Resource 实现 4.3.1. UrlResource 4.3.2. ClassPathResource 4.3.3. FileSystemResource 4.3.4. ServletContextResource 4.3.5. InputStreamResource 4.3.6. ByteArrayResource 4.4. ResourceLoader 4.5. ResourceLoaderAware 接口 4.6. 把Resource作为属性来配置 4.7. Application context 和Resource 路径 4.7.1. 构造application context 4.7.2. Application context构造器资源路径的通配符 4.7.3. FileSystemResource 提示 5. 校验,数据绑定,BeanWrapper,与属性编辑器 5.1. 简介 5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到错误信息 5.4. Bean处理和BeanWrapper 5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. Spring的AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 6.2.2. 声明一个切面 6.2.3. 声明一个切入点(pointcut) 6.2.4. 声明通知 6.2.5. 引入(Introductions) 6.2.6. 切面实例化模型 6
<br>认识 Spring <br> 来认识 Spring 的一些特性,并初步了解一下什么叫作 IoC?什么叫作 DI? <br>简介 Spring <br>Inversion of Control <br>Dependency Injection <br><br><br>核心容器 <br> Spring 核心容器实作了 IoC,BeanFactory 与 ApplicationContext 的运用是了解 Spring 的重点所在。 <br>管理 Bean <br>从第一个 Spring 应用程式开始,逐步了解何谓依赖注入,以及如何使用 Spring 的容器功能来管理 Bean,了解 Bean 在 Spring 容器的生命周期。<br>第一个 Spring 程式 <br>BeanFactory、 ApplicationContext <br>Type 2 IoC、Type 3 IoC <br>属性参考 <br>自动绑定 <br>集合物件注入 <br>Bean 的生命周期 <br>Bean 进阶管理 <br>理想上对于 Bean 来说,它不需要意识到 Spring 容器的存在,然而有时候 Bean 仍必须知道有关于 Spring 容器或自己的一些讯息,而另一方面,您可能必须让容器对 Bean 进行一些额外处理。<br>不使用XML定义档进行 Bean设置 <br>Aware 相关介面 <br>BeanPostProcessor <br>BeanFactoryPostProcessor <br>PropertyPlaceholderConfigurer <br>PropertyOverrideConfigurer <br>CustomEditorConfigurer <br>讯息与事件 <br>ApplicationContext 除了具备如 BeanFactory 基本的容器管理功能之外,并支援更多应用程式框架的特性,像是资源的取得、讯息解析、事件的处理与传播。<br>Resource 的取得 <br>解析文字讯息 <br>倾听事件 <br>事件传播 <br><br><br>AOP(Aspect-Oriented Programming) <br> 在一个服务的流程插入与服务无关的逻辑(例如Logging、Security),这样的逻辑称为 Cross-cutting concerns,将 Crossing-cutting concerns 独立出来为一个物件,这样的特殊物件称之为 Aspect,Aspect-oriented programming 着重在 Aspect 的设计及与应用程式的缝合(Weave)。<br><br>AOP 入门 <br>AOP 的观念与术语都不是很直觉,可以先从代理机制(Spring 实现 AOP 的一种方式)来看看实际的例子,从而了解 AOP 的观念与各种术语。<br>从代理机制初探 AOP <br>动态代理<br><br>AOP 观念与术语 <br>Spring AOP <br>Advices <br>Advices 包括了Aspect 的真正逻辑,由于缝合至Targets的时机不同,Spring 提供了几种不同的 Advices。<br>Before Advice <br>After Advice <br>Around Advice <br>Throw Advice <br>Pointcut、Advisor <br>Pointcut 定义了 Advice 的应用时机,在 Spring ,使用 PointcutAdvisor 将 Pointcut 与 Advice 结合成为一个物件,Spring 大部分内建的 Pointcut 都有对应的 PointcutAdvisor。<br>NameMatchMethodPointcutAdvisor <br>RegExpMethodPointcutAdvisor <br>ControlFlowPointcut <br>Pointcut 介面 <br>Pointcut 交集、联集操作 <br>Introduction <br>为特殊的 Advice,它影响的不是方法的流程,而是影响整个物件的行为,为物件动态 mixin 职责。<br>IntroductionInterceptor <br>DelegatingIntroductionInterceptor <br>Autoproxing <br>自动代理可以让您不用为每一个要被 Advised 的 Target 手动定义代理物件,透过 Bean 名称或是 Pointcut 的比对,自动为符合的 Target 建立代理物件。<br>BeanNameAutoProxyCreator <br>DefaultAdvisorAutoProxyCreator <br><br><br>持久层 <br> 来看看 Spring 的 IoC 容器与 AOP 框架如何应用于持久层,包括了资料库、交易等相关议题。 <br>资料库存取 <br>Spring 提供了 DAO 框架,让应用程式开发时无须耦合于特定资料库技术。<br>Spring 的 DAO 支持 <br>DataSource 注入 <br>DataSource 置换 <br>JDBC 支援 <br>Spring 在 JDBC 的使用上提供了几个类别,让您可以简化 JDBC 在使用时的流程。<br>使用 JdbcTemplate <br>JdbcTemplate 执行与更新<br><br>JdbcTemplate - 查询 <br>以物件方式进行操作 <br>DataFieldMaxValueIncrementer <br>交易管理 <br>Spring 提供编程式的交易管理(Programmatic transaction management)与宣告式的交易管理(Declarative transaction management),为不同的交易实作提供了一致的编程模型。<br>Spring 对交易的支援 <br>JDBC 编程式交易管理 <br>JDBC 宣告式交易管理 <br>交易的属性介绍 <br>TransactionAttributeSource、 TransactionAttribute <br>Hibernate 支援 <br>Spring 整合了对 Hibernate 的设定,并提供有 HibernateTemplate 等类别,让您在结合 Hibernate 时可以简化使用上的流程。<br>第一个 Hibernate 程式 <br>SessionFactory 注入 <br>HibernateTemplate <br>Hibernate 编程交易管理 <br>Hibernate 宣告式交易管理 <br><br><br>Web 层 <br> Spring 提供了 MVC Web 框架,您可以善用 IoC 容器在依赖注入上的好处,另一方面,Spring 也致力于与其它的 Web 框架的整合。 <br>Spring MVC 入门 <br>从一个最简单的 Spring Web 应用程式,来看看 Spring MVC 框架的架构与 API 组成元素。<br>第一个 Spring MVC 程式 <br>WebApplicationContext <br>Handler Mapping <br>Handler Interceptor <br>Controller 继承架构 <br>ModelAndView <br>View Resolver <br>Exception Resolver <br>使用 Controller 相关类别 <br>与其它 Web 框架的 Action 物件不同的是,Spring 提供了丰富的 Controller 相关类别,让您可以依需求来制作自己所需的 Controller 物件。<br>AbstractController <br>MultiActionController 与 ParameterMethodNameResolver <br>MultiActionController 与 PropertiesMethodNameResolver <br>ParameterizableViewController <br>AbstractCommandController <br>AbstractFormController <br>SimpleFormController <br>AbstractWizardFormController <br>ThrowawayController <br>搭配 Controller 的类别 <br>介绍如何在 Controller上搭配使用验证器(Validator)、如何实作Command资料的型态转换,以及如何使用Spring的相关API来实作档案上传的功能。<br>实作 Validator <br>使用 PropertyEditor <br>档案上传<br><br><br><br>View层方案、Web框架整合 <br> 当使用JSP作为View层技术时,您可以结合JSTL以及Spring提供的标签,而除了JSP技术作为View层之外,Spring还提供了不同 View层技术的解决方案,您甚至可以定义自己的View层技术实现。 <br>JSP View 层 <br>当使用 JSP 作为 View 层技术时,您可以结合 JSTL 以及 Spring 提供的标签。<br>结合 JSTL <br><spring:bind> 标签 <br>数据绑定的几个方法 <br><spring:message> 标签 <br><spring:transform> 标签 <br>其它 View 层 <br>除了 JSP View 层技术之外,您还可以使用其它的 View 层技术,或建立自己的 View Class。<br>以 Tiles 为例 <br>自订 View Class <br>与其它 Web 框架的整合 <br>您可以将 Spring 与现在的一些 Web 框架结合在一起,重点都在于如何让 Web 框架意识到 Spring 的存在。<br>第一个 Struts 程式 <br>在 Struts 整合 Spring <br>第一个 JSF 程式 <br>在 JSF 整合 Spring <br><br><br>其它 <br> Spring 提供了简化且一致的方式,让您在使用一些 API 或服务时更加简单。 <br>远程(Remoting) <br>Spring 提供了一致的使用方式,即使所采用的远程服务技术不尽相同,在 Spring 运用它们的方式却是一致的。<br>RMI <br>Hessian、 Burlap <br>Http Invoker <br>邮件 <br>对于邮件发送服务的支援是由Spring的 org.springframework.mail.MailSender介面所定义,它有两个实作类别, org.springframework.mail.cos.CosMailSenderImpl与 org.springframework.mail.javamail.JavaMailSenderImpl。<br>简单邮件 <br>HTML 邮件 <br>内嵌图片或附档 <br>排程 <br>Spring则对 java.util.Timer提供了抽象封装,让您可以善用Spring的容器管理功能,而Spring对Quartz进行了封装,让它在使用上更加方便。<br>使用 TimerTask <br>使用 MethodInvokingTimerTaskFactoryBean <br>使用 Quartz <br>使用 MethodInvokingJobDetailFactoryBean <br>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值