首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

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

2012-01-02 
怎样才能将hibernate 实体bean 与 struts actionform 合并??怎样才能将hibernate 实体bean 与 struts acti

怎样才能将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 tthe 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 beanbean to transform
* @param formtransformed bean
* @returnif 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);
}
/**


* 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 formtransformed bean
* @param beanbean to transform
* @returnif they are successfully transformed,return true;else false
*/
public static <AF extends ActionForm, O extends Object> boolean Form2Bean(
AF form, O bean) {
return transform(form, bean);
}
}

[解决办法]
User要new一下,要不报空指针异常
[解决办法]
可以的,我测试过。
你看看下面的介绍
http://blog.csdn.net/guoquanyou/archive/2008/09/02/2866201.aspx
http://blog.csdn.net/guoquanyou/archive/2008/09/06/2891477.aspx
[解决办法]
要是觉得2个麻烦,一个也完全可以搞定,不过大点的项目有个实体类比较好,form始终属于表现层,把它弄到持久层不符合规范!
[解决办法]
很简单的,你只要记清楚了和数据库打交道的一定是Hibernate自动生成的bean就好了,
actionForm可以是动态的也可以是静态的,如果是静态的那么你别忘了从这个bean中取出
表单值后还要放到Hibernate生成的bean中

热点排行