当前位置: 代码迷 >> J2EE >> 怎样才能将hibernate 实体bean 与 struts actionform 合并?该怎么处理
  详细解决方案

怎样才能将hibernate 实体bean 与 struts actionform 合并?该怎么处理

热度:483   发布时间:2016-04-22 03:22:48.0
怎样才能将hibernate 实体bean 与 struts actionform 合并??
怎样才能将hibernate 实体bean 与 struts actionform 合并??

------解决方案--------------------
合并不了,最简单的就是在actionForm中声明一个实体bean的变量,顺便要进行初始化
public class XxxForm{
private User user;
{
user = new User();
}
......
}
------解决方案--------------------
把POJO类继承下ActionForm好象就好.记得以前是这样做的
在XML里再配置一下..不过这样不好吧.结合的就紧密了
------解决方案--------------------
探讨
把POJO类继承下ActionForm好象就好.记得以前是这样做的
在XML里再配置一下..不过这样不好吧.结合的就紧密了

------解决方案--------------------
探讨
合并不了,最简单的就是在actionForm中声明一个实体bean的变量,顺便要进行初始化
public class XxxForm{
private User user;
{
user = new User();
}
......
}

------解决方案--------------------
学习ing 友情帮顶
------解决方案--------------------
用struts2吧,没有这个问题,如果一定要用struts1的话,也可以做个bean to form ,form to bean的转化工具类,也许还行吧,
我做过不过也许不太好

import java.lang.reflect.Method;
import java.util.Hashtable;
import java.util.Iterator;

import org.apache.struts.action.ActionForm;

/**
 * @author Flying11 E-mail:Flying11@126.com
 * Apr 14, 2008 10:51:52 PM
 */

public class BeanToForm<T extends Object, F extends ActionForm> implements Cloneable{
/**
* the method base on the fact that the bean's all methods is strictly observe java beans agreement
* @param f the source bean
* @param t the objective bean
* @return if they are successfully transformed,return true;else false
*/
private static boolean transform(Object f, Object t) {
try {
// put f to hashtable
Method[] temp = Class.forName(f.getClass().getName()).getMethods();
Hashtable<String, Method> from = new Hashtable<String, Method>();

for (int len = 0; len < temp.length; len++) {
if (temp[len].getName().startsWith("get")
&& temp[len].getParameterTypes().length == 0) {
from.put(temp[len].getName().substring(1), temp[len]);
}
}
// put t to hashtable
temp = Class.forName(t.getClass().getName()).getMethods();
Hashtable<String, Method> to = new Hashtable<String, Method>();
for (int len = 0; len < temp.length; len++) {
if (temp[len].getName().startsWith("set")
&& temp[len].getParameterTypes().length == 1) {
to.put(temp[len].getName().substring(1), temp[len]);
}
}
Iterator<String> it = from.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
if (to.containsKey(key)
&& to.get(key).getParameterTypes()[0].equals(from.get(
key).getReturnType())) {
to.get(key).invoke(t, from.get(key).invoke(f));
}
}
return true;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* the method base on the fact that the bean's all methods is strictly observe sun javabean agreement
* @param <AF> extends org.apache.struts.action.ActionForm
* @param <O> extends java.lang.Object
* @param bean bean to transform
* @param form transformed bean
* @return if they are successfully transformed,return true;else false
*/
public static <AF extends ActionForm, O extends Object> boolean Bean2Form(
O bean, AF form) {
return transform(bean, form);
}
/**
  相关解决方案