aspose file tools*
The moose likes Struts and the fly likes FormBean value on jsp page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "FormBean value on jsp page" Watch "FormBean value on jsp page" New topic
Author

FormBean value on jsp page

Kanchan Singh
Ranch Hand

Joined: Dec 27, 2007
Posts: 38
Hi All

I am using struts 1.2 what I want is that
I want to get a bean value directly on a jsp page without setting in the request attribute.I am setting the bean attribute in action class.

Please Help......................



Thanks & Regards
Kanchan
shalindra Singh Suryvanshee
Greenhorn

Joined: Aug 05, 2008
Posts: 25
Hi,

<action path="/changepassword"
type="com.changepassword.web.action.ChangepasswordAction"
name="changepassword"
parameter="changePassword"
scope="request"
validate="true" input="/jsp/changepassword/changepassword.jsp">
<forward name="success" path="/jsp/success1.jsp"/>
<forward name="error" path="/jsp/changepassword/changepassword.jsp"/>
<forward name="failure" path="/jsp/error.jsp"/>
</action>

If you are mentioning 'scope="request"' then in the immediate forwarded jsp like success1.jsp you will get access of form bean in this jsp in the request object.


Regards
Shalindra Singh
SCJP, SCWCD
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

and you can use

<jsp:useBean id="loginBean"
type="**relative / or fully qualified class path of your bean here**"
scope="page">

<jsp:getProperty
property="userName"
name="loginBean"/>

</jsp:useBean>

where 'name' attribute gives the name of your bean and 'property' attribute is the attribute from the bean which is being displayed.


Anrd
"One of the best things you could do is to simplify a larger application into a smaller one by reducing its process and complexity - Fowler"
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Hi Annirudha,

Thats fine using JSTL tags , but isn't there any tag in Struts tld which is equivalent jsp:usebean tag .!


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

yes, we can use

$(loginBean.userName)

...but i m not sure it works with struts 1.2, it sure does with Struts 2.0
Kanchan Singh
Ranch Hand

Joined: Dec 27, 2007
Posts: 38
Hi Aniruddh
Thanks for reply
But this is not working either.
what I is as per you suggestion
test.jsp
<jsp:useBean id="one" class="test.BeanClass" scope="session">

<jsp:getProperty name="one" property="name" />
</jsp:useBean>


public class Test extends Action
{
public ActionForward execute() throws Exception
{

BeanClass cs=new BeanClass();
cs.setName("Kanchan");
request.setAttribute("bean", cs);
return mapping.findForward("success");
}
}

OUTPUT is
it is able to set the value but not able to get
when I use setProperty of useBean it can set and get both......


Any suggestions.
Regards
Kanchan
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

Hi kanchan !!
Sorry for the delay, but wasn't in touch for a while.

I have 2 doubts in your code;
#1) you are using 2 diffrent objects for setting adn getting the value..

Originally posted by Kanchan Singh:

test.jsp
<jsp:useBean id="one" class="test.BeanClass" scope="session">

<jsp:getProperty name="one" property="name" />
</jsp:useBean>


public class Test extends Action
{
public ActionForward execute() throws Exception
{

BeanClass cs=new BeanClass();
cs.setName("Kanchan");




and #2: you need not save the value in request if you can retrieve it using usebean..




request.setAttribute("bean", cs);




As you have observed, if you set it in the JSP, it is avialable..this is because you save the value in one object(bean) and retrieve from the same..

I suggest to try using the same bean name(cs) as instantiated in your action class while getting the values...
I am sure about your your mistake, but I leave this experiment to you..let's see the result
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

Hi Kanchan !!


I think I have the solution...
create a dummy variable in your bean ..say 'String dummy'
and in the setDummy() method call all the setters you want ...i.e

set all your useful variables from the setter method of your dummy...


now in your JSP...call only the setter of your dummy variable with any arbitary String...and all your useful setters will be called..
hence you can retrieve them using getters.

This approach is used while doing DB transactions..
if you want...I can write the
Kanchan Singh
Ranch Hand

Joined: Dec 27, 2007
Posts: 38
Hi Aniruddh

Thanks for reply.
As you said "request.setAttribute" is not necessary this was my mistake.
Thanks

But I am getting null pointer exception on receiving request value in Action class .

What I want is setting request in an JSP page and getting value in an action class.

Will you please send me the example of your second way of setting object in bean . That was not claer.
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

Hi Kanchan,
I will show one example each for

a) setting in Action and getting in JSP
b) setting and getting from JSP but values decided in Action class

a) Action class

....execute(....){

// creating an object with some values
LoginBean loginObject = new LoginBean();
loginObject.setUserName("Aniruddh");
loginObject.setPassword("Joshi");

//storing this object in the request

request.setAttribute("loginObject",loginObject);

return mapping.findForward("VIEW_CREDENTIALS");

}

-----------------------------------------

<struts-config>

<.....>
<forward name="VIEW_CREDENTIALS" path="/ViewCredentials.jsp"/>
</>

----------------------------------------

ViewCredentials.jsp:


<html>
<h4>Hi !! The server has stored following values for you:</h4>

Username: ${loginObject.username}
<br>
Password: ${loginObject.password}

<%--the above expression is equivalent to jsp:usebean and getProperty --%>

</html>
[ August 14, 2008: Message edited by: Aniruddh Joshi ]
Kanchan Singh
Ranch Hand

Joined: Dec 27, 2007
Posts: 38
Hi Aniruddh

Thanks for the replay.

I have done same thing what you have suggested in action class but not able to get bean values on JSP page.
I tried this in JSP page:
<jsp:useBean id="one" beanName="test.BeanClass" type="test.BeanClass" scope="request"/>
<jsp:getProperty name="one" property="name" />

This is not giving me any output and when I write

<jsp:useBean id="one" beanName="test.BeanClass" type="test.BeanClass" scope="request"/>
<bean:write name="one"/>

it gives me "null test.BeanClass@d28dfa" as output on jsp page..

I am not understanding why.
Any solution.

Thanks in advance
Kanchan
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

Hi Kanchan!!

Your 2nd approach is correct, but there's one small mistake..you have not mentioned which property of the bean 'one' do you want to display..

just add property = "anyProperty" to your <bean:write ..../> tag , it will work.

I have just tried it with your code.



You are getting this garbage value because you're trying to display the onject's value...which is always random untill you override the toString() method in its class...

i am not sure why 'null' is printed before your package.Classname; I suspect it is because you have some null value in your code.

I got only package.class@garbage when I tried your jsp code in my Application
[ August 18, 2008: Message edited by: Aniruddh Joshi ]
Kanchan Singh
Ranch Hand

Joined: Dec 27, 2007
Posts: 38
Hi Aniruddh

I tried as per your advice and what I did is
<jsp:useBean id="one" beanName="test.BeanClass" type="test.BeanClass" scope="session"/>
<bean:write name="one" property="name"/>

output is "null"
Not the desired output..

I have one more requirement
My need is to set a list object in an request.setAttribute() on a JSP page and retrieve it on Action class.
Even this is not working
Is it that we cannot set an object in request scope in an JSP page.
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

Hi kanchan !!!

I don't understaand why your bean has a null value..scope= request works.
And your second requirement is also not difficult.Its a very common thing.

Can you mail me your code( struts-config. action and JSP )..I think I can fix it.
Kanchan Singh
Ranch Hand

Joined: Dec 27, 2007
Posts: 38
Hi Aniruddh

Thank You for your patience.

My code goes as follows on clicking below link problem starts.

<html:link action="test"><font color="#000000">Test</font></html:link>
***********************
My struts-config.xml
***********************
<action
path="/test" type="test.Test" scope="session">
<forward name="success" path="/pages/test.jsp"/>
</action>
*************************
My Action class
*************************
public class Test extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
{
System.out.println("inside test -------------");
BeanClass cs=new BeanClass();
// BeanClass cs=(BeanClass)form;


cs.setName("Kanchan");
request.setAttribute("bean", cs);
return mapping.findForward("success");
}
}
***********************
My JSP page
***********************
<body>
<jsp:useBean id="one" beanName="test.BeanClass" type="test.BeanClass" scope="session"/>

<bean:write name="one" property="name"/>

</body>
***********************
My Bean Class
***********************
<form-bean name="name1" type="test.BeanClass"></form-bean>
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

Hi Kanchan !!!

you have done one small mistake...

request.setAttribute("bean", cs);


sets an attribute called "bean"

but you're calling one called "one"



changing either of the names would work...i testeed with naming both as "one"

and ..once that is done you can replace the <bean:write by



now your issue about setting from JSP will be dealt...maybe later tonight or tomorrow
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FormBean value on jsp page