struts中的helper类的简单使用问题。 help!!!

firedrose 2006-03-18 02:41:43
按教程上的一个Action中如下的代码:

package app;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.io.*;
public class RegisterAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
HttpServletResponse res) {
// ○1 将form 转型为RegisterForm
RegisterForm rf = (RegisterForm) form;
String username = rf.getUsername();
String password1 = rf.getPassword1();
String password2 = rf.getPassword2();
ActionForward forward = new ActionForward();
// ○2 应用业务逻辑
if (password1.equals(password2)) {
try {
// ○3 如成功,则返回针对success 的ActionForward
UserDirectory.getInstance().setUser(username,password1);
forward = mapping.findForward("success");
} catch (UserDirectoryException e) {
forward = mapping.findForward("failure");
}
} else{
forward = mapping.findForward("failure");
}
// ○4 返回针对failure的ActionForward
return (forward);
}
}



在读到UserDirectory时出错了,显示“不能解析UserDirectory,它不是一种类型。”

可是教材上写“如果两次密码匹配○2 ,我们将用户添加到 UserDirectory 中,并返回与success 对应的ActionForward 。UserDirectory 是一个 helper 类,它记录usernames 和passwords 到一个标准的属性文件之中。”

这个helper类要如何加入,要如何设才可以用呢????

...全文
65 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
cenlmmx 2006-03-18
  • 打赏
  • 举报
回复
你去下载一下源代码,里面就有,大概如下:
import java.io.IOException;
//import java.io.InputStream;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.Properties;

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;

}


/**
* Transform id so that it will match any conventions used by user
* directory. The default implementation forces the id to
* uppercase. Does <b>not</b> expect the userId to be null and
* will throw a NPE if it is.
*
* @exception Throws Null Pointer Exception if userId is null.
*/
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);
p.store(new FileOutputStream(UserDirectoryFile),
UserDirectoryHeader);

}

catch (IOException e) {

throw new UserDirectoryException();

}
}

} // end UserDirectory

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧