应该是member没有成功注入,老师帮忙看一下

来源:9-2 项目作业

Java小彩鸡

2021-08-02 21:39:40

相关截图:

freemarker.core.InvalidReferenceException: The following has evaluated to null or missing:

==> evluation.member  [in template "detail.ftl" at line 135, column 53]


----

Tip: It's the step after the last dot that caused this error, not those before it.

----

Tip: If the failing expression is known to legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??

----


----

FTL stack trace ("~" means nesting-related):

- Failed at: ${evluation.member.nickname}  [in template "detail.ftl" at line 135, column 51]

----




相关代码:

@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
public class EvaluationServiceImpl implements EvaluationService {

@Resource
private EvaluationMapper evaluationMapper;

@Resource
private BookMapper bookMapper;

@Resource
private MemberMapper memberMapper;
/**
* 查图书的短评
* @param bookId
* @return
*/

public List<Evaluation> getEvaluationList(Long bookId) {
Book book = bookMapper.selectById(bookId);//得到对应的book,注入到评价实体类

QueryWrapper<Evaluation> queryWrapper = new QueryWrapper<Evaluation>();
//查询符合id,可用评价的评价并按照事件降序查询
queryWrapper.eq("book_id", bookId);
queryWrapper.eq("state", "enable");
queryWrapper.orderByDesc("create_time");
List<Evaluation> list = evaluationMapper.selectList(queryWrapper);
//把查询出来的评价得到会员id这个属性,来查询对应的会员信息,并注入到短评类
for (Evaluation evaluation : list) {
Member member = memberMapper.selectById(evaluation.getMemberId());
evaluation.setMember(member);
evaluation.setBook(book);
}
return list;
}
}


/**
* 图书详情页
* @param bookId
* @return
*/
@GetMapping("/book/{bookId}")
@ResponseBody
public ModelAndView showDetail(@PathVariable("bookId") Long bookId) {
ModelAndView modelAndView = new ModelAndView("/detail");
Book book = bookService.getBook(bookId);
modelAndView.addObject("book", book);
List<Evaluation> evaluationList = evaluationService.getEvaluationList(bookId);
modelAndView.addObject("evaluationList", evaluationList);
return modelAndView;
}



​<div class="reply pl-2 pr-2">
<#list evaluationList as evluation>
<div>
<div>
<span class="pt-1 small text-black-50 mr-2">${evluation.createTime?string('MM-dd')}</span>
<span class="mr-2 small pt-1">${evluation.member.nickname}</span>
<span class="stars mr-2" data-score="${evluation.score}"></span>
<#--${evluation.evaluationId?c}是因为数组大于等于1000会写成1,000
的形式,所以var获得并传递给后台的是String,转化不成Long,
这样写能转化成Long
-->
<button type="button" data-evaluation-id="${evluation.evaluationId?c}"
class="btn btn-success btn-sm text-white float-right" style="margin-top: -3px;">
<img style="width: 24px;margin-top: -5px;" class="mr-1"
src="https://img3.doubanio.com/f/talion/7a0756b3b6e67b59ea88653bc0cfa14f61ff219d/pics/card/ic_like_gray.svg"/>
<span>${evluation.enjoy}</span>
</button>
</div>

<div class="row mt-2 small mb-3">
${evluation.content}
</div>
<hr/>
</div>
</#list>


</div>


写回答

1回答

好帮手慕小尤

2021-08-03

同学你好,测试同学代码是可以实现的,如下所示:

http://img.mukewang.com/climg/6108b4b409f443ae13690325.jpg

同学可以打个断点,查看是否有成功查询到数据与赋值,如下所示:同学也可以与课程源码进行对比。

http://img.mukewang.com/climg/6108b4fa09e391a213250387.jpg

然后重构项目与清除浏览器缓存(Ctrl+F5)重新测试看一下。如下所示:

http://img.mukewang.com/climg/6108b52509c0a22601980138.jpg

注:建议同学在对应的课程中进行提问,便于老师定位课程与项目。

祝学习愉快!

0

0 学习 · 16556 问题

查看课程