NutzCN Logo
问答 Controller无法接收两个对象
发布于 3125天前 作者 qq_d6d46f6d 2710 次浏览 复制 上一个帖子 下一个帖子
标签: mvc

前台form截取部分







直播课



录播课



线下课



-----------------------------------------------












以上两个对象处于同一个表单中
----------------------------后台方法
此时仅仅接收LessonResouce是能够接收到的
public Lesson save(HttpServletRequest request,
// @Param("::lesson") Lesson lesson,
@Param("::lessonResource") List reses //
) {
int id = 0;
Lesson lesson = new Lesson();
if (!isEmpty(lesson.getId())) {
id = lesson.getId();
lesson.setDtUpdateTime(new Date());
lesson.setIntUserId(AuthorityHolder.getUser().getId());
lesson.setTxtUserName(AuthorityHolder.getUser().getTxtReName());
if (lesson.getTxtLessonDesc() != null)
lesson.setTxtLessonDesc(lesson.getTxtLessonDesc().replace(""", "'"));
lessonService.Update(lesson);
} else {
lesson.setDtCreateTime(new Date());
lesson.setIntUserId(AuthorityHolder.getUser().getId());
lesson.setTxtUserName(AuthorityHolder.getUser().getTxtReName());
lesson.setTxtStatus("未上架");
if (lesson.getTxtLessonDesc() != null)
lesson.setTxtLessonDesc(lesson.getTxtLessonDesc().replace(""", "'"));
id = lessonService.Save(lesson);

    }
    -------------------------
    问题:
    当**同时**接lesson 跟 lessonResource的时候** 会抛异常**,**所以怎样在一个表单中接收两个对象?**
    异常如下java.lang.ClassCastException: **java.util.ArrayList cannot be cast to java.util.Map**
at org.nutz.mapl.impl.convert.ObjConvertImpl.injectObj(ObjConvertImpl.java:192)
at org.nutz.mapl.impl.convert.ObjConvertImpl.inject(ObjConvertImpl.java:81)

2016-6-3 1:41:49.574 DEBUG [http-apr-8080-exec-7] Found mapping for [POST] path=/lesson/save : LessonController.save(LessonController.java:69)
at org.nutz.mapl.impl.convert.ObjConvertImpl.convert(ObjConvertImpl.java:65)
2016-6-3 1:41:49.574 DEBUG [http-apr-8080-exec-7] Get 'lessonController'<class admin.teachart.controllers.lesson.LessonController>
at org.nutz.mapl.Mapl.maplistToObj(Mapl.java:34)
at org.nutz.mvc.adaptor.injector.ObjectNavlPairInjector.get(ObjectNavlPairInjector.java:53)
at org.nutz.mvc.adaptor.AbstractAdaptor.adapt(AbstractAdaptor.java:256)
at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:28)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:196)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2508)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2497)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

7 回复

前台代码没发上去补一下。

表单中有两个对象:

对象lessonResource

         <div class="item-riwrap">
              <input name="lessonResource[0].txtResName" value="文件名" type="text">
              <input name="lessonResource[0].txtLessonResUrl" value="/resources/upload/52.zip"  type="text">
              <input class="filebtn filebtnr btnDeleteImg" value="删除" onclick="delRes(this)" type="button">
              <div></div>
         </div>
              <div class="item-riwrap">
              <input name="lessonResource[1].txtResName" value="文件名" type="text">
              <input name="lessonResource[1].txtLessonResUrl" value="/resources/upload/53.zip"  type="text">
              <input class="filebtn filebtnr btnDeleteImg" value="删除" onclick="delRes(this)" type="button">
              <div></div>
          </div>



          <form action="<%=path%>/lesson/save" method="post" id="form1">
                <input type="hidden" name="lesson.id" value="${obj.id}">
                <div class="crumbs"><img src="<%=path%>/resources/images/page-icon.png" class="img-ico">
                    <label
                            class="grey">课程管理>新建课程</label>
                </div>

                <div class="statue article-box">
                    <ul class="clearfix">
                        <li class="fl  select"><a style="color: #fff;"
                                                  href="<%=path%>/lesson/save/index?id=${LessonId}">1<label>设置课程</label></a>
                        </li>

                        <c:choose>
                            <c:when test="${obj.id != null}">
                                <li class="fl two"><a style="color: #fff;"
                                                      href="<%=path%>/lessonPeriod/save/index?id=${LessonId}">2<label>设置课次</label></a>
                                </li>
                            </c:when>

                            <c:otherwise>
                                <li class="fl two">2<label>设置课次</label></li>
                            </c:otherwise>
                        </c:choose>

                    </ul>
                </div>

                <div class="article-box">
                    <span class="color-red">* </span> 第一步:选择新建的课程类型
                </div>

                <div class="course-type c_58595b">
                    <input type="hidden" id="txtLessonCategory" name="lesson.txtLessonCategory"
                           value="${obj.txtLessonCategory}" class="mustInput">
                    <ul class="clearfix  selectlesson">
                        <li class="fl">
                            <img src="<%=path%>/resources/images/zbk.png"></br>
                            <span>直播课</span>
                        </li>
                        <li class="fl">
                            <img src="<%=path%>/resources/images/lbk.png"></br>
                            <span>录播课</span>
                        </li>
                        <li class="fl">
                            <img src="<%=path%>/resources/images/xxk.png"></br>
                            <span>线下课</span>
                        </li>
                    </ul>
                </div>

                <div class="article-box mt-40p">
                    第二步:设置课程分类
                </div>
                <div class="article-box c_58595b">
                    <span class="color-red">*</span>课程所属品类:
                    <select class="w200p" name="lesson.txtLessonType1">
                        <option>请选择</option>
                        <option value="音乐"
                                <c:if test='${obj.txtLessonType1=="音乐"}'>selected="selected"</c:if> >音乐
                        </option>
                        <option value="美术"
                                <c:if test='${obj.txtLessonType1=="美术"}'>selected="selected"</c:if> >美术
                        </option>
                        <option value="手工"
                                <c:if test='${obj.txtLessonType1=="手工"}'>selected="selected"</c:if> >手工
                        </option>
                    </select>
                </div>
                <div class="article-box c_58595b">
                    <span class="color-red">*</span>课程最小年龄:
                    <select class="w200p" name="lesson.intRightMinAge" class="mustInput">
                        <option
                                <c:if test="${obj.intRightMinAge=='0'}">selected="selected"</c:if> value="0">请选择
                        </option>
                        <option
                                <c:if test="${obj.intRightMinAge=='3'}">selected="selected"</c:if> value="3">3
                        </option>
                        <option
                                <c:if test="${obj.intRightMinAge=='4'}">selected="selected"</c:if> value="4">4
                        </option>
                        <option
                                <c:if test="${obj.intRightMinAge=='5'}">selected="selected"</c:if> value="5">5
                        </option>
                        <option
                                <c:if test="${obj.intRightMinAge=='6'}">selected="selected"</c:if> value="6">6
                        </option>
                        <option
                                <c:if test="${obj.intRightMinAge=='7'}">selected="selected"</c:if> value="7">7
                        </option>
                        <option
                                <c:if test="${obj.intRightMinAge=='8'}">selected="selected"</c:if> value="8">8
                        </option>
                        <option
                                <c:if test="${obj.intRightMinAge=='9'}">selected="selected"</c:if> value="9">9
                        </option>
                        <option
                                <c:if test="${obj.intRightMinAge=='10'}">selected="selected"</c:if> value="10">10
                        </option>
                    </select>
                    <span class="color-red">*</span>课程最大年龄:
                    <select class="w200p" name="lesson.intRightMaxAge" class="mustInput">
                        <option
                                <c:if test="${obj.intRightMaxAge=='0'}">selected="selected"</c:if> value="0">请选择
                        </option>
                        <option
                                <c:if test="${obj.intRightMaxAge=='5'}">selected="selected"</c:if> value="5">5
                        </option>
                        <option
                                <c:if test="${obj.intRightMaxAge=='6'}">selected="selected"</c:if> value="6">6
                        </option>
                        <option
                                <c:if test="${obj.intRightMaxAge=='7'}">selected="selected"</c:if> value="7">7
                        </option>
                        <option
                                <c:if test="${obj.intRightMaxAge=='8'}">selected="selected"</c:if> value="8">8
                        </option>
                        <option
                                <c:if test="${obj.intRightMaxAge=='9'}">selected="selected"</c:if> value="9">9
                        </option>
                        <option
                                <c:if test="${obj.intRightMaxAge=='10'}">selected="selected"</c:if> value="10">10
                        </option>
                        <option
                                <c:if test="${obj.intRightMaxAge=='11'}">selected="selected"</c:if> value="11">11
                        </option>
                        <option
                                <c:if test="${obj.intRightMaxAge=='12'}">selected="selected"</c:if> value="12">12
                        </option>
                        <option
                                <c:if test="${obj.intRightMaxAge=='13'}">selected="selected"</c:if> value="13">13
                        </option>
                        <option
                                <c:if test="${obj.intRightMaxAge=='14'}">selected="selected"</c:if> value="14">14
                        </option>
                    </select>
                </div>
                <div class="article-box mt-40p">
                    <span class="color-red">*</span>第三步:设置课程介绍
                </div>
                <div class="article-box c_58595b">
                <span>
                    <lable>课程名称:</lable><input type="text" class="w300p" placeholder="请在此填写课程名称"
                                               name="lesson.txtLessonName" value="${obj.txtLessonName }"
                                               class="mustInput">
                </span>
                <span class="margin-left20p">
                    <lable>允许报名人数:</lable><select class="w100p" name="lesson.intPeopleNum">
                    <option
                            <c:if test="${obj.intRightMaxAge=='0'}">selected="selected"</c:if> value="0">不限</option>
                    <option
                            <c:if test="${obj.intRightMaxAge=='2'}">selected="selected"</c:if> value="2">2</option>
                    <option
                            <c:if test="${obj.intRightMaxAge=='3'}">selected="selected"</c:if> value="3">3</option>
                    <option
                            <c:if test="${obj.intRightMaxAge=='4'}">selected="selected"</c:if> value="4">4</option>
                    <option
                            <c:if test="${obj.intRightMaxAge=='5'}">selected="selected"</c:if> value="5">5</option>
                    <option
                            <c:if test="${obj.intRightMaxAge=='6'}">selected="selected"</c:if> value="6">6</option>
                    <option
                            <c:if test="${obj.intRightMaxAge=='7'}">selected="selected"</c:if> value="7">7</option>
                    <option
                            <c:if test="${obj.intRightMaxAge=='8'}">selected="selected"</c:if> value="8">8</option>
                    <option
                            <c:if test="${obj.intRightMaxAge=='9'}">selected="selected"</c:if> value="9">9</option>
                    <option
                            <c:if test="${obj.intRightMaxAge=='10'}">selected="selected"</c:if> value="10">10</option>
                </select>
                </span>
                    <br/>
                    <span>
                        <label>课程简介(用于列表页显示)</label>
                        <textarea name="lesson.txtLessonSynopsisDesc">${obj.txtLessonSynopsisDesc}</textarea>
                    </span>
                </div>
                <div class="article-box c_58595b">
               <span>
                    <lable>课程封面:</lable><span><img src="">图片形式</span>
                </span>
                </div>
                <div class="mt-20p ml-70p">
                    <span>
                        <span class="upload-pictures"><em><a id="btn_txtLessonImg">点击上传图片</a></em></span><br>
                        <span>图片大于480*270支持JPG,JPEG,PNG格式(3M以下)</span>

                        <input type="hidden" id="txtLessonImg" value="${obj.txtLessonImg }" name="lesson.txtLessonImg">

                    </span>
                    <div class="" id="div_txtLessonImg"></div>

                    <div class="article-box c_58595b mt-20p">
                        课程介绍:
                    </div>
                    <div>
                        <div id="summernote">${obj.txtLessonDesc }</div>
                        <input type="hidden" id="txtLessonDesc" name="lesson.txtLessonDesc"
                               value="${obj.txtLessonDesc }">
                    </div>

表单以及lesson

重新发后台代码,用菜单里面的代码块包起来

而且,Lession的前缀应该是“lession.”

@Param("lession.")

如果用@Param("lesson.") 则什么都接受不到、重发后台代码如下

   @At("/save")
    @Ok("jsp:/jsp/lesson/save")

    public Lesson save(HttpServletRequest request,
                       @Param("::lesson.") Lesson lesson,
                       @Param("::lessonResource.") List<LessonResource> reses
    ) {
        int id = 0;

        if (!isEmpty(lesson.getId())) {
            id = lesson.getId();
            lesson.setDtUpdateTime(new Date());
            lesson.setIntUserId(AuthorityHolder.getUser().getId());
            lesson.setTxtUserName(AuthorityHolder.getUser().getTxtReName());
            if (lesson.getTxtLessonDesc() != null)
                lesson.setTxtLessonDesc(lesson.getTxtLessonDesc().replace("\"", "'"));
            lessonService.Update(lesson);
        } else {
            lesson.setDtCreateTime(new Date());
            lesson.setIntUserId(AuthorityHolder.getUser().getId());
            lesson.setTxtUserName(AuthorityHolder.getUser().getTxtReName());
            lesson.setTxtStatus("未上架");
            if (lesson.getTxtLessonDesc() != null)
                lesson.setTxtLessonDesc(lesson.getTxtLessonDesc().replace("\"", "'"));
            id = lessonService.Save(lesson);

        }

//        if(id != 0) {
//            String where = " where intLessonId = " + id;
//            List<LessonResource> resList = lrs.getListBySqlWhere(where);
//            //如果存在这些resources则删除原来的
//            if(!isEmpty(resList)) {
//                for (LessonResource res : resList) {
//                    lrs.del(res.getId());
//                }
//            }
//        }
//        //替换成新的资源包
//        for (LessonResource res : reses) {
//            res.setIntLessonId(id);
//            res.setTxtLessonName(lesson.getTxtLessonName());
//            res.setIntUserid(AuthorityHolder.getUser().getId());
//            res.setTxtUserName(AuthorityHolder.getUser().getTxtReName());
//            res.setDtCreateTime(new Date());
//            lrs.Save(res);
//        }
        if (id != 0) {
            request.setAttribute("error", "操作成功!#/lessonPeriod/save/index?id=" + id);
            lesson = lessonService.getLessonById(id);
            return lesson;
        } else {
            request.setAttribute("error", "操作失败!");
        }
        return null;

    }

上面的代码会抛出啥, 贴一下

我整明白了;lesson下有一个List的list;属性名称是resource
我在前台的input name 用 lesson.resouces[0].name这种形式 、后台用 @Param("::lesson.") Lesson lesson 就可以接到了。
还是看了兽总的例子才明白,多谢

添加回复
请先登陆
回到顶部