GeeCON Prague 2014*
The moose likes Struts and the fly likes Problem: Cannot find bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Problem: Cannot find bean "messageBean" in any scope" Watch "Problem: Cannot find bean "messageBean" in any scope" New topic
Author

Problem: Cannot find bean "messageBean" in any scope

venu vanga
Greenhorn

Joined: Aug 11, 2006
Posts: 7
Hi all
Iam new to struts and Iam getting the Exception javax.servlet.ServletException: Cannot find bean: "messageBean" in any scope
I think the problem is with struts-config.xml,but i coudn't find out the error.Can anyone Please help me with this?
my files are:
signUp1.jsp-front page
ContactFormBean.java-bean
MessageBean.java-result bean
confirmation.jsp
missing-value.jsp


signUp1.jsp
....
<BODY BGCOLOR="#FDF5E6">
some text.....
<P>
<CENTER>
<%@ taglib uri="http://struts.apache.org/tags-html"
prefix="html" %>
<html:form action="/actions/signup1">
First name: <html:text property="firstName" /><br>
Last name: <html:text property="lastName" /><br>
Email address: <html:text property="email" /><br>
Fax number: <html:text property="faxNumber" /><br>
<html:submit value="Sign Me Up!" />
</html:form>
</center>
</body>
</html>

ContactFormBean.java

package coreservlets;

import org.apache.struts.action.*;

public class ContactFormBean extends ActionForm
{
private String firstName= "First Name";
private String lastName= "Last Name";
private String email= "user @ host";
private String faxNumber= "xxx-yyy-zzzz";

public String getFirstName(){
return(firstName);}

public void setFirstName(String firstName){
this.firstName=firstName;}

public String getLastName(){
return(lastName);}

public void setLastName(String lastName){
this.lastName=lastName;}

public String getEmail(){
return(email);}

public void setEmail(String email){
this.email=email;}

public String getFaxNumber(){
return(faxNumber);}

public void setFaxNumber(String faxNumber){
this.faxNumber=faxNumber;}

private String[] defaultValues={firstName,lastName,email,faxNumber};

public boolean isMissing(String value){
if((value==null)||(value.trim().equals(""))){
return(true);
}
else{
for(int i=0;i<defaultValues.length;i++){
if(value.equals(defaultValues)){
return(true);
}
}
return(false);
}
}
}

[I]MessageBean.java

package coreservlets;

public class MessageBean
{
private String message="";

public String getMessage(){
return(message);
}
public void setMessage(String message){
this.message=message;
}
}

SignupAction1.java
package coreservlets;

import javax.servlet.http.*;
import org.apache.struts.action.*;

public class SignupAction1 extends Action
{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
ContactFormBean userBean=(ContactFormBean)form;
String firstName=userBean.getFirstName();
String lastName=userBean.getLastName();
String email=userBean.getEmail();
String faxNumber=userBean.getFaxNumber();
if(userBean.isMissing(firstName))
{
makeWarning(request,"firstName");
return(mapping.findForward("missing-value"));
}
else if(userBean.isMissing(lastName))
{
makeWarning(request,"lastName");
return(mapping.findForward("missing-value"));
}
else if(userBean.isMissing(email))
{
makeWarning(request,"email");
return(mapping.findForward("missing-value"));
}
else if(userBean.isMissing(faxNumber))
{
makeWarning(request,"faxNumber");
return(mapping.findForward("missing-value"));
}
else
{
return(mapping.findForward("success"));
}
}
protected void makeWarning(HttpServletRequest request,String message)
{
MessageBean messageBean=new MessageBean();
messageBean.setMessage(message);
request.setAttribute("messageBean",messageBean);
}
}

confirmation.jsp
....
<BODY BGCOLOR="#FDF5E6">
<CENTER>
<H1>Confirmation</H1>
Congratulations. You are now signed up for the
Single Provider of Alert Memos network!
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<UL>
<LI>First name:
<bean:write name="contactFormBean" property="firstName"/>
<LI>Last name:
<bean:write name="contactFormBean" property="lastName"/>
<LI>Email address:
<bean:write name="contactFormBean" property="email"/>
<LI>Fax number:
<bean:write name="contactFormBean" property="faxNumber"/>
</UL>
To be removed from the network, send email
<A HREF="mailto:blackhole@spam-network.com">here</A>.
</CENTER>
</BODY></HTML>

missing-value.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<%@ taglib uri="http://struts.apache.org/tags-bean"
prefix="bean" %>
<HEAD><TITLE>Missing
<bean:write name="messageBean" property="message"/>
</TITLE></HEAD>
<BODY BGCOLOR="#FDF5E6">
<CENTER>
<H2>Missing
<bean:write name="messageBean" property="message"/>!
</H2>
Please <A HREF="struts-forms/forms/signup1.jsp">try again</A>.
</CENTER>
</BODY></HTML>

struts-config.xml
<struts-config>
<form-beans>
<form-bean name="contactFormBean"
type="coreservlets.ContactFormBean"/>
<form-bean name="messageBean"
type="coreservlets.MessageBean"/>
</form-beans>
<global-forwards>
<forward name="success"
path="/WEB-INF/results/confirmation.jsp"/>
</global-forwards>
<action-mappings>
<action path="/actions/signup1"
type="coreservlets.SignupAction1"
name="contactFormBean"
scope="request">
<forward name="missing-value"
path="/WEB-INF/results/missing-value.jsp"/>
</action>

</action-mappings>
</struts-config>

please help me......
thanks
poornima balagopal
Ranch Hand

Joined: Dec 02, 2003
Posts: 83
Hi,
If you see your action mapping in the struts config file, you have mapped SignupAction1 class as the action class and contactFormBean as your formbean class for which the forward page is missing-value.jsp. But if you see in missing-value.jsp you have used 'messageBean' as your formbean class for your <bean:write> tag to display the value. When you give the name property as 'messageBean' in this tag it will look for this formbean class mapped for this particular action in struts config file.In your case it is contactFormBean not MessageFormBean.(you can go through the tag library user guide for more clarification) so it will show the error as 'Cannot find bean "messageBean" in any scope '.
When struts gives ActionForm classes to serve this requirment, i think there is no need to use a plain class like your messageFormBean and then set it into the request.
You can solve your problem by combining your contactFormBean class and MessageFormBean class into one you want to use only one FormBean class for the both. In your action class you just need to set the value for MessageFormBean.
 
GeeCON Prague 2014
 
subject: Problem: Cannot find bean "messageBean" in any scope