Ad

Showing posts with label STRUTS. Show all posts
Showing posts with label STRUTS. Show all posts

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.

Saturday, 22 August 2009

To dynamically populate the dropdrownBox with the values in the List by using <html:optionsCollection> tag in Struts

Most of the time in wepApplications, we need to populate the dropdown box with the values available in the database.Struts provide a <html:optionsCollection> tag for populating the dropdown box with the list.There are two flavours of <html:optionsCollection> tags comes with struts.

Lets says we need to populate the dropdownbox with the city name available in the backendtable.The Value object for the table will look like

public Class Citydetails
{
private String cityName;//To hold the cityname available in the table
private String cityId; //To hold the cityid available in the table
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getCityId() {
return cityId;
}
public void setCityId(String cityId) {
this.cityId = cityId;
}
}

In the action we will construct the list of above bean object from the data available in the table.

//Ge the data from the city table and construct the list
List<Citydetails> cityList =new ArrayList<Citydetails>();
while(//there is a city row in the table)
{
//construct the Citydetails bean object and set cityid and city name from the table
cityList.add(citydetails);//add the bean to the list
}
request.setAttribute("cityList",cityList)//set the list in the request scope
return mapping.findForward("jsp");//forward to Jsp page


we can populate the dropdownbox with the list which is available in any scope by using the <html:optionsCollection> in Jsp as shown below

<html:select property="formbeancitycode">
<html:optionsCollection name="citylist" value="cityID" label="cityName" />
</html:select>

Name - The attribute name which we have set the list in request,session or application scope
value - The bean property value which will be posted to the server on form submit
label - The bean property value which should get populated in the dropdownbox.

The dropdownbox will have all the cityName which are fetched from the table.
once we select the dropdown and submit the form,the cityID of the selected cityName will be posted to the server.

Instead of setting the list in any scope,we can also set the list to a formbean property in the action.Then we can populate the list from the formbean property in the dropdownbox as shown below

<html:select property="formbeancitycode">
<html:optionsCollection property="citylist" value="cityID" label="cityName" />
</html:select>

property - The formbean property name which has the list.


Note: The list which is set in the scope or in the formbean property for populating the dropdown should be the list of Java bean objects.