invalid comparison: java.util.ArrayList and java.lang.String——bug解决办法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liuchangjie0112/article/details/80688699

今天碰到个问题,解决了很久才搞定,来记录下,希望可以帮助到大家
一直报错:invalid comparison: java.util.ArrayList and java.lang.String,一直没有考虑是我后来添加的
and ascriptionId != ’ ’ 所带来的问题.
下面错误源码:

这是一个根据list集合的查找数据的 sql,在接收list的时候加了判断 list != ‘ ’ “”,引起了集合与Stirng类型的比较,故报错

<select id="userlistPage" parameterType="page" resultType="pd">
...
<if test="ascriptionId != null and ascriptionId != ''">
        and u.ascriptionId in
        <foreach collection="pd.ascriptionId" index="index" item="item" open="('" separator="','" close="')">${item}</foreach>
...
</if>
  </select>

解决方案: ①将判断条件改为 : list.size >0

<select id="userlistPage" parameterType="page" resultType="pd">
...
<if test="ascriptionId != null and ascriptionId >0 ">
        and u.ascriptionId in
        <foreach collection="pd.ascriptionId" index="index" item="item" open="('" separator="','" close="')">${item}</foreach>
...
</select>

②甚至可以考虑这样:

<select id="userlistPage" parameterType="page" resultType="pd">
...
<if test="ascriptionId != null">
        and u.ascriptionId in
        <foreach collection="pd.ascriptionId" index="index" item="item" open="('" separator="','" close="')">${item}</foreach>
...
</select>

实际上我就是用②的方式。
另外要说明一点#{}和美元符号的用法区别,我这里简单赘述几句,美元符号是原样输入;而#{}是根据传参的类型决定。
我在这个地方就不能用#{},因为page里面没有那个get方法,总是报错: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘__frch_item_0’ in ‘class com.fh.entity.Page’!

java.util.ArrayList cannot be cast to java.lang.String

07-12

小弟初学struts2,很多地方都不明白,遇到了很多问题,麻烦帅哥,美女,解决下!谢谢!rn从数据库里获得list (包括ID(自增),姓名,年龄)rn在strut2写的JSP里用iterator将list遍历输出rnrn下面是Actionrn[code=Java]rnpublic class UserAction extends ActionSupport rn private String name;//姓名rn private int age;//年龄rn private UserDao userdao;rn List list=new ArrayList();//获得数据库里所有的用户集合rn //下面是getter setter 方法rn public UserDao getUserdao() rn return userdao;rn rnrn public void setUserdao(UserDao userdao) rn this.userdao = userdao;rn rnrn public List getList() rn return list;rn rnrn public void setList(List list) rn this.list = list;rn rnrn public String getName() rn return name;rn rnrn public void setName(String name) rn this.name = name;rn rnrn public int getAge() rn return age;rn rnrn public void setAge(int age) rn this.age = age;rn rnrn //JSP页面里调用的action方法,从数据库中获得用户列表(ID(自增),姓名,年龄)rn public List listUser()rn list= userdao.list();rn return getList();rn rnrnrnrn[/code]rnrn这个是JSP页面rn[code=Java]rnrnrn rn rn rn rn rn rnrnrn[/code]rnrnrn运行时,就报错了。下面是错误信息rn[code=Java]rnrnjava.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Stringrn com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)rn com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)rn com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:163)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:249)rn org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:148)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:93)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:128)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:104)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:148)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:128)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)rn org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:468)rn org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)rn org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:76)rnrnrn[/code]rnrnrn请教大虾们,帮帮忙,谢谢!

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭