Mar*_*lic 3 html java spring-mvc thymeleaf spring-boot
从下面的控制器,我将资源值列表传递serviceRequestList到模型中,即绑定到HTML选择器(下拉菜单)。但是,此页面上存在单个对象ServiceRequestDO。我希望选择器默认为当前对象的值,这样在保存更改时,用户不必记住最初的所有值是什么,然后重置它们。有道理吧?是否存在一些Thymeleaf条件,它将某个字段的当前模型对象的值与应默认选择的选择器相匹配?
ServiceRequestController:
@RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
String GetEditDetails(Model model, Principal principal, @PathVariable("id") Long id, @Valid @ModelAttribute(value = "requestModel") RequestModel requestModel, BindingResult bindingResult, RedirectAttributes redirectAttributes)
{
UserDO currentUser = userRepository.findOneByInitialName(principal.getName());
// Redirect to index if user is not a manager
if (currentUser.getRole() != Role.MANAGER) {
return "redirect:/index";
}
ServiceRequestDO service = serviceRequestRepository.findById(id);
model.addAttribute("service", service);
model.addAttribute("currentUser", currentUser);
model.addAttribute("requestModel", new RequestModel());
List<ServiceRequestTypeResource> serviceRequestList = serviceRequestTypeRepository.findAll();
List<SubServiceRequestTypeResource> subServiceRequestTypeList = subServiceRequestTypeRepository.findAll();
List<ServiceLineTypeResource> serviceLineList = serviceLineRepository.findAll();
List<ProductTypeResource> productList = productRepository.findAll();
List<CustomerNameTypeResource> customerNameList = customerNameRepository.findAll();
List<LocalTeamTypeResource> localTeamList = localTeamRepository.findAll();
serviceRequestList.sort(null);
subServiceRequestTypeList.sort(null);
serviceLineList.sort(null);
productList.sort(null);
customerNameList.sort(null);
localTeamList.sort(null);
/* Drop-Down List */
model.addAttribute("serviceRequestList", serviceRequestList);
model.addAttribute("subServiceRequestList", subServiceRequestTypeList);
model.addAttribute("serviceLineList", serviceLineList);
model.addAttribute("productList", productList);
model.addAttribute("customerNameList", customerNameList);
model.addAttribute("localTeamList", localTeamList);
return "edit";
}
Run Code Online (Sandbox Code Playgroud)
HTML:
<tr>
<td>
<label rel="tooltip"
title="" data-placement="bottom" data-html="true">Service Request Type</label>
</td>
<td>
<select th:name="*{serviceRequestType}">
<option th:each="serviceRequestItem : ${serviceRequestList}"
th:value="${serviceRequestItem}"
th:text="${serviceRequestItem}"
th:selected="${service.serviceRequestType}">
</option>
</select>
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
使用上面的模板,选择器仅默认为列表中的底部值,例如,无论当前对象模型的值如何${service.serviceRequestType}。
如果我正确理解了所有内容,那么我会那样做(创建了一个简单的示例):
@GetMapping("/test/{id}")
public String test(Model model, @PathVariable("id") long id) {
//This part represent your serviceRequestRepository.findById(id);
User requestedUser = new User(id, "User " + id);
model.addAttribute("requestedUser", requestedUser);
//List of users represent your serviceRequestTypeRepository.findAll();
List<User> users = new ArrayList<>();
users.add(new User(1, "User 1"));
users.add(new User(2, "User 2"));
users.add(new User(3, "User 3"));
model.addAttribute("users", users);
return "test";
}
Run Code Online (Sandbox Code Playgroud)
您的选择如下所示:
<select th:name="*{requestedUser}">
<option th:each="user : ${users}"
th:value="${user.id}"
th:text="${user.name}"
th:selected="${requestedUser.id == user.id}">
</option>
</select>
Run Code Online (Sandbox Code Playgroud)
如果现在输入http://localhost:8080/test/2,则选择第二个用户:
<option value="1">User 1</option>
<option selected="selected" value="2">User 2</option>
<option value="3">User 3</option>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4578 次 |
| 最近记录: |