Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Struts Question

Thread view: 
p.munsu@gmail.com - 23 Apr 2006 20:04 GMT
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>
Tim B - 24 Apr 2006 03:04 GMT
> Hi,
>
[quoted text clipped - 18 lines]
>     </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/pack
age-summary.html

infonote - 24 Apr 2006 08:18 GMT
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,

> > Hi,
> >
[quoted text clipped - 25 lines]
> 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/pack
age-summary.html
Tim B - 24 Apr 2006 16:18 GMT
> I modified the jsp but still not working.
>
[quoted text clipped - 4 lines]
>
> Thanks in advance,

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.