Struts 2 动态参数绑定

发布于 2024-09-28 07:30:05 字数 888 浏览 5 评论 0原文

默认堆栈具有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹏半生如梦愿梦如真 2024-10-05 07:30:06

我认为这不是一个理想的解决方案,但它可以满足您的需要。

  1. 您的 Struts 2 Action 将需要实现 SessionAware 和 ParameterAware 接口。
  2. 创建一个名为“parameters”的映射的私有字段,并为其创建一个 getter/setter。
  3. 现在,当您调用 getParameters() 时,您将获得名称/值对的映射。

现在,对于下一部分,您将需要使用反射。

例如,您需要执行以下操作:

Map myParams = getParameters()
Employee e = new Employee()

Class clas = null;
try {
    clas = Class.forName("Employee");
    Field fieldlist[] = clas.getDeclaredFields();
    for (int i = 0; i < fieldlist.length; i++) {
        Field fld = fieldlist[i];
        if(myParams.containsKey(fld.getName()))
            fld.set(e, myParams.get(fld.getName())
    }

} catch (ClassNotFoundException ex) {
// handle exception case
}

这应该将该映射中的所有参数映射到员工对象中的字段(如果映射中存在该字段)。当然,这可以移植到一个单独的方法,该方法属于 Util 类之类的东西,您可以在其中传递要反映的类。

This is not an ideal solution in my opinion but it will do what you need.

  1. Your Struts 2 Action will need to implement SessionAware and ParameterAware interfaces.
  2. Create a private field that is a map called 'parameters' and create a getter/setter for it.
  3. Now when you call getParameters() you will have a map of your name/value pairs.

Now for this next portion you will need to use reflection.

So for example you will need to do this:

Map myParams = getParameters()
Employee e = new Employee()

Class clas = null;
try {
    clas = Class.forName("Employee");
    Field fieldlist[] = clas.getDeclaredFields();
    for (int i = 0; i < fieldlist.length; i++) {
        Field fld = fieldlist[i];
        if(myParams.containsKey(fld.getName()))
            fld.set(e, myParams.get(fld.getName())
    }

} catch (ClassNotFoundException ex) {
// handle exception case
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文