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类要如何加入,要如何设才可以用呢????

...全文
66 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
struts 2.3.4.1 最新英文版API =========================================================== 概览 程序包 使用 树 已过时 索引 帮助 上一个 下一个 框架 无框架 所有 ----------------------------------------------------------- Struts 2.3.4.1 API 程序包 程序包 说明 ------------------------------------------------------------ com.opensymphony.xwork2 Main XWork interfaces and classes. com.opensymphony.xwork2.config Configuration core classes. com.opensymphony.xwork2.config.entities Configuration entity classes. com.opensymphony.xwork2.config.impl Configuration implementation classes. com.opensymphony.xwork2.config.providers Configuration provider classes. com.opensymphony.xwork2.conversion com.opensymphony.xwork2.conversion.annotations Type conversion annotations. com.opensymphony.xwork2.conversion.impl com.opensymphony.xwork2.conversion.metadata Type conversion meta data classes. com.opensymphony.xwork2.inject Guice (pronounced "juice"). com.opensymphony.xwork2.inject.util Guice util classes. com.opensymphony.xwork2.interceptor Interceptor classes. com.opensymphony.xwork2.interceptor.annotations Interceptor annotations. com.opensymphony.xwork2.mock XWork specific mock classes. com.opensymphony.xwork2.ognl com.opensymphony.xwork2.ognl.accessor com.opensymphony.xwork2.spring Spring ObjectFactory classes. com.opensymphony.xwork2.spring.interceptor Spring specific interceptor classes. com.opensymphony.xwork2.test com.opensymphony.xwork2.util XWork util classes. com.opensymphony.xwork2.util.classloader com.opensymphony.xwork2.util.finder com.opensymphony.xwork2.util.fs com.opensymphony.xwork2.util.location Classes and utilities used to track location information. com.opensymphony.xwork2.util.logging com.opensymphony.xwork2.util.logging.commons com.opensymphony.xwork2.util.logging.jdk com.opensymphony.xwork2.util.profiling Classes to enable profiling of action execution. com.opensymphony.xwork2.util.reflection com.opensymphony.xwork2.validator XWork validation subsystem. com.opensymphony.xwork2.validator.annotations Validator annotations. com.opensymphony.xwork2.validator.metadata Validator meta data classes. com.opensymphony.xwork2.validator.validators XWork default validator classes. org.apache.struts2 org.apache.struts2.components org.apache.struts2.components.template org.apache.struts2.config Classes for Struts configuration and property handling. org.apache.struts2.dispatcher Classes for action dispatching in Struts (the Controller part of MVC). org.apache.struts2.dispatcher.mapper org.apache.struts2.dispatcher.multipart Classes to help dispatch multipart HTTP requests. org.apache.struts2.dispatcher.ng This package contains a reimagining of the traditional Struts filter dispatchers. org.apache.struts2.dispatcher.ng.filter org.apache.struts2.dispatcher.ng.listener org.apache.struts2.dispatcher.ng.servlet org.apache.struts2.impl org.apache.struts2.interceptor org.apache.struts2.interceptor.debugging org.apache.struts2.interceptor.validation org.apache.struts2.servlet.interceptor org.apache.struts2.util org.apache.struts2.views org.apache.struts2.views.annotations org.apache.struts2.views.freemarker org.apache.struts2.views.freemarker.tags org.apache.struts2.views.jsp Struts's JSP tag library. org.apache.struts2.views.jsp.iterator org.apache.struts2.views.jsp.ui org.apache.struts2.views.util Miscellaneous helper classes for all views. org.apache.struts2.views.velocity Classes for views using Velocity. org.apache.struts2.views.velocity.components org.apache.struts2.views.xslt The new xslt view supports an extensible Java XML adapter framework that makes it easy to customize the XML rendering of objects and to incorporate structured XML text and arbitarary DOM fragments into the output. ------------------------------------------------------------ 概览 程序包 使用 树 已过时 索引 帮助

81,122

社区成员

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

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