File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Newbie Problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Newbie Problems" Watch "Newbie Problems" New topic
Author

Newbie Problems

John Gregory
Ranch Hand

Joined: Oct 05, 2006
Posts: 118
I'm trying to learn struts by using the huested book.
His 1st example, a simple login type application has
about 3 html/jsp pages, and about 4 java classes
(a form, action, helper class and exception). I've
compiled & deployed everything as the book describes,
however, when I enter the information I've entered,
I get this white screen of death. The url has what I
believe the session id is, but I don't get the success
or failure page to show, as I'd expect.

I'm using netbeans5.5 as my IDE. In creating the app,
I selected the struts framework, and loaded the TLD's.
Can anybody offer any suggestions on what I am missing?

Thanks,

John
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Show us your code, including JSPs, Action classes and the struts-config.xml file.


Merrill
Consultant, Sima Solutions
John Gregory
Ranch Hand

Joined: Oct 05, 2006
Posts: 118
Merrill,

Here are the pages you mentioned:

//register.jsp

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="form" %>

<form:form action="register.do">
UserName:<form:text property="username"/><br>
enter password:<form assword property="password1"/><br>
re-enter password:<form assword property="password2"/><br>
<form:submit value="Register"/>
</form:form>
=========================

//success && failure.html -- only difference is in name and message.

<HTML>
<HEAD>
<TITLE>SUCCESS</TITLE>
</HEAD>
<BODY>
Registration succeeded!
<P><A href="register.jsp">try another?</A></P>
</BODY>
</HTML>
=========================
package app;

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

public class RegisterAction extends Action {

public ActionForward perform (ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
HttpServletResponse res) {

// (1) Cast the form to the RegisterForm
RegisterForm rf = (RegisterForm) form;

String username = rf.getUsername();
String password1 = rf.getPassword1();
String password2 = rf.getPassword2();

// (2) Apply business logic
if (password1.equals(password2)) {

try {
// (3) Return ActionForward for success
UserDirectory.getInstance().setUser(username,password1);
return mapping.findForward("success");
} catch (UserDirectoryException e) {
return mapping.findForward("failure");
}

}
// (4) Return ActionForward for failure
return mapping.findForward("failure");
}

}

=========================
package app;
import org.apache.struts.action.*;

public class RegisterForm extends ActionForm {
protected String username;
protected String password1;
protected String password2;

public String getUsername () {return this.username;};
public String getPassword1() {return this.password1;};
public String getPassword2() {return this.password2;};

public void setUsername (String username) {this.username = username;};
public void setPassword1(String password) {this.password1 = password;};
public void setPassword2(String password) {this.password2 = password;};

}
================================
package app;

import java.io.IOException;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.Properties;
import java.net.URL;

public class UserDirectory {

private static final String UserDirectoryFile = "resources/users.properties";
private static final String UserDirectoryHeader = "${user}=${password}";
private static UserDirectory userDirectory = null;
private static Properties p;

private UserDirectory() throws UserDirectoryException {

java.io.InputStream i = null;
p = null;
i = this.getClass().getClassLoader().
getResourceAsStream(UserDirectoryFile);

if (null==i) {
throw new UserDirectoryException();
}
else {
try {
p = new Properties();
p.load(i);
i.close();
}
catch (java.io.IOException e) {
p = null;
System.out.println(e.getMessage());
throw new UserDirectoryException();
}
finally {
i = null;
}
} // end else
}

public static UserDirectory getInstance() throws UserDirectoryException {

if (null==userDirectory) {
userDirectory = new UserDirectory();
}
return userDirectory;

}

public String fixId(String userId) {
return userId.toUpperCase();
}


public boolean isValidPassword(String userId, String password) {
if (null==password) return false;

String _userId = fixId(userId);

if (!isUserExist(_userId)) return false;
return (password.equals(getPassword(_userId)));
}

public boolean isUserExist(String userId) {
if (null==userId) return false;
return !(null==p.getProperty(userId));
}

public String getPassword(String userId) {
return p.getProperty(userId);
}

public Enumeration getUserIds() {
return p.propertyNames();
}

public void setUser(String userId, String password) throws UserDirectoryException {

if ((null==userId) || (null==password)) {
throw new UserDirectoryException();
}

try {
p.put(fixId(userId), password);
String o = this.getClass().getClassLoader().getResource(UserDirectoryFile).getFile();
p.store(new FileOutputStream(o), UserDirectoryHeader);
}
catch (IOException e) {
throw new UserDirectoryException();
}
}
} // end UserDirectory

=============================================
//struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">

<struts-config>

<form-beans>
<form-bean name="registerForm" type="app.RegisterForm"/>
</form-beans>

<action-mappings>
<action path="/register"
type="app.RegisterAction"
name="registerForm">
<forward name="success" path="/success.html"/>
<forward name="failure" path="/failure.html"/>
</action>
</action-mappings>
</struts-config>
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The problem appears to be with the name of the method in your RegisterAction. It should be execute instead of perform. This is something that has changed since earlier versions of Struts. Another thing to watch out for is that in your JSP, the html:form tag should no longer have a name or type attribute. Just specify action and that will be sufficient.

Also, for future reference, when posting code, please highlight the code and press the code button. This will preserve the indents in your code and make it much easier to read.
[ September 01, 2007: Message edited by: Merrill Higginson ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Newbie Problems