胸腺替换Th:野外以符合TH:选定
感谢您通过我的帖子去世。我也在Stackoverflow和Google中搜索了我的以下代码:
我的html代码:
<form th:action="@{/surgerywaitinglist/saveToWaitinList}"
th:object="${waitinglistDTO}" method="POST">
.
.
.
<select name="departmentName"
th:field="*{department.departmentName}"
th:with="departmentName = ${waitinglistDTO.department.departmentName}"
class="form-control" id="departmentJS">
<option value="" th:selected="selected"
th:disabled="disabled">select option
</option>
<option th:each="department: ${departments}"
th:value="${department.departmentName}"
th:text="${department.departmentName}">
</option>
</select>
.
.
.
<input type="submit" value="Save" class="btn btn-primary" />
</form>
形成其他帖子,例如这篇文章我发现Th:field and th:选择不工作;实际上,需要用其他东西替换字段。请注意,th:对象拥有另一个对象(来自部门类)...
我的DTO类:
public class WaitinglistDTO {
private Long waitingListId;
@NotBlank(message = "Please enter a procedure")
private String waitingListProcedure;
private String waitingListDiagnosis;
private Long waitingListPatientId;
private Long waitingListSurgeonId;
private Long waitingListDepartmentId;
@DateTimeFormat(iso=ISO.DATE)
private Date waitingListAdditionDate;
@DateTimeFormat(iso=ISO.DATE)
private Date waitingListActualBookingDate;
private Patient patient;
private Surgeon surgeon;
private Department department;
您能帮我弄清楚吗?
许多thanxxxxx :)
... ... 更新:
I appreciate your passing by my post. I have searched here in StackOverFlow and google as well to fix my following code:
My HTML code:
<form th:action="@{/surgerywaitinglist/saveToWaitinList}"
th:object="${waitinglistDTO}" method="POST">
.
.
.
<select name="departmentName"
th:field="*{department.departmentName}"
th:with="departmentName = ${waitinglistDTO.department.departmentName}"
class="form-control" id="departmentJS">
<option value="" th:selected="selected"
th:disabled="disabled">select option
</option>
<option th:each="department: ${departments}"
th:value="${department.departmentName}"
th:text="${department.departmentName}">
</option>
</select>
.
.
.
<input type="submit" value="Save" class="btn btn-primary" />
</form>
form other posts like this post I found out that th:field and th:selected do not work together; in fact, th:field needs to be replace with something else. Notice that the th:object holds another object (from Department class) ...
My DTO class:
public class WaitinglistDTO {
private Long waitingListId;
@NotBlank(message = "Please enter a procedure")
private String waitingListProcedure;
private String waitingListDiagnosis;
private Long waitingListPatientId;
private Long waitingListSurgeonId;
private Long waitingListDepartmentId;
@DateTimeFormat(iso=ISO.DATE)
private Date waitingListAdditionDate;
@DateTimeFormat(iso=ISO.DATE)
private Date waitingListActualBookingDate;
private Patient patient;
private Surgeon surgeon;
private Department department;
Could you help me figure this out?
Many thanxxxxx :)
...
Update:
The following image explains how it should look,,, however, the default option should be the select option which should be somehow disabled
This is the result I get when I apply the suggested code of Rafael da Silva ,, you can see the pre-selected option is the first option rather than the select option option :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以这样做:
这对我有用。
you can do like this:
this worked for me.
我不明白为什么您会将所选的选项添加到空白选项中,如果您的模型对象没有设置,并且如果采取正常行为,则始终选择第一个选项。 应该解决。
如果您希望选择始终以默认选项启动,即使模型具有另一个值或想将其设置为其他方式。您可以设置
th:id =“*{dectiber.departmentName}”
andth:name =“*{dectiber.departmentName}”
,而不是手动handleth :选择
。作为附带说明,无需使用
th
版本th:selected =“ Selected”
如果使用静态值selected
或selected =“选择
在那种情况下会用我没有工作电脑
代码编辑,而不会测试,因为
。想要填写现有数据,但默认为第一个选项
,我不确定使用禁用的数据,但我认为它应该有效,就个人而言,我只能在控制器验证中选择它并在您的bean上执行@notnull。
th:使用
注释我什么都看不到,我可以看到什么th:with
用于定义一个新的变量,适用于嵌套在您定义它的Elment中的所有元素。例子:I don't see why you would add the selected to the blank option, the first option is always selected if your model object has nothing set and if it does normal behaviour would be to have that item selected, which
th:field
should resolve.In case you want the select to always start at the default option, even if the model has another value or want to set it some other way. you can set
th:id="*{department.departmentName}"
andth:name="*{department.departmentName}"
and than manualy handleth:selected
.As a side note theres no need to use the
th
versionth:selected="selected"
if using static values justselected
orselected="selected
would sufice in that caseEdit with code, not tested as i don't have my work pc where i am.
If you want to always have the first option selected even when editing with previous data
If you want to fill with existing data but default to the first option
I'm not 100% sure on the use of disabled but i think it should work, personally i would just leave it selectable and enforce a @notnull on your bean in controller validation.
the
th:with
annotation does nothing asfar i can see, whatth:with
is used for is defining a new variable for all elements nested inside the elment you define it on. example: