0

I have been working on migrating a web application from Struts1 to Struts2.

I have a simple Form with around 45 Fields (basically a grid with data). I have to capture all those in Struts2 Action.I noticed that in struts2 we have OGNL through which we just write getters setters in action itself by declaring fields locally to get those variables flowing in the request. I cannot write 45 getters/setters in my action, there must be a way to pass whole object(a POJO) from jsp to Action layer.

In Struts 1, we normally get a ActionForm Object and/or get request parameters in a map and then populate.

user229044
  • 232,980
  • 40
  • 330
  • 338
Neeraj
  • 1,163
  • 2
  • 18
  • 32
  • If the getter/setter exposes a collection then the type conversion will push all the same named attributes into that collection. You can also use an array/list and use array notation. This works from client -> action and action -> jsp. – Quaternion Apr 10 '12 at 23:31
  • i understand declaring an array in struts action and getting it will work but my all field names are different and i dont want to give them notations with same. isn't there somethign like an ActionForm in struts1 ? I request.getParametersMap something like this i could fire to get them in a bunch. I tried to fetch request parameters but nothing there as well. In url i see the params as passed in GET. – Neeraj Apr 11 '12 at 00:18
  • You're going to need to get them into variables at some point right?! In your ide you can just write. "String name; int age; ..." then I highlight the bunch and ask it to encapsulate the fields (I'm sure they can all do this this). It will write the getters and setters and add private before the fields. You should at least try it the struts2 way before doing weird things. 45 separate fields is a bit... if they were lines, like employee details then you know you can even send colections of complex types right? Just look into this. Say the grid had nine lines by 5 details... – Quaternion Apr 11 '12 at 01:13
  • 1
    line1 could be written as: "employee.v1, employee.v2,..., emp.vn", line2 exactly the same... "employee.v1, employee.v2,..., emp.vn", which clearly are easy to iterate over and struts2 can then produce a collection of employees from this (assuming an employee has the required getters/setters) – Quaternion Apr 11 '12 at 01:17
  • 2
    @Neeraj: if they are simple fields and you want them to be sent using POJO, you have 2 ways 1.) Use object backed bean property 2) user model driven interface.http://struts.apache.org/2.3.1/docs/model-driven.html – Umesh Awasthi Apr 11 '12 at 03:48
  • Thanks Quaternion and Umesh - I believe that its better to use Model Driven which looks promising and more cleaner action code. This Pojo is actually a Hibernate Pojo for me, this will work for me. – Neeraj Apr 11 '12 at 15:58
  • If i declare a pojo object in my action e.g ABC abc; where ABC is class with three variables a, b, c. I can use in my form id's as abc.a = 10 .. or abc.b = 20. It automatically populates the Pojo class in Action. – Neeraj Apr 11 '12 at 16:01
  • Can you redefine your question in terms of what you now know, or if resolved delete it. – Quaternion Apr 11 '12 at 22:08

0 Answers0