簡體   English   中英

使用Thymeleaf創建動態字段時獲取IllegalStateException

[英]Getting IllegalStateException when creating dynamic Fields using Thymeleaf

在我的程序中,我根據獲得的feedbacks數量生成動態表單名稱。 然后,我將輸入satisfactioncommentfeedbackId 它們對於每次迭代都是不同的。 這給了我一個IllegalStateException錯誤。

我的HTML表單是:

<form action="#" th:action="@{/heart2heart/token/__${tokenId}__/}" method="post">               
    <div class="table-responsive">
        <table class="table table-striped table-bordered table-hover table-condensed">
            <tr>
                <th style="display:none;"></th>
                <th th:text="#{service.desc}">Service</th>
                <th th:text="#{feedback.description}">Feedback</th>
                <th th:text="#{visit.date}">Date of Visit</th>
                <th th:text="#{repr.name}">FU Repr</th>
                <th th:text="#{resolution.response}">Response</th>
                <th th:text="#{resolution.satisfactionLevel}">Satisfaction</th>
                <th th:text="#{resolution.comment}">Comment</th>
            </tr>
            <tr th:each="feedback, feedbackStat : *{feedbacks}">
                <td style="display:none;"><input type="hidden" th:field="*{feedbacks[__${feedbackStat.index}__].feedbackId}" th:value="${feedback.id}" /></td>
                <td th:text="${feedback.service.description}">Steel</td>
                <td th:text="${feedback.description}">Problem</td>
                <td th:text="${feedback.visits[0].date}">12/08/2015</td>
                <td th:text="${feedback.visits[0].repr.fullName}">XYZ</td>
                <td th:text="${feedback.receipt.resolutions[0].response}">response</td>
                <td>
                    <div class="radio">
                        <label><input type="radio" th:field="*{feedbacks[__${feedbackStat.index}__].satisfaction}" th:text="#{global.yes}" value="SATISFIED">Yes</input></label>
                    </div>
                    <div class="radio">
                        <label><input type="radio" th:field="*{feedbacks[__${feedbackStat.index}__].satisfaction}" th:text="#{global.no}" value="NOT SATISFIED">No</input></label>
                    </div>
                </td>
                <td><textarea th:field="*{feedbacks[__${feedbackStat.index}__].comment}" class="form-control" rows="2"></textarea></td>
            </tr>
        </table>
        <div class="form-group">
            <button type="submit" name="addRow" th:text="#{button.submit}"
                class="btn btn-primary btn-md">Submit</button>
        </div>
    </div>
</form>

我的控制器是:

@RequestMapping(value = "/{tokenId}/", method = RequestMethod.POST)
public String addSatisfaction(@PathVariable int tokenId, @Valid ReceiptForm receiptForm, BindingResult result, Model model) {
    try {
        for (SatisfactionForm satisfactionForm : receiptForm.getFeedbacks()) {
        Feedback feedback = new Feedback();
        feedback.setId(satisfactionForm.getFeedbackId());
        Feedback feedback1 = heart2heartService.getFeedbackById(feedback);
        Resolution resolution = new Resolution();
        resolution.setId(feedback1.getReceipt().getResolutions().get(0).getId());
        resolution.setSatisfactionLevel(satisfactionForm.getSatisfaction().name());
        resolution.setComment(satisfactionForm.getComment());
        heart2heartService.addSatisfaction(resolution);
        model.addAttribute("success", "Satisfaction added for tokenId " + tokenId);
        }
    } catch (Exception e) {
        logger.error("Exception :: ", e);
    }
    return "success2";
}

@RequestMapping(value = "/{tokenId}/", method = RequestMethod.GET)
public String getSatisfaction(@PathVariable int tokenId, Model model) {
    Token token = new Token();
    token.setId(tokenId);
    try {
        model.addAttribute("feedbacks", heart2heartService.getFeedbacksByToken(token));
    } catch (Exception e) {
        logger.error("Exception :: ", e);
    }
    return "heart2heart/closeFeedback";
}

我的表格是:

public class ReceiptForm {
    private List<SatisfactionForm> feedbacks = new ArrayList<SatisfactionForm>();

    public List<SatisfactionForm> getFeedbacks() {
    return feedbacks;
    }

    public void setFeedbacks(List<SatisfactionForm> feedbacks) {
    this.feedbacks = feedbacks;
    }
}

public class SatisfactionForm {
    public static enum Satisfaction {
        NOT_SATISFIED, SATISFIED
    }

    private String comment;
    private int feedbackId;
    private Satisfaction satisfaction;

    public String getComment() {
    return comment;
    }

    public int getFeedbackId() {
    return feedbackId;
    }

    public Satisfaction getSatisfaction() {
    return satisfaction;
    }

    public void setComment(String comment) {
    this.comment = comment;
    }

    public void setFeedbackId(int feedbackId) {
    this.feedbackId = feedbackId;
    }

    public void setSatisfaction(Satisfaction satisfaction) {
    this.satisfaction = satisfaction;
    }
}

我收到以下錯誤:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'feedbacks[0]' available as request attribute

我該如何解決?

您沒有在Thymeleaf模板中指定表單支持bean。 您正在使用*{}運算符來訪問bean的字段,但是Thymeleaf不知道您要使用哪個bean。 要解決該錯誤,請將th:object添加到表單中。 假設heart2heartService.getFeedbacksByToken(token)返回一個ReceiptForm您的表單將如下所示:

<form action="#" th:action="@{/heart2heart/token/__${tokenId}__/}" method="post" th:object="${feedbacks}">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM