SpringMVC实战(三种控制器方式)
1.前言
上篇博客着重说了一下SpringMVC中几种处理映射的方式,这篇博客来说一下SpringMVC中几种经常使用的控制器.
2.经常使用控制器
2.1 ParameterizableViewController(參数控制器)
<span style="font-size:18px;"><?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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!-- 配置文件形式要配置的组建:Controller,handlermapping(有默认规则),viewResolver,interceptor -->
<!-- 视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置从项目根文件夹到一端路径 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 文件后缀名称 -->
<property name="suffix" value=".jsp" />
</bean> <!-- 使用简单url来映射 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello1.do">TestController</prop>
<prop key="/hello1.do">toLogin</prop>
<prop key="/comm.do">command</prop>
<prop key="/form.do">form</prop>
</props>
</property>
</bean>
<!-- 參数控制器,须要配置对应的URL来映射 -->
<bean id="toLogin"
class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<!-- 配置你所要跳转到视图的名称 -->
<!-- 要跳转的视图的名称 -->
<property name="viewName" value="index"></property>
</bean> </beans>
</span>从上述代码中我们能够发现,參数控制器跟寻常的控制器差点儿相同,可是不能通过控制器名称来訪问到,由于參数控制器是唯一的.
2.2 AbstractCommandController(命令控制器)
<span style="font-size:18px;"><?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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!-- 配置文件形式要配置的组建:Controller,handlermapping(有默认规则),viewResolver,interceptor -->
<!-- 视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置从项目根文件夹到一端路径 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 文件后缀名称 -->
<property name="suffix" value=".jsp" />
</bean> <!-- 使用简单url来映射 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello1.do">TestController</prop>
<prop key="/hello1.do">toLogin</prop>
<prop key="/comm.do">command</prop>
<prop key="/form.do">form</prop>
</props>
</property>
</bean>
<!-- 命令控制器信息,须要指定简单的URL来映射 -->
<bean class="com.url.controller.commController" id="command">
<!-- 指定收集对象的类型 -->
<property name="commandClass" value="com.url.Entity.person"></property>
</bean> </beans>
</span>接着来创建控制器信息,须要继承AbstractCommandController
<span style="font-size:18px;">package com.url.controller; import java.util.HashMap;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController; import com.url.Entity.person; public class commController extends AbstractCommandController { // 通过命令控制器,每次在訪问的时候,都会创建一个新的Person对象
@Override
protected ModelAndView handle(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, BindException arg3)
throws Exception {
//使用命令控制器来收集数据而且创建指定的对象
person person=new person();
System.out.println(person);
//把后台的数据和视图封装成ModelAndView
Map<String, Object> map=new HashMap<String, Object>();
map.put("person", person);
return new ModelAndView("index", map);
} }
</span>3.3 FormController(表单控制器)
通过表单控制器,能够把表单上的数据封装成为一个对象,在控制器中收集到.
<span style="font-size:18px;"><?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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!-- 配置文件形式要配置的组建:Controller,handlermapping(有默认规则),viewResolver,interceptor -->
<!-- 视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置从项目根文件夹到一端路径 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 文件后缀名称 -->
<property name="suffix" value=".jsp" />
</bean> <!-- 使用简单url来映射 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello1.do">TestController</prop>
<prop key="/hello1.do">toLogin</prop>
<prop key="/comm.do">command</prop>
<prop key="/form.do">form</prop>
</props>
</property>
</bean>
<!-- 表单控制器,须要指定对应的URL来映射 -->
<bean class="com.url.controller.FormController" id="form">
<!-- 指定收集对象的类型 -->
<property name="commandClass" value="com.url.Entity.person"></property>
<!-- 表单页面 -->
<property name="formView" value="form"></property>
<!-- 成功后的跳转页面操作 -->
<property name="successView" value="success"></property>
</bean> </beans>
</span>接着创建控制器
<span style="font-size:18px;">package com.url.controller; import org.springframework.web.servlet.mvc.SimpleFormController; import com.url.Entity.person; /*表单控制器*/
public class FormController extends SimpleFormController { /*通过此方法来收集表单上的数据,并自己主动封装成一个对象*/
protected void doSubmitAction(Object command) throws Exception {
person p=(person)command;
System.out.println(p);
super.doSubmitAction(command);
} }
</span>
3.小结
本篇博客解说了一下SpringMVC中经常使用的控制器,通过控制器能够与View进行对应的传值操作.
SpringMVC实战(三种控制器方式)的更多相关文章
- SpringMVC实战(三种映射处理器)
1.前言 上一篇博客,简单的介绍了一下SpringMVC的基础知识,这篇博客来说一下SpringMVC中的几种映射处理器机制. 2.三种映射处理器 2.1 BeanNameUrlHandlerMapp ...
- SpringMVC 三种异常处理方式
SpringMVC 三种异常处理方式 在 SpringMVC, SpringBoot 处理 web 请求时, 若遇到错误或者异常,返回给用户一个良好的错误信息比 Whitelabel Error Pa ...
- 实战三种方式部署 MySQL5.7
作者:北京运维 常见的 MySQL 安装方式有如下三种: RPM 包方式:这种方式安装适合对数据库要求不太高的场合,安装速度快: 通用二进制包方式:安装速度相较于源码方式快,可以自定义安装目录. 源码 ...
- MVC-AOP思想-Filter 三种注册方式
在ASP.NET MVC框架中,为我们提供了四种类型的Filter类型包括:IAuthorizationFilter.IActionFilter.IResultFilter.IExceptionFil ...
- MVC-AOP(面向切面编程)思想-Filter 三种注册方式
在ASP.NET MVC框架中,为我们提供了四种类型的Filter类型包括:IAuthorizationFilter.IActionFilter.IResultFilter.IExceptionFil ...
- 2019年6月14日 Web框架之Django_07 进阶操作(MTV与MVC、多对多表三种创建方式、前后端传输数据编码格式contentType、ajax、自定义分页器)
摘要 MTV与MVC 多对多表三种创建方式 ajax ,前后端传输数据编码格式contentType 批量插入数据和自定义分页器 一.MVC与MTV MVC(Model View Controller ...
- Django多对多表的三种创建方式,MTV与MVC概念
MTV与MVC MTV模型(django): M:模型层(models.py) T:templates V:views MVC模型: M:模型层(models.py) V:视图层(views.py) ...
- 分布式锁的三种实现方式 数据库、redis、zookeeper
版权声明: https://blog.csdn.net/wuzhiwei549/article/details/80692278 一.为什么要使用分布式锁 我们在开发应用的时候,如果需要对某一个共享变 ...
- spring注解之@Import注解的三种使用方式
目录 1.@Import注解须知 2.@Import的三种用法 3.@Import注解的三种使用方式总结 @ 1.@Import注解须知 1.@Import只能用在类上 ,@Import通过快速导入的 ...
随机推荐
- HTTP请求和响应1:概述
HTTP的报文分为请求报文和响应报文,打开一个web页面后,浏览器将发起一个HTTP请求报文.HTTPserver收到请求后将回送一个响应报文. 报文的基本结构 HTTP的请求和响应报文都由三个部分组 ...
- cocos2d-x 3.2 之 2048 —— 第一篇
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- hdu5024
思路要开阔些,或者说要转化一下思路,别太死 把每一个点当拐点,爆一边就能够.用记忆化搜索也行.都不会超时 #include<bits/stdc++.h> using namespace s ...
- win7中下mysql-5.6.22免安装配置
windows下mysql免安装配置 1. 下载mysql免安装压缩包 下载mysql-5.6.22-winx64.zip 解压到本地D:\mysql-5.6.22-winx64 2. 修改配置文件 ...
- 慢慢人生路,学点Jakarta基础-深入剖析Java的接口和抽象类
在java面向对象编程的,抽象类和接口始终存在有疑问的地方,因为两者太多相似有太多不同,在刚开始学习的时候经常弄的不对,使用情景搞混,今天来总结之前学习Java中接口和抽象类的问题. 抽象类 了解:只 ...
- 项目: 更新(二) python 实现大概FTP的功能
服务器利用 socketserver 模块 构造, 实现了 多进程. 客户端仍然利用的是底层的 socket模块. 只不过进行了更深度的 解耦, 新加或者删除 某些功能 更方便 在上一个版本的基础上, ...
- 突破极限 解决大硬盘上安装Unix新思路
一.问题提出 硬盘越做越大,然我喜欢让我忧.10年前就遇到过在586电脑BIOS不认识超过8.4G容量硬盘的问题,以及Windows Nt操作系统不认大硬盘(容量超过8.4G)的问题,对于Linux ...
- day 5 集合
# -*- coding: utf_8 _*_# Author:Vi#集合是无序的 list_1 = [1,2,3,2,3,5,7]list_1 = set(list_1)#将列表转变成集合list_ ...
- 【iOS与EV3混合机器人编程系列之中的一个】iOS要干嘛?EV3能够更酷!
乐高Mindstorm EV3智能机器人(下面简称EV3)自从在2013年的CES(Consumer Electronics Show美国消费电子展)上展出之后,就吸引了全球广大机器人爱好者的眼球!E ...
- 分组的listview——ExpandableListView
开发使用到的数据统计时可以用分组的ExpandablelistView 效果: