`
锅巴49
  • 浏览: 161067 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

strurts2 中的 ActionMapper的 作用

阅读更多

最近在项目中要做http api,要求提供的url是 http://***.domain.com/api/rest?sign={签名}&method={namespace}.{action名}.{调用方法名}&......

 

类似淘宝的top api url 风格,一个url,根据参数不同,映射到不同的控制器。

 

实现方法详细:

 

1、实现自己的ActionMapper,通过method参数将请求转发到具体的action

public class RestActionMapper extends DefaultActionMapper {

	private static final Pattern p = Pattern
			.compile("([^\\.]+?)\\.([^\\.]+?)\\.([^\\.]+?)$");

	@Override
	public ActionMapping getMapping(HttpServletRequest request,
			ConfigurationManager configManager) {

		ActionMapping mapping = new ActionMapping();

		final String method = request.getParameter("method");
		if (method == null) {
			throw new IllegalArgumentException("param method can not be null");
		}
		Matcher m = p.matcher(method);
		if (!m.matches()) {
			throw new RuntimeException("method" + method + " unmatch pattern:"
					+ p);
		}

		mapping.setNamespace("/" + m.group(1));
		mapping.setName(m.group(2));
		mapping.setMethod(m.group(3));

		return mapping;
	}

}

 2、在struts.xml增加配置,将默认的actionmapper换成自定义的

<bean type="org.apache.struts2.dispatcher.mapper.ActionMapper"
		name="api_rest" class="com.my.profile.struts.RestActionMapper" />
<constant name="struts.mapper.class" value="api_rest" />

 

3、在web.xml中,这样将接收/api/rest 的所有请求

<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/api/rest</url-pattern>
	</filter-mapping>

 

struts2很强大

0
2
分享到:
评论
1 楼 xiaokang1582830 2012-10-31  
api/rest这样的定义如果项目中不存在此路径则会是404错误

相关推荐

    Struts2_s2-016&017&ognl2.6.11_patch漏洞补丁

    &lt;bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="myDefaultActionMapper" class="com.struts2.MyDefaultActionMapper" /&gt; &lt;constant name="struts.mapper.class" value=...

    Struts2 chm文档

    6.Struts2中的零配置与CoC(Convention over Configration).doc 7.Struts2介绍之使用链接标签.doc 8.Struts2入门.doc 9.Struts2学习进阶(基础篇1-7) 10.Struts2学习进阶(实例篇1)——struts2-blank-2.0.8实例...

    struts2流程与流程图

    一个请求在Struts 2框架中的处理大概分为以下几个步骤。  客户端提交一个(HttpServletRequest)请求,如上文在浏览器中输入 http://localhost: 8080/bookcode/ch2/Reg.action就是提交一个(HttpServletRequest)...

    Struts2的工作原理和流程

    2 这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做ActionContextCleanUp的可选过滤器,这个过滤器对于Struts2和其他框架的集成很有帮助,例如:SiteMesh Plugin) 3 接着FilterDispatcher被调用,...

    struts2开发文档

    一个请求在Struts2框架中的处理大概分为以下几个步骤: 1 客户端初始化一个指向Servlet容器(例如Tomcat)的请求; 2 这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做 ActionContextCleanUp的可选...

    Struts课堂笔记.rar--struts2的struts.properties配置文件详解

    The org.apache.struts2.dispatcher.mapper.ActionMapper implementation class org.apache.struts2.dispatcher.mapper.ActionMapper接口 struts.multipart.maxSize The maximize size of a multipart request ...

    Struts2 2.3.16_doc

    This is better than the ServletRedirectResult because it does not require you to encode the URL patterns processed by the ActionMapper in to your struts.xml configuration files. This means you can ...

    Java Struts 实现拦截器

    Struts2的处理流程: • 客户端产生一个HttpServletRequest的请求,该请求被提交到一系列的标准过滤器(Filter)组建链中(如ActionContextCleanUp:它主要是清理当前线程的ActionContext、Dispatcher,...

    struts2urlplugin:Struts2 插件支持 URL 中的模式匹配,用于动作映射器

    Struts2 插件支持 URL 中的模式匹配以进行动作映射 Struts 2 的插件,允许开发人员控制 URL 如何映射到他们的操作: 使用正则表达式; 通过路径或命名空间将参数传递给动作; 控制允许的 HTTP 方法; 使用替代...

    Struts2请求处理流程及源码分析

    b)根据Web.xml配置,请求首先经过ActionContextCleanUp过滤器,其为可选过滤器,这个过滤器对于Struts2和其他框架的集成很有帮助(SiteMeshPlugin),主要清理当前线程的ActionContext和Dispatcher;c)请求经过插件...

    SSH的jar包.rar

    2、这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做ActionContextCleanUp的可选过滤器,这个过滤器对于Struts2和其他框架的集成很有帮助,例如:SiteMesh Plugin) 3、接着FilterDispatcher被调用,...

Global site tag (gtag.js) - Google Analytics