configure request param for action to be assigned to fields of bean

一曲冷凌霜 提交于 2019-12-04 01:48:45

问题


I am using www.datatables.net. JS framework to show data in tables. It has server mode and it sends a lot of params during this mode.

Example: sortColumn, sortType(asc,desc), filter values, pagenum, itemsonpage and so on.

So i handle it in action. i assign to each request param field in action and it work fine.

But now i have several table. So i have to make different actions but request params same and there are a lots of them. It is not a good idea to copy paste code from one action to another.

So i did implement a DatatableParamBean which contain all params needed to work properly.

problem is that params send this way iSortColumn, iDisplayTotalLength, iTotalItems and so on but i need to them to be assigned to bean fields.

bean.iSortColumn, bean.iDisplayTotalLength and so on.

Consider that DatatableParamBean has reference in my action class as 'bean';

If there is a way to override default mechanism of assigning request param values? Only solution i found for now is to create an action say DatatableAction class with all this params and create an new action if i need to handle dataatble, using extending from DatatableAction


回答1:


This is usual way to associate or aggregate a bean to the action class. The action class properties can be used directly by name that have property accessors. Nested beans properties are accessible via OGNL by specifying proper OGNL expression which is a path to the property. Assumed all properties accessors have not null references to beans. That could be achieved via providing corresponding getters and setters to properties and initializing bean references if necessary. So, bean.iSortColumn, bean.iDisplayTotalLength are valid OGNL expressions to set/get the bean properties. But you need to initialize it in the action. Like this

private Bean bean = new Bean();

public Bean getBean() { return bean; }

References:

  • To be familiar how OGNL works you can read in the OGNL Basics.
  • The base OGNL reference including a link to the OGNL language guide.


来源:https://stackoverflow.com/questions/21184608/configure-request-param-for-action-to-be-assigned-to-fields-of-bean

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!