• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Problem in setting bean scope to request

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am trying to implement a very simple example, where I am invoking one jsp from another using jsf and spring framework. given below are the code snippets -

web.xml contents -


<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<!--context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param-->

<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Faces Servlet -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>-1</load-on-startup>
</servlet>


<!-- Faces Servlet Mapping -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>

</web-app>

faces-config contents -

<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config>

<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
</application>

<managed-bean>
<description>
Backing bean that contains transaction information.
</description>
<managed-bean-name>firstBean</managed-bean-name>
<managed-bean-class>com.rnd.web.faces.backinbean.FirstBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>

<navigation-rule>
<from-view-id>/pages/one.jsp</from-view-id>
<navigation-case>
<from-outcome>second</from-outcome>
<to-view-id>/pages/two.jsp </to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>

</faces-config>

application-context contents -


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<bean id="firstBean" class="com.rnd.web.faces.backinbean.FirstBean" scope="request">
</bean>
</beans>

I am calling FirstBean.firstToSecondMethod from one.jsp, setting some values in the method and calling two.jsp -

public String firstToSecMethod(){
System.out.println("inside java method.. ");
firstProp = "prop set in FirstBean java";
secondProp = 10;
return "second";
}

but two.jsp does not display the values

<td><h:outputText value="#{firstBean.firstProp}"/></td>

after changing the bean scope to session values are getting displayed properly.

I am not sure why this is happening.

I think request scope should work fine. Please advise.


Thanks,
Abhijeet
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's similar to http request and session scope.

For example, if on 1 page you set a value on the request, and forward to another page and try to read that value off the request object, you won't get it. A new request object has been created for that page.

If you want to get values from a previous page you have to store those values on the session.

JSF has 4 scopes, None, Request, Session and Application.

So let's say you had a LoginBean, that only did some kind of verification, nothing else. You would put that into the request scope, and call it when the user clicked login, for example. but now let's say after logon, you want to say hello to that user, so he logs on as billybob, and upon successfully logon, you want to say hello billybob, And you want to read the username from the bean, you will have to put that bean into the session scope, so that it is not discarded when you forward to the welcome page.

Does this make sense?
 
Abhijeet Vaidya
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply.

yeah, that's what it looks like. But won't it cause too much load on the server? if practically, backing beans (with domain objects) are same for data input and display and all are stored in session?

Also, if we do request.setAttribute() in java method, we can access those values in normal jsp, but it seems like that is not happening in case of JSF.

Correct me if I am wrong.


Thanks,
Abhijeet
[ June 02, 2008: Message edited by: Abhijeet Vaidya ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic