【问题标题】:How to using th:if tag to check database field using thymeleaf如何使用 th:if 标签使用 thymeleaf 检查数据库字段
【发布时间】:2017-02-03 19:31:04
【问题描述】:

我是 spEL 新手,我想使用“SELECT COUNT(*) FROM Exams;”用JPQL或spEL查询数据库中的记录是否小于6(6); 这就是我所拥有的。我无法真正弄清楚如何去做,因为我无法在控制器中使用 @Query 注释。

 //Problem writing spEL
<div class="form-group" th:if="${exams.count(*) < 6}" >
<form method="post"  th:object="${newExam}" th:action="@{/exams}" class="inline new-item">
    <label class="form-control input-sm"><input type="text" th:field="*{indexNumber}" placeholder="Index Number" autocomplete="off"/></label>
    <select th:field="*{grade}" class="form-control input-sm">
        <option value="" disabled="disabled">[Select Grade]</option>
        <option th:each="grade : ${grades}" th:value="${grade.values}" th:text="${grade.name}">Grade</option>
    </select>
    <label class="form-control input-sm"><input type="text" th:field="*{courseOffered}" placeholder="CourseOffered"  autocomplete="off"/></label>
    <label class="form-control input-sm"><input type="text" th:field="*{examType}" placeholder="ExamType" autocomplete="off"/></label>
    <label class="form-control input-sm"><input type="text" th:field="*{subject}" placeholder="Subject"  autocomplete="off"/></label>
    <label class="form-control input-sm datepicker"><input type="text" th:field="*{gradeYear}" placeholder="ExamYear"  autocomplete="off"/></label>
    <button type="submit" class="btn btn-primary">Add</button>
</form>

@Controller
public class ExamsController {

    @Autowired
    private ExamService examService;

    @Autowired
    private UserService userService;

    @RequestMapping(path = "/cert_prog")
    public String examsList(Model model){
        Iterable<Exams> exams = examService.findAll();
        model.addAttribute("exams", exams);
        model.addAttribute("newExam", new Exams());
        model.addAttribute("grades", Grade.values());
        return "cert_prog";
    }

    @RequestMapping(path = "/mark", method = RequestMethod.POST)
    public String toggleComplete(@RequestParam Long id) {
        Exams exams = examService.findOne(id);
        examService.toggleComplete(id);
        return "redirect:/cert_prog";
    }

    @RequestMapping(path = "/exams", method = RequestMethod.POST)
    public String addTask(@ModelAttribute Exams exams, Principal principal){
        //User user = userService.findByUsername(principal.getName());
        User user = (User)((UsernamePasswordAuthenticationToken)principal).getPrincipal();
        exams.setUser(user);
        //examService.increment(exams);
        examService.save(exams);
        return "redirect:/cert_prog";
    }
}

@服务 公共类 ExamServiceImpl 实现 ExamService {

@Autowired
private ExamsDao examsDao;

@Override
public Iterable<Exams> findAll() {
    return examsDao.findAll();
}

@Override
public Exams findOne(Long id) {
    return examsDao.findOne(id);
}

@Override
public void toggleComplete(Long id) {
    Exams exams = examsDao.findOne(id);
    exams.setComplete(!exams.isComplete());
    examsDao.save(exams);
}

@Override
public void save(Exams exams) {
    examsDao.save(exams);
}

@Override
public void increment(Exams exams) {
    //exams.setCounter(exams.getCounter() + 1);
}

【问题讨论】:

    标签: java jpa spring-boot thymeleaf spring-el


    【解决方案1】:

    您无需再次查询数据库。考试已经是所有考试记录的可迭代集合,不是吗?试试

    <div class="form-group" th:if="${exams.size() < 6}" >
    

    只要Iterable对象的底层类实现了size()方法,thymeleaf应该可以找到并调用它。否则,在将对象添加到模型之前,您需要强制转换为实现 size 的集合。

    【讨论】:

    • 感谢@geneSummons。当我使用该脚本时,在解析 org.xml.sax.SAXParseException 时出现致命错误:与元素类型“div”关联的属性“th:if”的值不能包含“
    • 这有效
    猜你喜欢
    • 2014-08-29
    • 2013-04-07
    • 1970-01-01
    • 2015-06-17
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-28
    相关资源
    最近更新 更多