gpt4 book ai didi

java - 在 DAO 中获取请求变量

转载 作者:行者123 更新时间:2023-12-01 11:24:40 25 4
gpt4 key购买 nike

我有一个场景,我无法访问 DAO 中操作类中存在的变量。它是一个带有 EJB 的 struts 框架和非常古老的代码,其中 JSP 是用 java 类文件编写的。所以基本上它从 jsp 显示的反向代码来自 JAVA 和 EJB 类文件作为接口(interface)。 Action 类中的这个特定变量是从请求变量中获取的。我想在 DAO 类中使用这个变量。所以我只能想到两个选择。在表单中设置它并在 DAO 中使用该表单并获取值。但它不起作用,因为表单在其使用的方法中被重新初始化。第二次在 session 或请求中设置它们,但在 DAO 中其值为空。

我还能怎样完成这件事。我只想从 DAO 中的操作类访问变量。请建议。谢谢!

最佳答案

使用重载方法

假设你有

public void save(String firstName, String secondName){
JDBC.save(firstName, secondName);
}

但是现在您需要一个新参数!因此,您创建本质上相同的方法并执行此操作

public void save(String firstName, String secondName, String userId){
//this to keep backward compatibility
if(userId == null){
//whatever is in the older method, in this case just the simple save
JDBC.save(firstName, secondName);
} else {
JDBC.save(firstName, secondName, userId);
}
}

public void save(String firstName, String secondName){
save(firstName, secondName, null);
}

关于java - 在 DAO 中获取请求变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30924954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com