1. 程式人生 > >SpringMVC實戰(三種控制器方式)

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進行相應的傳值操作.