写的一个多方法映射多请求的控制器,可以配置一个Controller的Bean。然后做请求-->方法的映射,并且将参数反射进配置的FormBean中。
有兴趣的朋友可以试试看,比自带的Controller要上手的多。起码配的少了,看的清楚了,呵呵。
类代码
---------------------------------
(Controller)
package org.rock.framework.controller;
import java.util.Map;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.rock.framework.operation.Action;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver;
/**
* 描述: 多方法处理Controller
*
* @author rockmanliu
* @version ver 1.0.0.0
*/
public class MultiMethodController extends MultiActionController implements InitializingBean
{
protected Logger log = Logger.getLogger(MultiMethodController.class);
/**
* 描述: 实现了具体得配置方法
*
* @throws Exception
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
public void afterPropertiesSet() throws Exception
{
if (null == urlMethodmappings || urlMethodmappings.isEmpty())
{
logger.debug("URL方法分配为NULL");
}
else
{
methodNameResoler = new PropertiesMethodNameResolver();
methodNameResoler.setMappings(urlMethodmappings);
this.setMethodNameResolver(methodNameResoler);
}
}
/* ---------------- SETER & GETER ---------------- */
// ---------------- 属性 ---------------- //
private Properties urlMethodmappings;
private PropertiesMethodNameResolver methodNameResoler;
// ---------------- 方法 ---------------- //
public void setUrlMethodmappings(Properties urlMethodmappings)
{
this.urlMethodmappings = urlMethodmappings;
}
public Properties getUrlMethodmappings()
{
return urlMethodmappings;
}
protected Map map = null;
protected Action action = null;
public void setAction(Action action)
{
this.action = action;
}
public void setMap(Map map)
{
this.map = map;
}
}
(MultiMethodControllerUrlHandlerMapping)
package org.rock.framework.controller;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.core.OrderComparator;
import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping;
/**
* 描述: 多方法请求映射处理类
*
* @author Rockmanliu
* @version ver 1.0.0.0
*/
public class MultiMethodControllerUrlHandlerMapping extends AbstractUrlHandlerMapping
{
private Map urlMap = new HashMap();
/**
* 映射URL 到 Controller 的bean 名称 这是一个配置HandMapping的典型的方式.
* 支持直接URL匹配和"ant风格"模式的匹配 详细的语法,参见AntPathMatcher类
*
* @param mappings
* URL作为键,而bean 名称作为键值的Properties
* @see org.springframework.util.AntPathMatcher
*/
@SuppressWarnings("unchecked")
public void setMappings(Properties mappings)
{
this.urlMap.putAll(mappings);
}
/**
* @return Returns the urlMap.
*/
public Map getUrlMap()
{
return urlMap;
}
/**
* @param urlMap
* The urlMap to set.
*/
public void setUrlMap(Map urlMap)
{
this.urlMap = urlMap;
}
public void initApplicationContext() throws BeansException
{
initialUrlMap();
registerUrlMap();
}
public void registerUrlMap() throws BeansException
{
if (this.urlMap.isEmpty())
{
logger.info("Neither 'urlMap' nor 'mappings' set on SimpleUrlHandlerMapping");
}
else
{
Iterator itr = this.urlMap.keySet().iterator();
while (itr.hasNext())
{
String url = (String) itr.next();
Object handler = this.urlMap.get(url);
// prepend with slash if it's not present
if (!url.startsWith("/"))
{
url = "/" + url;
}
registerHandler(url, handler);
}
}
}
@SuppressWarnings("unchecked")
public void initialUrlMap() throws BeansException
{
Map matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(getWebApplicationContext(), MultiMethodController.class, true, false);
List controllers = new ArrayList();
if (!matchingBeans.isEmpty())
{
ArrayList arrayList = new ArrayList(matchingBeans.values());
controllers = arrayList;
Collections.sort(controllers, new OrderComparator());
for (int i = 0; controllers != null && i < controllers.size(); i++)
{
MultiMethodController controller = (MultiMethodController) controllers.get(i);
Properties urlPros = controller.getUrlMethodmappings();
Iterator itr = urlPros.keySet().iterator();
for (; itr.hasNext();)
{
String url = (String) itr.next();
urlMap.put(url, controller);
}
}
}
}
}
XML配置
---------------------------------------
list
view
save
remove
实现
---------------------------------------
只需要继承上面的Controller然后配置一下,即可,超简单
下面这个例子没有具体的实现,需要的自己实现。和普通的Controller一样一样的。
package com.rock.test;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.rock.framework.controller.MultiMethodController;
import org.springframework.web.servlet.ModelAndView;
/**
* 描述: 多方法控制器演示类
*
* @author Rockmanliu
* @version ver 1.0.0.0
*/
public class MDemoController extends MultiMethodController
{
public ModelAndView view(HttpServletRequest request, HttpServletResponse response)
{
return null;
}
public ModelAndView save(HttpServletRequest request, HttpServletResponse response, MyBean bean)
{
return null;
}
public ModelAndView remove(HttpServletRequest request, HttpServletResponse response)
{
return null;
}
public ModelAndView list(HttpServletRequest request, HttpServletResponse response, MyBean bean)
{
return null;
}
}