2月16日
REST风格的URL映射之实现篇
昨天写了个接口类,今天就来实现了!
首先,官方文档说明有个Restful2ActionMapper的现实类,可以做到这点!Restful2ActionMapper源码有一以下一些比较重要的:
1: ActionMapping mapping = super.getMapping(request, configManager);
2: String actionName = mapping.getName();
3: //接着是一大堆判断数据包开头的方法isGet、isPost、isPut、isDelete方法
4: if (isGet(request)) {
5: mapping.setMethod("index");
6: } else if (isPost(request)) {
7: mapping.setMethod("create");
8: } else if (isGet(request)) {
9: mapping.setMethod("view");
10: } else if (isDelete(request))
11: mapping.setMethod("remove");
12: } else if (isPut(request)) {
13: mapping.setMethod("update"); }
14: //判断包头方法
15: protected boolean isPost(HttpServletRequest request) {
16: return "post".equalsIgnoreCase(request.getMethod());
17: }
我们现在明白了这个类的操作方式,接着要在web.xml文件设置一些参数:
struts.xml:
1: <!--</span><span style="font-family:宋体">设置是否可以在</span><span style="font-family:Consolas">action</span><span style="font-family:宋体">中使用斜线,默认为</span><span style="font-family:Consolas">false</span><span style="font-family:宋体">不可以</span><span style="font-family:Consolas">-->
2: constant name="struts.enable.SlashesInActionNames" value="true" />
3: <!--</span><span style="font-family:宋体">指定请求</span><span style="font-family:Consolas">url</span><span style="font-family:宋体">与</span><span style="font-family:Consolas">action</span><span style="font-family:宋体">映射器</span><span style="font-family:Consolas">-->
4: constant name="struts.mapper.class"
5: value="org.apache.struts2.dispatcher.mapper.Restful2ActionMapper" />
6: <!--</span><span style="font-family:宋体">这里我比较简单,只要一个</span><span style="font-family:Consolas">ID</span><span style="font-family:宋体">就算了</span><span style="font-family:Consolas">-->
7: package name="restful" extends="struts-default" >
8: action name="restful/*" class="demo.ssh.action.RestfulAction">
9: param name="id">{1}param>
10: <!--</span><span style="font-family:宋体">然后是一堆</span><span style="font-family:Consolas">CRUD</span><span style="font-family:宋体">指向的页面</span><span style="font-family:Consolas">-->
11: result name="create">/create.jspresult>
12: action>
13: package>
由于观察实现类,必需在JAVA代码中写死这几个方法名:
RestfulAction代码:
1: public class RestfulAction extends ActionSupport {
3: private Integer id;
5: // getters and setters
6: @Override
7: public String execute() throws Exception {
8: return super.execute();
9: }
10: public String remove(){
11: //TODO READ方法
12: return "delete";
13: //省略一大堆其他实现方法
14: }
然后是JSP页面的:
1: s:form method="POST" action="restful">
2: s:textfield id="id" label="ID" />
3: s:submit value="提交" />
4: s:form>
这里应该大概可以实现ReST风格的URL,但是我的报错…希望大家指点一下…
然后我喋喋不休,我找到一个比这个更好的struts2-rest-plugin.jar这个包,我开始怀疑Struts官方说明不可信了,有这么好的REST Plugin居然不说,我google一下这个类,网址:http://struts.apache.org/2.x/docs/rest-plugin.html
这个类不仅可以以ReST风格显示URL,而且可以用/product/1.xml 或者 /product/1.json,来返回文件的类型
以下是官方一个example:
1: public class OrdersController implements ModelDriven<order> {</order>
2: private OrderManager orderManager;
3: private String id;
4: private Order model;
5:
6: public HttpHeaders show() {
7: model = orderManager.findOrder(id);
8: //下面应该就是实现xml,Json的返回类型格式
9: return new DefaultHttpHeaders("show")
10: .withETag(model.getUniqueStamp())
11: .lastModified(model.getLastModified());
12: }
13: // 和我写的形式一样哈哈
14: public String update() {
15: orderManager.updateOrder(model);
16: return "update";
17: }
18: // getters and setters
19: }
好了,我又要写多次了…to be continue…