struts2的一个赋值问题
|
ttitfly
2008-05-29
1.页面部分
<a href="${ctx}/admin/user!delete.action?id=${id}">删除</a>
确定这个参数id是有值的。 2.Action方法定义关键部分代码如下:
@SuppressWarnings("unchecked")
public abstract class CRUDAction<T> extends ActionSupport implements Preparable, ModelDriven {
// the model
protected T entity;
/**
* @see ModelDriven
*/
public T getModel() {
return entity;
}
/**
* 每个请求方法执行前都会调用
*/
public void prepare() throws Exception {
if (getId() != null) {
entity = (T) MethodUtils.invokeMethod(getManager(), "get" , new Object[]{entityClass,getId()});
if (entity == null)
throw new IllegalArgumentException("id not exist");
} else
entity = entityClass.newInstance();
onPrepare();
}
/**
* 删除对象.
*/
public String delete() throws Exception {
getManager().removeById((Long)getId());
return SUCCESS;
}
/**
* 从参数中取得id,子类必须实现.
*/
protected abstract Object getId();
}
@SuppressWarnings( { "serial", "unchecked" })
public class UserAction extends CRUDAction<User> {
private Long id;
@Override
protected Long getId() {
// String str[] = (String[])ActionContext.getContext().getParameters().get("id");
// String str1 = (String)ActionContext.getContext().get("id");
return id;//这里被父类CRUDAction回调时放回的为null,不知道是什么原因
}
public void setId(Long id) {
this.id = id;
}
}
问题:getId()方法返回的id为null,getId()方法里注释的2行代码 ,str[]是有值的,str1是没有值的,也就是说没有自动给id属性赋上值,请问这是为什么呢?多谢 |
|
|
kyo100900
2008-05-29
我估计你99%使用的是默认拦截器是配置,即 <interceptor-stack name="defaultStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="debugging"/>
<interceptor-ref name="profiling"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
注意,在这里 prepare 是在 params 前面的,也就是说,你下面的这段代码中的getId()方法肯定得不到值的:
/**
* 每个请求方法执行前都会调用
*/
public void prepare() throws Exception {
if (getId() != null) {
.........
} else {
........
}
}
修改办法很简单,我先给你一个最简单的解决办法:重新为些设置一个新的拦截器是配置,只要将params放在prepare上面即可.
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*</param>
</interceptor-ref>
<interceptor-ref name="prepare"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="debugging"/>
<interceptor-ref name="profiling"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
如果有别的需求,自己调节就行了.
|

