*
The moose likes Struts and the fly likes simple login n home(w/ iterate tag) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "simple login n home(w/ iterate tag)" Watch "simple login n home(w/ iterate tag)" New topic
Author

simple login n home(w/ iterate tag)

kel vin
Greenhorn

Joined: Jun 15, 2011
Posts: 2
i'n newbie in struts... n i'm using netbeans ide 7 with strust 1.3.8 and i hoping that anyone can help me with a simple code for login n home with iterate tag
cause i'm stuck with combining this 2 jsp

if i create login only then it will work fine

if i create home(w/ iterate tag) only the it also work fine

but when i combine this 2 jsp i receive this error

Cannot find bean

my code for login.jsp/index.jsp
<%@page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<!DOCTYPE html>
<html>
<head>
<title><bean:message key="login.title"/></title>
</head>
<body>
<html:errors property="login"/>
<html:form action="index.do" focus="userName">
<table align="center">
<tr align="center">
<td><H1><bean:message key="login.message"/></H1></td>
</tr>
<tr align="center">
<td>
<table align="center">
<tr>
<td align="right">
<bean:message key="login.username"/>
</td>
<td align="left">
<html:text property="userName" size="15" maxlength="15" />
</td>
<td>
<html:errors property="userName" />
</td>
</tr>
<tr>
<td align="right">
<bean:message key="login.password"/>
</td>
<td align="left">
<html:password property="password" size="15" maxlength="15" redisplay="false"/>
</td>
<td>
<html:errors property="password" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<html:submit>
<bean:message key="login.button.signon"/>
</html:submit>
</td>
</tr>
</table>
</td>
</tr>
</table>
</html:form>
</body>
</html>



my code for home.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="html" uri="/WEB-INF/struts-html.tld" %>
<%@taglib prefix="bean" uri="/WEB-INF/struts-bean.tld" %>
<%@taglib prefix="logic" uri="/WEB-INF/struts-logic.tld" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Struts Logic Iterate Tag</title>
</head>
<body bgcolor="DDDDDD">
<h1>Struts logic:iterate tag</h1>
<table style="font-weight:bold">
<tr><td>Employee ID</td><td>Employee Name</td></tr>
<logic:iterate id="listMaintenance" name="HomeForm" property="emp">
<tr>
<td><bean:write name="listMaintenance" property="empid"/></td>
<td><bean:write name="listMaintenance" property="empname"/></td>
</tr>
</logic:iterate>
</table>
</body>
</html>


struts-config.xml
<struts-config>
<form-beans>
<form-bean name="HomeForm" type="com.myapp.struts.HomeForm"/>
<form-bean name="IndexForm" type="com.myapp.struts.IndexForm"/>
</form-beans>

<global-exceptions>
</global-exceptions>

<global-forwards>
</global-forwards>

<action-mappings>
<action path="/home" type="com.myapp.struts.HomeAction" name="HomeForm" scope="session" validate="false">
<forward name="success" path="/home.jsp"/>
</action>
<action path="/index" type="com.myapp.struts.IndexAction" name="IndexForm" input="/index.jsp" scope="request" validate="true">
<forward name="failure" path="/index.jsp"/>
<forward name="success" path="/home.jsp"/>
</action>
</action-mappings>

<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>

<message-resources parameter="com/myapp/struts/ApplicationResource"/>

<!-- ========================= Tiles plugin ===============================-->
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
</plug-in>

<!-- ========================= Validator plugin ================================= -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>



my loginAction.java
package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.ResultSet;
import java.util.ArrayList;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class IndexAction extends Action
{
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
throws Exception
{
DBConnection dbc = new DBConnection();
ResultSet rs;
rs = dbc.selectFromDatabase("SELECT username, age, status FROM users WHERE username='" + ((IndexForm)form).getUserName() + "' AND password='" + ((IndexForm)form).getPassword() + "'");
int totalRow = 0;
while(rs.next()){totalRow++;}
if(totalRow > 0)
{
return (mapping.findForward("success"));
}
else
{
ActionErrors errors = new ActionErrors();
ActionError error = new ActionError("error.login.invalid");
errors.add("login",error);
saveErrors(request,errors);
return (mapping.findForward("failure"));
}
}
}


my loginform.java
package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;

public class IndexForm extends ActionForm
{
private String userName;
private String password;

public String getPassword() {return password;}
public String getUserName() {return userName;}
public void setPassword(String string) {password = string;}
public void setUserName(String string) {userName = string;}

public void reset(ActionMapping mapping, HttpServletRequest request)
{
password = "";
userName = "";
}

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if((userName == null) || (userName.length() < 1))
errors.add("userName", new ActionError("error.username.required"));
if((password == null) || (password.length() < 1))
errors.add("password", new ActionError("error.password.required"));
return errors;
}
}



my homeaction.java
package com.myapp.struts;

import java.util.ArrayList;
import javax.servlet.http.*;
import org.apache.struts.action.*;

public class HomeAction extends org.apache.struts.action.Action {
private final static String SUCCESS = "success";
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HomeForm formBean = (HomeForm)form;
ArrayList<ListMaintenance> list=new ArrayList<ListMaintenance>();
list.add(new ListMaintenance("11A0","Jack"));
list.add(new ListMaintenance("11A1","Sam"));
list.add(new ListMaintenance("11A2","Joe"));
list.add(new ListMaintenance("11A3","John"));
formBean.setEmp(list);
return mapping.findForward(SUCCESS);
}
}



my homeform.java
package com.myapp.struts;
import java.util.*;

public class HomeForm extends org.apache.struts.action.ActionForm {
private List<ListMaintenance> emp=new ArrayList<ListMaintenance>();

public List<ListMaintenance> getEmp() {return emp;}
public void setEmp(List<ListMaintenance> emp) {this.emp = emp;}
public HomeForm() {super();}
}



my ListMaintenance.java

package com.myapp.struts;
public class ListMaintenance {
String empid;
String empname;

public String getEmpid() {return empid;}
public void setEmpid(String empid) {this.empid = empid;}
public String getEmpname() {return empname;}
public void setEmpname(String empname) {this.empname = empname;}

public ListMaintenance(String empid, String empname) {
this.empid = empid;
this.empname = empname;
}
}



hope anyone can help me... cause this very urgent.... THANKS in advance
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: simple login n home(w/ iterate tag)
 
Similar Threads
problem in Developing Struts small Apllication
Problem in SessionManagement in Struts
help with login program
error in my struts application no getter methods
how to do validation in struts1