You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by arin_12 <ar...@lycos.com> on 2011/05/10 20:26:06 UTC

Separate Java Bean Class for Getter/Setter method.

I am trying to develop a small struts apps. I want to write all the setter
and getter method in a separate Bean file and want to keep the controller
clean :) If we remem, struts 1.2 style. 

I dont want to use viewname.elementname style. This create a issue in the
javascript validation. As JS is not able to recognize the "." dot. 

<s:textfield id="alName" name="objAllergyView.alName"/>
<s:textfield id="alDescription" name="objAllergyView.alDescription"/>

Thanks,

--
View this message in context: http://struts.1045723.n5.nabble.com/Separate-Java-Bean-Class-for-Getter-Setter-method-tp4385274p4385274.html
Sent from the Struts - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Separate Java Bean Class for Getter/Setter method.

Posted by arin_12 <ar...@lycos.com>.
Here is my whole code. implement ParameterAware. 

Declaration of MAP.

Map&lt;String, String[]&gt; parameters = null;

Call this method.

getParameters("alId")





	public void setParameters(Map&lt;String, String[]&gt; params) {
		// TODO Auto-generated method stub
		this.parameters = params;
	}

	public String getParameters(String param) {
		StringBuffer sbVal = new StringBuffer();
		paramVal = (String[]) parameters.get(param);
		for (int i = 0; i < paramVal.length; i++) {
			sbVal.append(paramVal[i]);
		}
		return sbVal.toString();
	}



--
View this message in context: http://struts.1045723.n5.nabble.com/Separate-Java-Bean-Class-for-Getter-Setter-method-tp4385274p4442897.html
Sent from the Struts - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Separate Java Bean Class for Getter/Setter method.

Posted by Chris Pratt <th...@gmail.com>.
Yes, the map is Map<String,String[]>.  Because the HTTP protocol allows
sending multiple parameters with the same name.
  (*Chris*)
On May 19, 2011 2:06 AM, "arin_12" <ar...@lycos.com> wrote:
> I also tried Map <String String>. But the same result.
> .toString also does not help any thing. It looks like the Parameter from
> request is returning
>
> Map<String , String[]>.
>
> Later If i run the value as String array I get the value. The length is
> always sets to 1.
>
> String st[] = map.get("param");
> value = st[1];
>
> This gives me the exact value. This extra code I am writing in every
action
> class.
>
> Any better solution ?
>
> --
> View this message in context:
http://struts.1045723.n5.nabble.com/Separate-Java-Bean-Class-for-Getter-Setter-method-tp4385274p4408881.html
> Sent from the Struts - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>

RE: Separate Java Bean Class for Getter/Setter method.

Posted by arin_12 <ar...@lycos.com>.
I also tried Map <String String>. But the same result. 
.toString also does not help any thing. It looks like the Parameter from
request is returning 

Map<String , String[]>. 

Later If i run the value as String array I get the value. The length is
always sets to 1.

String st[] = map.get("param");
value = st[1];

This gives me the exact value. This extra code I am writing in every action
class. 

Any better solution ?

--
View this message in context: http://struts.1045723.n5.nabble.com/Separate-Java-Bean-Class-for-Getter-Setter-method-tp4385274p4408881.html
Sent from the Struts - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Separate Java Bean Class for Getter/Setter method.

Posted by "Biesbrock, Kevin" <Bi...@aoins.com>.
You have to override the Object's toString method.  Why is your map
holding a Object?  Should be one of your classes.

Beez
r 5347 
 

> -----Original Message-----
> From: arin_12 [mailto:arin_12@lycos.com] 
> Sent: Wednesday, May 18, 2011 1:13 PM
> To: user@struts.apache.org
> Subject: Re: Separate Java Bean Class for Getter/Setter method.
> 
> Now the URL looks like, No extra amp;
> http://localhost:8080/Samvidh_CIS/doEditAllergy.action?alId=2&
> alDescription=algy_full_desc&alName=algy_short_desc
> 
> Now the New problem is that, I am not able to retrieve the 
> value of the key though I am able to see the key.
> 
> Code in Action Class-
> 
> 		Map&lt;String, Object&gt; map =
> ActionContext.getContext().getParameters();	
> 		Iterator entries = map.entrySet().iterator();
> 		while (entries.hasNext()) {
> 		    Map.Entry entry = (Map.Entry) entries.next();
> 		    String key = entry.getKey().toString();
> 		    String value = entry.getValue().toString();
> 		    System.out.println("Key = " + key + ", 
> Value = " + value);
> 		}
> 
> Console Output.
> 
> Key = alDescription, Value = [Ljava.lang.String;@b90a6e Key = 
> alId, Value = [Ljava.lang.String;@b4b0a4 Key = alName, Value 
> = [Ljava.lang.String;@5bcd91
> 
> Help Please...
> 
> --
> View this message in context: 
> http://struts.1045723.n5.nabble.com/Separate-Java-Bean-Class-f
> or-Getter-Setter-method-tp4385274p4407093.html
> Sent from the Struts - User mailing list archive at Nabble.com.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Separate Java Bean Class for Getter/Setter method.

Posted by arin_12 <ar...@lycos.com>.
Now the URL looks like, No extra amp;
http://localhost:8080/Samvidh_CIS/doEditAllergy.action?alId=2&alDescription=algy_full_desc&alName=algy_short_desc

Now the New problem is that, I am not able to retrieve the value of the key
though I am able to see the key.

Code in Action Class-

		Map&lt;String, Object&gt; map =
ActionContext.getContext().getParameters();	
		Iterator entries = map.entrySet().iterator();
		while (entries.hasNext()) {
		    Map.Entry entry = (Map.Entry) entries.next();
		    String key = entry.getKey().toString();
		    String value = entry.getValue().toString();
		    System.out.println("Key = " + key + ", Value = " + value);
		}

Console Output.

Key = alDescription, Value = [Ljava.lang.String;@b90a6e
Key = alId, Value = [Ljava.lang.String;@b4b0a4
Key = alName, Value = [Ljava.lang.String;@5bcd91

Help Please...

--
View this message in context: http://struts.1045723.n5.nabble.com/Separate-Java-Bean-Class-for-Getter-Setter-method-tp4385274p4407093.html
Sent from the Struts - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Separate Java Bean Class for Getter/Setter method.

Posted by arin_12 <ar...@lycos.com>.
http://localhost:8080/Samvidh_CIS/doEditAllergy.action?alId=2&amp;alDescription=algy_full_desc&amp;alName=algy_short_desc

This the URL that is generating.
It is al not all.


--
View this message in context: http://struts.1045723.n5.nabble.com/Separate-Java-Bean-Class-for-Getter-Setter-method-tp4385274p4400903.html
Sent from the Struts - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Separate Java Bean Class for Getter/Setter method.

Posted by Dave Newton <da...@gmail.com>.
On Thu, May 12, 2011 at 11:59 AM, arin_12 wrote:
> I am passing three parameters via URL from one page to another.
> I only see one available on the second page; don't know why.
>
> Page one
>
> <s:url action="doEditAllergy" id="url">
>     <s:param name="alDescription" value="%{#view.alDescription}" />
>     <s:param name="alId" value="%{#view.alID}" />
>     <s:param name="alName" value="%{#view.alName}" />
> </s:url>
>
> Page two
>
> <s:textfield id="alDescription" name="alDescription" value="%{#parameters.alDescription}"/>
> <s:checkbox id="alEnabled" name="alEnabled" value="true" />
> <s:hidden name="alID" value="%{#parameters.alId}"/>
>
> Any feed back ?

Sure.

* Is it "al", or "all"? You show both. IMO that's a bad idea--too
similar if they're supposed to be different.
* You say you pass three, get one. Which one?
* You say you pass three, get one; I only see two references to parameters.
* Is the URL being constructed properly? Check the HTML source.

Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Separate Java Bean Class for Getter/Setter method.

Posted by arin_12 <ar...@lycos.com>.
I dont know if its a bug or functinoality. I am passing 3 param from a page.
I am only able to receive the 1st one in the next page... Dont know why.

Page one :- 
<s:url action="doEditAllergy" id="url">
		<s:param name="alDescription" value="%{#view.alDescription}" />
		<s:param name="alId" value="%{#view.alID}" />
		<s:param name="alName" value="%{#view.alName}" />
</s:url>

Page two :- 

	<label for="alDescription">Description</label>
	<s:textfield id="alDescription" name="alDescription"
value="%{#parameters.alDescription}"/>	
</p>	
<p>		
	<label for="alEnabled">Enable this Allergy ?</label>
	<s:checkbox id="alEnabled" name="alEnabled" value="true" />
	</p><p>			
	<s:hidden name="alID" value="%{#parameters.alId}"/>


Any feed back ?

--
View this message in context: http://struts.1045723.n5.nabble.com/Separate-Java-Bean-Class-for-Getter-Setter-method-tp4385274p4390648.html
Sent from the Struts - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Separate Java Bean Class for Getter/Setter method.

Posted by arin_12 <ar...@lycos.com>.
Thanks for the reply. I had tried the same thing and again removed. Will
there be any dis/adv if I use this Model class ?
Another question is how can i pass/retrieve a parameter in my action class ?
http://localhost:8080/Samvidh_CIS/doEditAllergy.action?allergy.Id=1002

I want this allergy.Id=1002 value in action controller so that I can query
the DB to read the necessary data. 

--
View this message in context: http://struts.1045723.n5.nabble.com/Separate-Java-Bean-Class-for-Getter-Setter-method-tp4385274p4387786.html
Sent from the Struts - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Separate Java Bean Class for Getter/Setter method.

Posted by foramp <fa...@gmail.com>.
Hi,

If you want to keep the bean in a separate class then your action class that
needs to implement two interfaces Modeldriven and Preparable. Its called
model driven approach in struts 2

class MyBean
{
// attributes
// getter and setter
}

class MyAction extends ActionSupport implements ModelDrive, Preparable
{

MyBean myBean;

public Object getModel()
{
return myBean;
}

public void prepare()
{
myBean = new MyBean();
}

}


}


This is beautifully explained in the book Struts 2 black book on page 
number 435. please find at

http://books.google.co.in/books?id=-V86IiyqZ_YC&pg=PA357&lpg=PA357&dq=struts2+black+book+model+driven+and+preparable+example&source=bl&ots=riNsAvbHwX&sig=NclG8_JKyS3sKH6taJFCKgmjGvQ&hl=en&ei=MF3KTe2DN4nNrQemwLCJBQ&sa=X&oi=book_result&ct=result&resnum=1&ved=0CBgQ6AEwAA#v=onepage&q&f=false
http://books.google.co.in/books?id=-V86IiyqZ_YC&pg=PA357&lpg=PA357&dq=struts2+black+book+model+driven+and+preparable+example&source=bl&ots=riNsAvbHwX&sig=NclG8_JKyS3sKH6taJFCKgmjGvQ&hl=en&ei=MF3KTe2DN4nNrQemwLCJBQ&sa=X&oi=book_result&ct=result&resnum=1&ved=0CBgQ6AEwAA#v=onepage&q&f=false 




arin_12 wrote:
> 
> I am trying to develop a small struts apps. I want to write all the setter
> and getter method in a separate Bean file and want to keep the controller
> clean :) If we remem, struts 1.2 style. 
> 
> I dont want to use viewname.elementname style. This create a issue in the
> javascript validation. As JS is not able to recognize the "." dot. 
> 
> <s:textfield id="alName" name="objAllergyView.alName"/>
> <s:textfield id="alDescription" name="objAllergyView.alDescription"/>
> 
> Thanks,
> 


--
View this message in context: http://struts.1045723.n5.nabble.com/Separate-Java-Bean-Class-for-Getter-Setter-method-tp4385274p4386991.html
Sent from the Struts - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org