在项目中Struts2.0页面中采用枚举常量的使用:
<s:select id="payType2" list="@com.unutrip.commonbiz.enums.OrderPaymentType@values()" listKey="code" listValue="description"></s:select>
OrderPaymentType一个枚举对象:
由于多个地方应用时:
public enum OrderPaymentType {
CREDITCARD(0, "信用卡"), ONLINE(1, "网上支付"), // ->网银支付给环游
UNSCARD(2, "商联卡POS"), // ->包含在环游商联卡里面
CASH(3, "现金"), CHECK(4, "支票"), BANKPOS(5, "银行卡POS"), TRANSLATE(6, "银行转帐"), VOICE(7, "商联卡语音");
private int code;
private String description;
public static final List<OrderPaymentType> FRONT_VALUE = Arrays.asList(OrderPaymentType.values()).subList(0, 4);
public static final List<OrderPaymentType> SETTLE_VALUE = Arrays.asList(CREDITCARD, ONLINE);
public static final List<OrderPaymentType> BACK_VALUE = Arrays.asList(CASH, CHECK, CREDITCARD, VOICE, ONLINE);
private OrderPaymentType(int code, String description) {
this.code = code;
this.description = description;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public static OrderPaymentType fromCode(int code) {
return EnumUtils.fromEnumProperty(OrderPaymentType.class, "code", code);
}
}
今天研究struts2的标签功能,在用select的时候,
代码如采用部分是:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
<s:select id="payType2" list="@com.unutrip.commonbiz.enums.OrderPaymentType@SETTLE_VALUES" listKey="code" listValue="description"></s:select>
出现了如下异常:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
org.apache.jasper.JasperException: tag
'
checkboxlist
'
, field
'
list
'
, name
'
selectRoleId
'
: The requested list key
'
roles
'
could not be resolved as a collection
/
array
/
map
/
enumeration
/
iterator type. Example: people or people.{name}
-
[unknown location]
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:
541
)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:
435
)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:
320
)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:
266
)
javax.servlet.http.HttpServlet.service(HttpServlet.java:
803
)
org.apache.struts2.dispatcher.ServletDispatcherResult.doExecute(ServletDispatcherResult.java:
154
)
org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:
186
)
com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:
361
)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
265
)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:
249
)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:
68
)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:
87
)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236
)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:
122
)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236
)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:
195
)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:
87
)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236
)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:
195
)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:
87
)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236
)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:
148
)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236
)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:
93
)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236
)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:
235
)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236
)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:
89
)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236
)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:
128
)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236
)
org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:
104
)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236
)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:
267
)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236
)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:
126
)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236
)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:
138
)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:
87
)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236
)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:
148
)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236
)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:
164
)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236
)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:
128
)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236
)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:
176
)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236
)
org.apache.struts2.interceptor.TokenInterceptor.handleValidToken(TokenInterceptor.java:
178
)
org.apache.struts2.interceptor.TokenInterceptor.doIntercept(TokenInterceptor.java:
144
)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:
87
)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:
236
)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:
52
)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:
468
)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:
77
)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:
76
)
在网上Google一把网上说:
出现这个错误的原因可能如下:
1 刚进入该界面的时候发生错误,原因是 list="roles"中的这个集合是空的,导致错误
解决办法很简单,不能让list为空
2 刚进入该界面的时候list是有数据的,当点击提交等按钮的时候,数据被提交到后台,如果配置了验证框架或者在action中写了validate方法,校验没有通过,未走action,直接返回了input,又指定回了当前界面。
此时的checkboxlist中的list丢失了,导致了如上错误(这个错误提示的不太友好,让人认为是类转换错误)
解决办法是把初始化list的工作放到prepare拦截器中,因为prepare是在validate拦截器之前执行,即实现prepareble接口:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
public
class
RoleAction
extends
ActionSupport
implements
Preparable{
@Override
public
void
prepare()
throws
Exception {
//
初始化list
}
}
本人代碼不可能出现这样的错误,我很郁闷,继续探索中....
希望有人知道的告诉一下,谢谢!!
终于发现在struts2.16之前,struts2.0支持OGNL直接访问静态常量,之后不再支持OGNL直接访问静态常量,仅仅支持访问静态方法。问题终于解决。