Struts Question

P

p.munsu

Hi,

I am making a simple Login application with Struts.

The program is working ok except for validation. In the JSP page if i
do not enter username or password or enter incorrect username/password,
the error messages are not displaying.

I am using Netbeans 5.0
J.Boss
and Struts 1.2.7

Thanks in advance

Here is the relevant code.

LoginForm:


* LoginForm.java

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public final class LoginForm extends ActionForm {
private String userName;
private String password;

public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void reset(ActionMapping mapping, HttpServletRequest request)
{
password = null;
userName = null;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if ((userName==null) || (userName.length() < 1))
errors.add("name",new ActionMessage("error.login.userName"));
if ((password==null) || (password.length() < 1))
errors.add("password", new
ActionMessage("error.login.password"));
return errors;
}

}

LoginAction:

/*
* LoginAction.java
*

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;


public final class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest request, HttpServletResponse
response)
throws Exception {

String userName = ((LoginForm) form).getUserName();
String password = ((LoginForm) form).getPassword();

if (userName!=null && password!=null &&
userName.equals("john") && password.equals("123")) {
HttpSession session = request.getSession();
session.setAttribute("loggedIn", "1");
return mapping.findForward("success");
}
else {
ActionMessages messages = new ActionErrors();
messages.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("invalidUser", "Invalid user name and/or
password."));
saveErrors(request, messages);
return (new ActionForward(mapping.getInput()));
}
}
}

ApplicationResource.properties:

errors.header=<UL>
errors.prefix=<LI>
errors.suffix=</LI>
errors.footer=</UL>
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an byte.
errors.date={0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
errors.cancel=Operation cancelled.
errors.detail={0}
errors.general=The process did not complete. Details should follow.
errors.token=Request could not be completed. Operation is not in
sequence.
welcome.title=Struts Application
welcome.heading=Struts Applications in Netbeans!
welcome.message=It's easy to create Struts applications with NetBeans.

error.login.userName=<li>UserName is required</li>
error.login.password=<li>Password is required</li>
errors.header=<h3>Errors:</h3><UL>
errors.footer=</UL><hr>

and the login JSP:

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@ page import="org.apache.struts.action.Action" %>
<%@ page import="org.apache.struts.action.ActionMessage" %>
<%@ page import="org.apache.struts.action.ActionErrors" %>
<%@ page import="java.util.Iterator" %>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<html:errors/>

Please enter your user name and password
<br>
<form action="login.do" method=POST>
<table>
<tr>
<td>User Name:</td>
<td><input type=text name=userName>
</tr>
<tr>
<td>Password:</td>
<td><input type=password name=password>
</tr>
<tr>
<td colspan=2 align=right><input type=submit
value="Login">
</tr>
</table>

</form>
</body>
</html>
 
T

Tim B

Hi,

I am making a simple Login application with Struts.

The program is working ok except for validation. In the JSP page if i
do not enter username or password or enter incorrect username/password,
the error messages are not displaying.

and the login JSP:

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@ page import="org.apache.struts.action.Action" %>
<%@ page import="org.apache.struts.action.ActionMessage" %>
<%@ page import="org.apache.struts.action.ActionErrors" %>
<%@ page import="java.util.Iterator" %>
<html>
<head>
<title>Login Page</title>
</head>
<body>

<html:errors/>

this should be <html:messages/>

for for further information on displaying errors and/or messages see
http://struts.apache.org/struts-doc-1.2.7/api/org/apache/struts/taglib/html/package-summary.html
 
I

infonote

I modified the jsp but still not working.

Is it possible to provide me a simple login (or any other) example.
Because the tutorials i found on the internet are for struts 1.1.

I cannot find an example for struts 1.2

Thanks in advance,
 
T

Tim B

infonote said:
I modified the jsp but still not working.

Is it possible to provide me a simple login (or any other) example.
Because the tutorials i found on the internet are for struts 1.1.

I cannot find an example for struts 1.2

Thanks in advance,

did you add the import for org.apache.struts.action.ActionMessages?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top