Struts 2 动态参数绑定
默认堆栈具有 parms 拦截器,它将参数绑定到正在调用的操作中找到的设置器。当您知道参数名称时,效果很好。例如,如果请求参数是“employee.name”,则可以将其绑定到我的操作的属性,该操作具有属性“employee”和属性“name”。为了让它工作,我需要知道请求参数名称是什么,并在我的操作中放置一个名为 setEmployee() 的对象类型 Employee 的 setter,或者它也可以是一个 Map。
如果我想让操作将该参数绑定到另一个我不知道哪个属性,该怎么办?假设该操作接收将设置请求参数的名称作为参数。
<s:action name="showEmployee" executeResult="true">
<s:param name="employeePrefix">xyz.wz.empl</s:param>
</s:action>
这意味着将员工的所有参数绑定到 xyz.wz.empl 的操作。 例如,假设请求参数具有以下内容: xyz.wz.empl.name=阿尔弗雷多 xyz.wz.empl.lastName=Osorio
我想将其绑定到我的操作的属性(假设是 Map 员工),但这不起作用,因为请求参数是 xyz.wz.empl。如何使用发送到操作的参数 (employeePrefix) 将该动态参数绑定到调用的操作。
我可以请求请求参数
ActionContext.getContext().getParameters()
并自己进行转换,但我认为必须有另一种方法可以显式调用 Struts 2 框架中的某些内容进行转换,就像 com.opensymphony.xwork2.interceptor.ParametersInterceptor 那样。
谢谢。
The default stack has the parms interceptor who binds the parameters to the setters found in the action who is being invoked. That works fine when you know the name of the parameter. For example if a request parameter is "employee.name" that could be binded to a property of my action which has a property "employee" with a property "name". In order to get it to work I need to know which name the request parameter name would be and put a setter in my action called setEmployee() of an object type Employee or it could be a Map too.
What if I want to let the action bind that param to another property that I don't know which one will be. Let's say that the action receive as a parameter the name on which the request parameter will be set.
<s:action name="showEmployee" executeResult="true">
<s:param name="employeePrefix">xyz.wz.empl</s:param>
</s:action>
That would mean to the action to bind all the parameters of the employee to xyz.wz.empl.
For example let's say the request parameter has the following:
xyz.wz.empl.name=Alfredo
xyz.wz.empl.lastName=Osorio
I would like to bind that to a property of my action, let's say a Map employee, but that won't work because the request parameter is xyz.wz.empl. How can I bind that dynamic parameter to the invoked action using the parameter that was sent to the action (employeePrefix).
I could ask for the request parameters
ActionContext.getContext().getParameters()
and do the conversion myself but I think there must be another way to explicitly call something from the Struts 2 framework to the conversion, in the way that com.opensymphony.xwork2.interceptor.ParametersInterceptor does.
Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这不是一个理想的解决方案,但它可以满足您的需要。
现在,对于下一部分,您将需要使用反射。
例如,您需要执行以下操作:
这应该将该映射中的所有参数映射到员工对象中的字段(如果映射中存在该字段)。当然,这可以移植到一个单独的方法,该方法属于 Util 类之类的东西,您可以在其中传递要反映的类。
This is not an ideal solution in my opinion but it will do what you need.
Now for this next portion you will need to use reflection.
So for example you will need to do this:
This should map all parameters in that map to a field in the employee object if it exists in the map. This can be of course ported out to a separate method that would belong in something like a Util class where you pass in the class you want to reflect on.