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

...全文
101 1 打赏 收藏 转发到动态 举报
写回复
用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
内容概要:本文研究了一种基于遗传算法的新型异构分布式系统任务调度算法,旨在解决异构计算环境任务分配与调度的复杂优化问题。通过Matlab代码实现该算法,充分利用遗传算法强大的全局搜索能力和鲁棒性,对任务执行时间、资源利用率、系统负载均衡等关键性能指标进行综合优化,有效提升分布式系统的整体运行效率与稳定性。研究详细阐述了算法的整体架构设计、染色体编码策略、适应度函数构造、选择机制以及交叉与变异等遗传操作的实现细节,并通过大量仿真实验验证了所提出算法相较于传统调度方法在收敛速度、解的质量和调度性能方面的显著优越性。; 适合人群:具备一定编程基础和优化算法理论知识,从事分布式计算、高性能计算、云计算资源调度或智能优化算法研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于高性能计算、云计算、边缘计算等异构计算平台的任务调度优化,提升资源利用效率;②为研究人员提供遗传算法在复杂组合优化问题应用的完整实现案例,深化对智能优化算法设计原理与仿真实践的理解; 阅读建议:建议读者结合提供的Matlab代码深入研读,重点理解适应度函数的设计逻辑与遗传算子的参数调优策略,并可通过更换不同的任务集和系统模型来测试算法的泛化能力与鲁棒性。
参考李林凤等(2025)一文关于农村劳动力人均受教育年限指标的构建与计算方法,整理了国31个省份总体、分性别的农村人均受教育年限数据,具体计算方法如下: 农村劳动力人均受教育年限 = (农村未上过学人数 × 1+小学学历人数 × 6+初学历人数 × 9+高专学历人数 ×12+大专及以上学历人数 × 16)/农村6岁及以上总人口 相关数据:各地区、分性别人均受教育年限数据 一、数据介绍 数据名称:国各省农村人均受教育年限 数据范围:全国31个省份 时间范围:2006-2024年 样本数量:590条 数据来源:《国人口和就业统计年鉴》、《国劳动统计年鉴》 二、数据指标 年份 省份 省份代码 农村人均受教育年限 男性-农村人均受教育年限 女性-农村人均受教育年限 6岁及以上人口 6岁及以上人口_男 6岁及以上人口_女 未上过学人口 未上过学人口_男 未上过学人口_女 小学人口 小学人口_男 小学人口_女 初人口 初人口_男 初人口_女 高人口 高人口_男 高人口_女 大专及以上人口 大专及以上人口_男 大专及以上人口_女 三、参考文献 [1]李林凤,刘杨,杨亦民.种业创新驱动农村产业融合的作用机制与空间分异效应[J].广东财经大学学报,2025,40(6):97-109. [2]徐小阳,李洁,金丽馥.普惠金融对农村教育贫困的纾解效应[J].国农村经济,2020,(9):41-64.

81,111

社区成员

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

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