SpringMVC实战(三种控制器方式)

 

可以参照:https://blog.csdn.net/luckyzhoustar/article/details/105979560

 

 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进行相应的传值操作.

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值