最近在项目中要做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很强大
分享到:
相关推荐
<bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="myDefaultActionMapper" class="com.struts2.MyDefaultActionMapper" /> <constant name="struts.mapper.class" value=...
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实例...
一个请求在Struts 2框架中的处理大概分为以下几个步骤。 客户端提交一个(HttpServletRequest)请求,如上文在浏览器中输入 http://localhost: 8080/bookcode/ch2/Reg.action就是提交一个(HttpServletRequest)...
2 这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做ActionContextCleanUp的可选过滤器,这个过滤器对于Struts2和其他框架的集成很有帮助,例如:SiteMesh Plugin) 3 接着FilterDispatcher被调用,...
一个请求在Struts2框架中的处理大概分为以下几个步骤: 1 客户端初始化一个指向Servlet容器(例如Tomcat)的请求; 2 这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做 ActionContextCleanUp的可选...
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 ...
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 ...
Struts2的处理流程: • 客户端产生一个HttpServletRequest的请求,该请求被提交到一系列的标准过滤器(Filter)组建链中(如ActionContextCleanUp:它主要是清理当前线程的ActionContext、Dispatcher,...
Struts2 插件支持 URL 中的模式匹配以进行动作映射 Struts 2 的插件,允许开发人员控制 URL 如何映射到他们的操作: 使用正则表达式; 通过路径或命名空间将参数传递给动作; 控制允许的 HTTP 方法; 使用替代...
b)根据Web.xml配置,请求首先经过ActionContextCleanUp过滤器,其为可选过滤器,这个过滤器对于Struts2和其他框架的集成很有帮助(SiteMeshPlugin),主要清理当前线程的ActionContext和Dispatcher;c)请求经过插件...
2、这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做ActionContextCleanUp的可选过滤器,这个过滤器对于Struts2和其他框架的集成很有帮助,例如:SiteMesh Plugin) 3、接着FilterDispatcher被调用,...