Ad

Monday 24 August 2009

Struts - Cannot find bean in any scope

When we develop application using struts ,the most common error we encounters are:
(1)Cannot find bean 'myformbeanname' in any scope
(2)Cannot find bean 'mylistname' in any scope
(3)Cannot find bean error in any scope
(4)Cannot find bean org.apache.struts.taglib.html.BEAN in any scope

(1)Cannot find bean 'myformbeanname' in any scope
we will encounter this error in Jsp when we incorrectly use formbean in the Struts bean and logic tags.The cause for this error may be due to one of the below:
(a) If the formbean Name is misspelled in the Jsp.So make sure the correct formbean name is used by comparing it in struts-config.xml
(b)If we set the scope of the formbean as request and if use the forward tag as shown below

<action path="/actionurl" type="com.Sample" name="SampleFormbean" scope="request">
<forward name="view" path="sample.jsp" redirect="true"/>
</action>


The above forward tag will make the struts to use 'sendRidrect' instead of
requestdispatcher (forward method) to forward the request to jsp from the action(Sample.java).since the scope of the formbean is request,the formbean wont be available in JSP and hence we will get this error.To rectify this ,do either one of the below:
(i)set the scope of the formbean as session as shown below,so that the formabean will be available in jsp when using sendredirect by struts .

<action path="/actionurl" type="com.Sample" name="SampleFormbean" scope="session">
<forward name="view" path="sample.jsp" redirect="true"/>
</action>


(OR)
(ii)Remove the redirect attribute from the forward tag,so that the struts will use requestdispatcher to display the jsp and the formbean which is in request scope will be available to them.

<action path="/actionurl" type="com.Sample" name="SampleFormbean" scope="request">
<forward name="view" path="sample.jsp"/>
</action>


(2)Cannot find bean 'mylistname' in any scope
We get this error when we try to access the list(which is set by the servlet/action in some scope) in jsp.A typical example will be the optionCollection tag which will dynamically populate the dropdown box with list. The cause of this error may due to one of the below
(a) if the list name is misspelled in the struts tag in Jsp.Check the list attribute name in Servlet/action in which the list is set in some scope.
(b) if we set the scope of the list as request in the Action and in the forward tag if we use the attribute redirect="true".

(3)Cannot find bean error in any scope
we get this error when we do not properly implement the validation framework.The cause of this error may due to one of the below
(a) In the Formbean validate method, we add the error key for the error message to be displayed as shown below

errors.add("fieldblank",new ActionMessage("errkey1"));


If the above error key("errKey1") is not configured in the message resource properties file, then the <html:error/> used in Jsp will throw the error "Cannot find bean error in any scope".
(b)Make sure the message resource properties file in the correct classpath.If you use the properties file name other than "ApplicationResources.properties",then make an entry in the struts config like below

<message-resources parameter="com.ErrorFile" />


(4)Cannot find bean org.apache.struts.taglib.html.BEAN in any scope
we get this error,if we put our Struts tag like <html:hidden>,<html:text> outside the <html:form>tag in JSP.So make sure you put all the struts html tags inside <html:form></html:form> block.

2 comments:

  1. Thank you so much for sharing this precious information. Following your above solution, I was able to successfully resolve "Cannot find bean error in any scope" issue. I got stuck for a whole week and wasn't able to fix it. Your posting made big difference.

    ReplyDelete
  2. Thank you! I have been stuck for several days on this. Turns out my issue was #3. I did not have the resource properties file updated with the correct message name I was using in the code. Took me ages to find this page on the web. Bookmarking it now. Thanks again.

    ReplyDelete