TechnoBuzz

A Techno Blog, mainly about Java

First Struts Example

Using WSAD 5.1.1 for the Struts register example.
Note that I had to apply this fix to WSAD due to an error I was having with the EAR files.
In WASD I create a blank struts web project (with struts 1.0.2) and imported the WAR example file.
Some of the files in the WEB-INF to know about:


struts.tld
struts-bean.tld
struts-form.tld
struts-html.tld
struts-logic.tld
struts-template.tld
struts-config.xml
web.xml


Lets start out with the register.jsp file:

<%@ taglib uri=”/WEB-INF/struts-form.tld” prefix=”form” %>
<form:form action=”register.do”>
UserName:

enter password:<form:password property=”password1″/>
re-enter password:

<form:submit value=”Register”/>
</form:form>


The key thing here is that the jsp makes use of the struts form tag library, and submits the data to register.do

Now lets take a look first at the web.xml and then the struts-config.xml file:

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<!DOCTYPE web-app
PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN”
http://java.sun.com/j2ee/dtds/web-app_2_2.dtd”&gt;
</web-app>


The key thing to note here is its using the web-app_2_2.dtd. Now on to the struts config file:


<?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”&gt;
<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>

So, from the config xml file, its using the struts-config_1_0.dtd, specifies a form bean of
app.RegisterForm, specifies an action of app.RegisterAction, and contains two forwards of
success and failure.

So, in the java resources side of the project, we have a resources folder and a package called
app. One class in the app folder is the RegisterAction mentioned above, lets take a look:


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”);
}
}


So in this RegisterAction class (which inherits from the Action class),
we have method perform. On of the parms to the method is the ActionForm
which I’ll show you in just a sec. We cast the ActionForm parm as RegisterForm which
was specified in the struts config file. Lets go to that RegisteForm class now.


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
} // end UserDirectory
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) {
// no null passwords
if (null==password) return false;
// conform userId to uppercase
String _userId = fixId(userId);
// no passwords for non-users
if (!isUserExist(_userId)) return false;
// does password match user’s password
return (password.equals(getPassword(_userId)));
}
public boolean isUserExist(String userId) {
// no null users
if (null==userId) return false;
// if not null, it’s a user
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 {
// no nulls
if ((null==userId) || (null==password)) {
throw new UserDirectoryException();
}
try {
// conform userId to uppercase when stored
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();
}
}
}

August 16, 2004 Posted by | Struts | 2 Comments