the expression inside th:field gives Neither BindingResult nor plain target object for bean name error

huangapple go评论54阅读模式
英文:

the expression inside th:field gives Neither BindingResult nor plain target object for bean name error

问题

I am trying to create a table that can be saved as a form, but I am encountering an error: "Neither BindingResult nor plain target object for bean name 'rows[0]' available as request attribute." The issue seems to be related to the usage of "rows" inside th:field, but I can't figure out how to fix it.

以下是代码示例:

// 实体类

public class TimeTable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected Integer id;

    String day;

    @Column(name="start_time")
    String startTime;

    @Column(name = "end_time")
    String endTime;
}

// 控制器类

@GetMapping("/timetable/new")
public String newTimetable(Model model) {

    List<TimeTable> rows = new ArrayList<>();
    rows.add(new TimeTable("Monday", "", ""));
    rows.add(new TimeTable("Tuesday", "", ""));
    rows.add(new TimeTable("Wednesday", "", ""));
    rows.add(new TimeTable("Thursday", "", ""));
    rows.add(new TimeTable("Friday", "", ""));
    rows.add(new TimeTable("Saturday", "", ""));
    rows.add(new TimeTable("Sunday", "", ""));
    
    model.addAttribute("rows", rows);
    
    return "timetable/timetable_new";
}

// 表单部分

<form th:action="@{/timetable/save}" method="post">
    <table class="table table-striped table-hover table-responsive-sm align-middle" id="tableRefresh">
        <thead class="text-center">
            <tr>
                <th scope="col">Day</th>
                <th scope="col">Start Time</th>
                <th scope="col">End Time</th>
            </tr>
        </thead>
        <tbody>
            <tr th:each="row, rowStat : ${rows}">
                <td>[[${row.day}]]</td>
                <td>
                    <input type="text" name="startTime" th:field="*{rows[__${rowStat.index}__].startTime}" class="form-control">
                </td>
            </tr>
        </tbody>
    </table>
    <button type="submit" class="form-control btn btn-outline-success">Save</button>
</form>

我正在尝试实现如下图片所示的功能:
the expression inside th:field gives Neither BindingResult nor plain target object for bean name error

出现的错误信息如下:

Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'rows[0]' available as request attribute
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:153)
    at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:926)
    at org.thymeleaf.spring6.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:232)
    at org.thymeleaf.spring6.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:306)
    at org.thymeleaf.spring6.util.FieldUtils.getBindStatus(FieldUtils.java:253)
    at org.thymeleaf.spring6.util.FieldUtils.getBindStatus(FieldUtils.java:227)
    at org.thymeleaf.spring6.processor.AbstractSpringFieldTagProcessor.doProcess(AbstractSpringFieldTagProcessor.java:174)
    at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74)
    ... 116 more
英文:

I am trying to create table that can be saved as a form and I am Neither BindingResult nor plain target object for bean name 'rows[0]' available as request attribute. I can see the problem is from rows used inside th:field but can figure out how to fix it

Below is the code sample

//Entity class
public class TimeTable {
        @Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	protected Integer id;

	String day;
	
	@Column(name=&quot;start_time&quot;)
	String startTime;
	
	@Column(name = &quot;end_time&quot;)
	String endTime;

}
//controller class
	@GetMapping(&quot;/timetable/new&quot;)
	public String newTimetable(Model model) {
		
		List&lt;TimeTable&gt; rows = new ArrayList&lt;&gt;();
		rows.add(new TimeTable(&quot;Monday&quot;, &quot;&quot;, &quot;&quot;));
		rows.add(new TimeTable(&quot;Tuesday&quot;, &quot;&quot;, &quot;&quot;));
		rows.add(new TimeTable(&quot;Wednesday&quot;, &quot;&quot;, &quot;&quot;));
		rows.add(new TimeTable(&quot;Thusday&quot;, &quot;&quot;, &quot;&quot;));
		rows.add(new TimeTable(&quot;Friday&quot;, &quot;&quot;, &quot;&quot;));
		rows.add(new TimeTable(&quot;Saturday&quot;, &quot;&quot;, &quot;&quot;));
		rows.add(new TimeTable(&quot;Sunday&quot;, &quot;&quot;, &quot;&quot;));
		
		//model.addAttribute(&quot;timetableObject&quot;, new TimeTable());
		model.addAttribute(&quot;rows&quot;, rows);
		
		return &quot;timetable/timetable_new&quot;;
	}
&lt;form th:action=&quot;@{/timetable/save}&quot; method=&quot;post&quot; &gt;
				&lt;table class=&quot;table table-striped table-hover table-responsive-sm align-middle&quot; id=&quot;tableRefresh&quot;&gt;
					&lt;thead class=&quot; text-center&quot;&gt;
						&lt;tr&gt;
							&lt;th scope=&quot;col&quot;&gt;Day&lt;/th&gt;
							&lt;th scope=&quot;col&quot;&gt;Start Time&lt;/th&gt;
							&lt;th scope=&quot;col&quot;&gt;End Time&lt;/th&gt;
						&lt;/tr&gt;
					&lt;/thead&gt;
					&lt;tbody&gt;
							&lt;tr th:each=&quot;row, rowStat : ${rows}&quot;&gt;
								&lt;td&gt;[[${row.day}]]&lt;/td&gt;
								&lt;td&gt;
									&lt;input type=&quot;text&quot; name=&quot;startTime&quot;  th:field=&quot;*{rows[__${rowStat.index}__].startTime}&quot; class=&quot;form-control&quot;&gt;
								&lt;/td&gt;
							&lt;/tr&gt;
					&lt;/tbody&gt;
				&lt;/table&gt;
			&lt;button type=&quot;submit&quot; class=&quot;form-control btn btn-outline-success&quot;&gt;Save&lt;/button&gt;
			&lt;/form&gt;

picture of what I am trying to achieve

the expression inside th:field gives Neither BindingResult nor plain target object for bean name error

generated error

Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name &#39;rows[0]&#39; available as request attribute
at org.springframework.web.servlet.support.BindStatus.&lt;init&gt;(BindStatus.java:153)
at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:926)
at org.thymeleaf.spring6.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:232)
at org.thymeleaf.spring6.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:306)
at org.thymeleaf.spring6.util.FieldUtils.getBindStatus(FieldUtils.java:253)
at org.thymeleaf.spring6.util.FieldUtils.getBindStatus(FieldUtils.java:227)
at org.thymeleaf.spring6.processor.AbstractSpringFieldTagProcessor.doProcess(AbstractSpringFieldTagProcessor.java:174)
at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74)
... 116 more

答案1

得分: 0

If you're using th:field, then you need to add a model attribute to your model and set it as the th:object of your <form />. Since you are using a variable named rows, your th:object should look like this:

如果你正在使用th:field,那么你需要在你的模型中添加一个模型属性,并将其设置为你的<form />th:object。由于你正在使用名为rows的变量,你的th:object应该如下所示:

public class TimeTableForm {
    private List<TimeTable> rows;

    public void setRows(List<TimeTable> rows) {
        this.rows = rows;
    }

    public List<TimeTable> getRows() {
        return rows;
    }
}

it must be added to your model in your controller:

它必须在你的控制器中添加到你的模型中:

TimeTableForm form = new TimeTableForm();
form.setRows(rows);
model.addAttribute("timeTableForm", timeTableForm);

and your form must be updated to reflect the new object:

并且你的表单必须更新以反映新的对象:

<form th:action="@{/timetable/save}" th:object="timeTableForm" method="post">

(Note: The code snippets provided are in both Chinese and English as requested.)

英文:

If you're using th:field, then you need to add a model attribute to your model and set it as the th:object of your &lt;form /&gt;. Since you are using a variable named rows, your th:object should look like this:

public class TimeTableForm {
    private List&lt;TimeTable&gt; rows;

    public void setRows(List&lt;TimeTable&gt; rows) {
        this.rows = rows;
    }

    public List&lt;TimeTable&gt; getRows() {
        return rows;
    }
}

it must be added to your model in your controller:

TimeTableForm form = new TimeTableForm();
form.setRows(rows);
model.addAttribute(&quot;timeTableForm&quot;, timeTableForm);

and your form must be updated to reflect the new object:

&lt;form th:action=&quot;@{/timetable/save}&quot; th:object=&quot;timeTableForm&quot; method=&quot;post&quot;&gt;

huangapple
  • 本文由 发表于 2023年5月17日 19:57:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定