webwork与orm结合时,参数绑定的方法

WebWork参数绑定
本文介绍如何通过自定义WebWork转换器和拦截器实现从客户端传来的ID参数绑定为持久化对象,以及如何处理列表类型的参数绑定。
问题1.从客户端传来的id,需要转化成持久化对象,webwork默认会为我们创建出空对象,但不满足,希望webwork能按照id值绑定持久化对象.
解决办法:创建自己的转化器
1.创建xwork-conversion.properties,内容如下
[code]com.abest.common.domain.CommonEntity=com.abest.common.EntityWebworkConverter[/code]
其中CommonEntity为所有持久化实体类的超类,更详细的资料见:
[url]http://wiki.opensymphony.com/display/XW/XWork+Conversion[/url]

2.实现自定义转换器
[code]public class EntityWebworkConverter extends WebWorkTypeConverter {

public static EntityWebworkConverterDelegate delegate = null;

public EntityWebworkConverter() {
}

public Object convertFromString(Map map, String[] strings, Class toClass) {
return delegate.convertFromString(map,strings,toClass);
}

public String convertToString(Map map, Object o) {
return delegate.convertToString(map,o);
}
}
[/code]


[code]public class EntityWebworkConverterDelegate extends WebWorkTypeConverter {

private QueryService queryService = null;

public EntityWebworkConverterDelegate(QueryService queryService) {
this.queryService = queryService;
}


public Object convertFromString(Map map, String[] strings, Class toClass) {
if(strings.length > 0)
{
int id = 0;
try {
id = Integer.parseInt(strings[0]);
} catch (NumberFormatException e) {
}
if(id > 0)
{
return queryService.find(toClass,id);
}
}

try {
return toClass.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}

public void setQueryService(QueryService queryService) {
this.queryService = queryService;
}


public String convertToString(Map map, Object o) {
return null;
}
}[/code]

在这里,因为不了解webwork有什么合适的机制对converter的创建进行控制,因此,converter使用静态属性delegate,该属性由ServletContextListener来注入

这样便可完成参数的绑定了,页面传的参数形式,譬如 foo.bar=1即可

问题2:如何绑定到列表
1.使用java5泛型,xwork..-tiger.jar
2.客户端参数形式changedList[0].id=1,changedList[0].name=name,changedList[1].id=2,changedList[1].name=name2
3.预load对象,以使得其它属性能正确的绑定到对象中.处理逻辑为"先过滤出id,对起进行参数绑定,之后,再交由webwork自带的参数拦截器去绑定其它的参数",拦截器代码如下(保留使用原有的params拦截器):

[code]public class AnotherGridEditDataBindInterceptor extends AroundInterceptor {
protected void after(ActionInvocation actionInvocation, String s) throws Exception {

}

protected void before(ActionInvocation actionInvocation) throws Exception {
Object action = actionInvocation.getAction();
Map map = actionInvocation.getInvocationContext().getParameters();
Map entityIdMap = new HashMap();
for(int i = 0;i < 200;i++)
{
String key = "changedList[" + i + "].id";
String[] ids = (String[]) map.get(key);
if(ids != null && ids.length > 0)
{
entityIdMap.put("changedList["+i+"]",ids);
map.remove(key);
}
}
for(int i = 0;i < 200;i++)
{
String key = "newList[" + i + "].id";
String[] ids = (String[]) map.get(key);
if(ids != null && ids.length > 0)
{
entityIdMap.put("newList["+i+"]",ids);
map.remove(key);
}
}
bindParametersToTarget(entityIdMap,action);

}


private void bindParametersToTarget(Map propertiesValue, Object target) {
ActionContext invocationContext = ActionContext.getContext();
OgnlValueStack stack = null;
Object preRoot = null;
try {
invocationContext.put(InstantiatingNullHandler.CREATE_NULL_OBJECTS, Boolean.TRUE);
invocationContext.put(XWorkMethodAccessor.DENY_METHOD_EXECUTION, Boolean.TRUE);
invocationContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);

if (propertiesValue != null) {
stack = ActionContext.getContext().getValueStack();
preRoot = stack.pop();
stack.push(target);

for (Iterator iterator = propertiesValue.entrySet().iterator();
iterator.hasNext();) {
Map.Entry entry = (Map.Entry) iterator.next();
stack.setValue(entry.getKey().toString(), entry.getValue());
}
}
} finally {
invocationContext.put(InstantiatingNullHandler.CREATE_NULL_OBJECTS, Boolean.FALSE);
invocationContext.put(XWorkMethodAccessor.DENY_METHOD_EXECUTION, Boolean.FALSE);
invocationContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.FALSE);
if (preRoot != null) {
stack.pop();
stack.push(preRoot);
}
}
}


}[/code]

其中,changedList表示的是修改过的对象,newList表示的是新增的对象

总结:需要建立Xwork的Converter,Interceptor。servlet的ServletContextListener为converter注入orm的持久对象查找方法;orm提供find(class,id)这样的方法
提供了基于BP(Back Propagation)神经网络结合PID(比例-积分-微分)控制策略的Simulink仿真模型。该模型旨在实现对杨艺所著论文《基于S函数的BP神经网络PID控制器及Simulink仿真》中的理论进行实践验证。在Matlab 2016b环境下开发,经过测试,确保能够正常运行,适合学习和研究神经网络在控制系统中的应用。 特点 集成BP神经网络:模型中集成了BP神经网络用于提升PID控制器的性能,使之能更好地适应复杂控制环境。 PID控制优化:利用神经网络的自学习能力,对传统的PID控制算法进行了智能调整,提高控制精度和稳定性。 S函数应用:展示了如何在Simulink中通过S函数嵌入MATLAB代码,实现BP神经网络的定制化逻辑。 兼容性说明:虽然开发于Matlab 2016b,但理论上兼容后续版本,可能会需要调整少量配置以适配不同版本的Matlab。 使用指南 环境要求:确保你的电脑上安装有Matlab 2016b更高版本。 模型加载: 下载本仓库到本地。 在Matlab中打开.slx文件。 运行仿真: 调整模型参数前,请先熟悉各模块功能和输入输出设置。 运行整个模型,观察控制效果。 参数调整: 用户可以自由调节神经网络的层数、节点数以及PID控制器的参数,探索不同的控制性能。 学习和修改: 通过阅读模型中的注释和查阅相关文献,加深对BP神经网络PID控制结合的理解。 如需修改S函数内的MATLAB代码,建议有一定的MATLAB编程基础。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值