Hi,
I am new to
Struts. when i try to get collection value from Action Class to my next
jsp page i got that bellow error:
javax.servlet.ServletException: Cannot find bean loginForm in any scope
Any body please help me, i give my code also
Login.jsp <%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ 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">
<html:html locale="true">
<head>
<html:base />
<title><bean:message key="login.title"/></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="-1">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<html:form action="register.do" method="post" focus="login">
<table border="0">
<tr>
<td><bean:message key="login.username"/></td>
<td><html:text property="login"/></td>
</tr>
<tr>
<td><bean:message key="login.password"/></td>
<td><html
assword property="password" /></td>
</tr>
<tr>
<td colspan="2" align="center"><html:submit value="Login"/></td>
</tr>
</table>
</html:form>
</body>
</html:html>
LoginForm.java import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import java.util.*;
public class LoginForm extends ActionForm {
private
String password;
private String login;
private Vector vect;
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public Vector getVect() {
return vect;
}
public void setVect(Vector vect) {
this.vect = vect;
}
}
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
import javax.mail.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.Globals;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.sql.DataSource;
import java.util.Vector;
LoginAction.java public class LoginAction extends Action {
Connection con = null;
Statement st = null;
ResultSet rs = null;
boolean result = false;
Vector vec = new Vector();
boolean user ;
public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
LoginForm lf = (LoginForm)form;
HttpSession session = request.getSession();
String username = lf.getLogin();
String password = lf.getPassword();
DataSource dataSource = (DataSource) servlet.getServletContext().getAttribute(Globals.DATA_SOURCE_KEY);
try{
con = dataSource.getConnection();
st = con.createStatement();
if(username.length()>0 && password.length()>0){
login log = new login(st);
user=log.userChecking(username,password);
if(user)
vec = log.getRecords();
lf.setVect(vec);
return mapping.findForward("success");
}
}catch(Exception e){
System.out.println("Error "+e);
}
return mapping.findForward("failure");
}
}
login.java import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionMapping;
import javax.sql.DataSource;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class login {
ResultSet rs = null;
ActionMapping mapping=null;
Vector vec = new Vector();
boolean user =false;
login(){
}
public login(Statement st){
this.st = st;
}
public boolean userChecking(String username,String password){
try{
String query = "select username,password from tbluser where username='"+username+"' and password='"+password+"'";
rs = st.executeQuery(query);
if(rs.next()){
user = true;
return user;
}
}catch(Exception e){
System.out.println("Error "+e);
}
return user;
}
public Vector getRecords(){
LoginAction la = new LoginAction();
try{
String query1 = "select * from tbldata";
rs = st.executeQuery(query1);
while(rs.next()){
la.setName(rs.getString("name"));
la.setAddress(rs.getString("address"));
la.setPhone(rs.getInt("phoneno"));
la.setDt(rs.getString("date"));
vec.addElement(la);
}
}catch(Exception e){
System.out.println("Error "+e);
}
return vec;
}
}
data.jsp <%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ 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" %>
<%@ taglib uri="/WEB-INF/display.tld" prefix="display" %>
<html:html>
<head>
<title><bean:message key="login.title" />
</title>
</head>
<body>
<table border="1">
<tr>
<th>Name</th>
<th>Address</th>
<th>Phone No</th>
<th>Date</th>
</tr>
<logic:iterate id="result" name="loginForm" property="vect">
<tr>
<td><bean:write name="result" property="name"/></td>
<td><bean:write name="result" property="address"/></td>
<td><bean:write name="result" property="phone"/></td>
<td><bean:write name="result" property="dt"/></td>
</tr>
</logic:iterate>
</table>
</body>
</html:html>
struts-config.xml <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources >
<data-source key="org.apache.struts.action.DATA_SOURCE">
<set-property property="password" value="" />
<set-property property="minCount" value="1" />
<set-property property="maxCount" value="4" />
<set-property property="user" value="" />
<set-property property="driverClass" value="sun.jdbc.odbc.JdbcOdbcDriver" />
<set-property property="description" value="Struts Test" />
<set-property property="url" value="Jdbc
dbc
river={Microsoft Access Driver (*.mdb)};DBQ=C:\Program Files\Apache
Tomcat 4.0\webapps\ROOT\Struts\StrutsDB.mdb" />
<set-property property="readOnly" value="false" />
<set-property property="autoCommit" value="false" />
<set-property property="loginTimeout" value="" />
</data-source>
</data-sources>
<form-beans >
<form-bean name="loginForm" type="LoginForm" />
</form-beans >
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="LoginForm" name="loginForm" path="/register" scope="request" type="LoginAction">
<forward name="success" path="/data.jsp" redirect="true" />
<forward name="failure" path="/Failure.html" redirect="true" />
</action>
</action-mappings>
<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>