struts拓展restful

爲restful開發的話,必定要設計到URL,struts正好是管理URL的json

因此在struts的項目中用restful,要遵照struts的規則restful

rest在struts中對應的方法

restful

上面是官方給出的幫助文檔(默認狀況下)app

  • get,不帶id參數——index
  • get,帶id——show
  • post,不帶id——create
  • put,帶id——update
  • delete,帶id——destroy
  • get,帶id且訪問edit資源——edit
  • get,不帶id訪問edit資源——editNew

rest的相關常量

  •  - If set, this value will be the name of the parameter under which the id is stored. The id will then be removed from the action name. Whether or not the method is specified, the mapper will  try to truncate the identifier from the url and store it as a parameter.
  •  - The method name to call for a GET request with no id parameter. Defaults to index.
  •  - The method name to call for a GET request with an id parameter. Defaults to show.
  •  - The method name to call for a POST request with no id parameter. Defaults to create.
  •  - The method name to call for a PUT request with an id parameter. Defaults to update.
  •  - The method name to call for a DELETE request with an id parameter. Defaults to destroy.
  •  - The method name to call for a GET request with an id parameter and the edit view specified. Defaults to edit.
  •  - The method name to call for a GET request with no id parameter and the new view specified. Defaults to editNew.

在struts.xml裏面設置這些常量能夠改變訪問的method以及id參數在action中的的變量名jsp

配置

  • 使用restful開發action的package要繼承自rest-default
  • 在struts.xml中配置 爲了讓struts根據rest的約定解析URL且映射到具體的action
  • 將struts2-convention-plugin-2.1.6.jar、struts2-rest-plugin-2.1.6.jar導入項目
  • 將 xstream-1.2.2.jar、json-lib-2.1.jar、ezmorph-1.0.3.jar導入項目

注意

  • jsp只支持get、post、head方法訪問,因此put和delete就返回json數據給前臺
  • 要返回json數據的話,須要在package繼承"json-default"
  • 帶id參數,是指在url中直接加入id,而不是parameter中,也不須要key,只用value 好比/123,123就是id,/haha,haha也是個id
  • ?id=123這種不是帶id參數的訪問
  • id和edit結合的話,id在edit前面 好比/123;edit或者/123/edit
查看原文:http://www.wewill.top/2016/06/07/struts%e6%8b%93%e5%b1%95restful/struts.mapper.idParameterNamestruts.mapper.indexMethodNamestruts.mapper.getMethodNamestruts.mapper.postMethodNamestruts.mapper.putMethodNamestruts.mapper.deleteMethodNamestruts.mapper.editMethodNamestruts.mapper.newMethodName<constant name="struts.mapper.class" value="rest" />
相關文章
相關標籤/搜索