jQuery in Action, 2nd edition*
The moose likes Struts and the fly likes Cannot find bean  in any scope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Cannot find bean  in any scope" Watch "Cannot find bean  in any scope" New topic
Author

Cannot find bean in any scope

Raf Beni
Greenhorn

Joined: Aug 22, 2011
Posts: 4
Hi everybody!

I try to do a web with structs! and now i have this problem:

*This is my struct ActionForm bean
public class StudentForm extends org.apache.struts.action.ActionForm {

private List<Student> stuList;
private String rollNoParamInForm ;

public String getRollNoParamInForm() {
return rollNoParamInForm;
}

public void setRollNoParamInForm(String rollNoParamInForm) {
this.rollNoParamInForm = rollNoParamInForm;
}

public List<Student> getStuList() {
return stuList;
}

public void setStuList(List<Student> stuList) {
this.stuList = stuList;
}

}

*and my struct Action:
public class InitDataAction extends org.apache.struts.action.Action {


@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

Connection con = DBUtility.getConnection();
PreparedStatement stm = null;
ResultSet rs = null;
try {
stm = con.prepareStatement("select * from Student");
rs = stm.executeQuery();
List<Student> list = new ArrayList<Student>();
while (rs.next()) {
Student stu = new Student();

stu.setAge(rs.getInt("age"));
stu.setSex(rs.getBoolean("sex"));

stu.setClassName(rs.getString("className"));
stu.setName(rs.getString("sName"));
stu.setRollNo(rs.getString("rollNumber"));

java.util.Date dob= new Date(rs.getDate("dob").getTime());
SimpleDateFormat dateFormat= new SimpleDateFormat("dd/MM/yyyy");
stu.setDob(dateFormat.format(dob));

list.add(stu);
}
StudentForm stuForm= (StudentForm) form;
stuForm.setStuList(list);
} finally {
DBUtility.closeResultSet(rs);
DBUtility.closeStatement(stm);
DBUtility.closeConnection(con);
}

return mapping.findForward("initOk");
}
}

*my jsp to display data :StudentList.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<center>
<h1 style="background-color: gray">Student List</h1>
<table border="1">
<tr>
<th>Name</th>
<th>Class</th>
<th>Birthday</th>
<th>GioiTinh</th>
<th>Sex</th>
<th>Edit</th>
</tr>
<logic:iterate name="StudentForm" property="stuList" id="student">
<tr>
<td><bean:write name="student" property="name" /></td>
<td><bean:write name="student" property="className" /></td>
<td><bean:write name="student" property="dob" /></td>
<td><bean:write name="student" property="gioiTinh" /></td>
<td>
<logic:equal name="student" property="sex"value="true" >Male</logic:equal>
<logic:equal name="student" property="sex"value="false" >Female</logic:equal>
</td>
<td>
<html:link action="PrepareDataForUpdate" paramId="rollNoParamInForm" paramName="student" paramProperty="rollNo">
<bean:write name="student" property="rollNo" />
</html:link>
</td>
</tr>
</logic:iterate>
</table>
<br/>
<span style="background: gray"><html:link action="AddNew">Add new</html:link></span>
<span style="background: gray"><html:link action="BackToHome">Back to home</html:link></span>
</center>
</body>
</html>

*struts-config.xml
<action-mappings>
<action input="/" name="StudentForm" path="/InitDataAction" scope="session" type="abc.InitDataAction">
<forward name="initOk" path="/StudentList.jsp"></forward>
</action>
</action-mappings>


And when run proejct. error:
javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot find bean: "StudentForm" in any scope

Do you have me to solve it??
Orton K Randy
Ranch Hand

Joined: Aug 12, 2011
Posts: 41

Please UseCodeTags.

Is the action class run before you call the JSP? Otherwise, you don't have a StudentForm with you in any scope, let alone session. That is, InitDataAction must be performed before you route to the JSP like you mentioned in the struts-config.xml. If you tried to access the JSP directly in anyway(before the InitDataAction is invoked), you're likely to get this error. What URL do you hit to run the app?


Coderanch, best ever forum on the net. Hands down.
Raf Beni
Greenhorn

Joined: Aug 22, 2011
Posts: 4
Dear Orton K Randy
Sorry, I will use code tag for next time!

How to run action class?? after i create project, i have page "index.jsp " and i write in index.jsp
when i run project, its url is: "http://localhost:8081/NameProject/" and it error "The requested resource (/NameProject/InitDataAction) is not available."
manikandan jayakumar
Ranch Hand

Joined: Aug 20, 2011
Posts: 44



In the above code type represents the InitDataAction.class file under abc.
Orton K Randy
Ranch Hand

Joined: Aug 12, 2011
Posts: 41

I'll make it clear, you don't run the Action class yourself, rather you perform the action that would invoke the action class. So, you should have run it as:
http://localhost:8081/NameProject/InitAction.do
directly
or if you intend to use a JSP(index.jsp in your case) to perform the InitAction, here's how you do it.

index.jsp


In your struts-config.xml(after </form-beans>),
Raf Beni
Greenhorn

Joined: Aug 22, 2011
Posts: 4
Ok! I solved it!
I used a html link and call action to the InitDataAction, and data was be showed!

Thanks for help
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Cannot find bean in any scope