Rookie007
Rookie007

Reputation: 1219

is it possible to transfer a class object as hidden variable from JSP to action

I am using Struts 1.x as my framework,

Here I have situation like I need to pass my custom class object as Hidden variable Like I want to do something like below

  <html:form name="FormName">
   <html:hidden property="ClassObject" value="<%=new MyClass() %>" >
  </html:form>

I my FormName form I have created getters and setters for ClassObject as shown below.

     private MyClassObject ClassObject; 

     public MyClassObject getClassObject()
       {
             return ClassObject;
              }
      //setter also I have created

So In my action class I am trying to get that ClassObject as below

      MyClassObject obj = ((FormName) form).getClassObject();

But when I do like this I am getting error as Unable to crate class for JSP at line

      `<html:hidden property="ClassObject" value="<%=new MyClass() %>" >`

My doubt is: Is it possible to carru objects like that if so Where I am mistaking ?

         If not What is the best way to pass Object from JSP to action ? 

Thanks in advance.

Upvotes: 0

Views: 625

Answers (1)

Jericho
Jericho

Reputation: 10953

No,Because whatever you are sending from client side is just html not any objects.Request only having set of Strings like (Header,QueryString and body etc).

To pass Human Object to Servler Side from client you can use JSON.Otherwise go with Session.

JSON:

Class Human
{
private int id;
privae String name;
// other attributes etc
}

[{id:"",name:""}]---this is String but you can bring that using JSON related APIs.

P.S :JSON related idea got from Scary.

Upvotes: 2

Related Questions