【问题标题】:Getting IllegalStateException when creating dynamic Fields using Thymeleaf使用 Thymeleaf 创建动态字段时出现 IllegalStateException
【发布时间】:2015-06-25 09:18:04
【问题描述】:

在我的程序中,我根据我得到的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

我该如何解决这个问题?

【问题讨论】:

    标签: java html spring spring-mvc thymeleaf


    【解决方案1】:

    您没有在 Thymeleaf 模板中指定表单支持 bean。 您正在使用 *{} 运算符访问 bean 的字段,但 Thymeleaf 不知道 你想使用哪个 bean。要解决该错误,请将th:object 添加到您的表单中。 假设heart2heartService.getFeedbacksByToken(token) 返回ReceiptForm,您的表单将如下所示:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-26
      • 1970-01-01
      • 2021-11-09
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多