Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Spring
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Paul Clapham
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Liutauras Vilda
Saloon Keepers:
Tim Holloway
Carey Brown
Roland Mueller
Piet Souris
Bartenders:
Forum:
Spring
Preselect dropdown using thymeleaf
liz Duapa
Greenhorn
Posts: 2
posted 9 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I have no problem populating the value of my dropdown, the issue is that I cannot preselect the value based on the search I made in the database.
My Thymeleaf:
<form class="form-narrow form-horizontal" method="post" th:action="@{/userdetails/} + *{username}" th:object="${userdetailform}"> <!--/* Show general error message when form contains errors */--> <th:block th:if="${#fields.hasErrors('${userdetailform.*}')}"> <div th:replace="fragments/alert :: alert (type='danger', message='Form contains errors. Please try again.')">Alert</div> </th:block> <fieldset> <div class="form-group" th:classappend="${#fields.hasErrors('status')}? 'has-error'"> <label for="status" class="col-lg-2 control-label">Status</label> <div class="col-lg-10"> <select class="form-control" aria-describedby="status-addon" id="inputstatus" name="status" th:field="*{status}"> <option th:each="enumStatus : ${listStatus}" th:value="${enumStatus.code}" th:text="#{${enumStatus.value}}">Status</option> </select> </div> </div> </fieldset> </form>
My Controller:
private static final String ACCNTSEARCH_MODEL_ACCNTSTATUS_KEY = "listStatus"; private static final String ACCNTDETAIL_MODEL_ATTRIB_KEY = "userdetailform"; @RequestMapping(value = "userdetails/{username}") //to be called. @PreAuthorize("hasRole('ROLE_USER')") public String getAccounts(@PathVariable String username, Model model) { if(!model.containsAttribute(ACCNTDETAIL_MODEL_ATTRIB_KEY)) { Account accountDetail = rsAccountDetailsService.loadAccountByUserName(username); model.addAttribute(ACCNTSEARCH_MODEL_ACCNTSTATUS_KEY, AccountDetailsStatus.values()); model.addAttribute("userdetailform",accountDetail); } return "account/userdetails"; }
liz Duapa
Greenhorn
Posts: 2
posted 9 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Just to add:
I am using Enum also:
public enum AccountDetailsStatus { ACTIVE(0, "status.active"), EXPIRED(2, "status.expired"), LOCKED(3, "status.locked"); private int code; private String value; private final static class BootstrapSingleton { public static final Map<String, AccountDetailsStatus> lookupByValue = new HashMap<String, AccountDetailsStatus>(); public static final Map<Integer, AccountDetailsStatus> lookupByCode = new HashMap<Integer, AccountDetailsStatus>(); } AccountDetailsStatus(int code, String value) { this.code = code; this.value = value; BootstrapSingleton.lookupByValue.put(value, this); BootstrapSingleton.lookupByCode.put(new Integer(code), this); } public int getCode() { return code; } public String getValue() { return value; }
Don't get me started about those stupid
light bulbs
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
SpringMVC form handling, domain objects fields not present form are nullified after commit
checkbox list is not updated to backend when using spring and thymeleaf
Many-to-Many form with checkboxes,no converter java.lang.String to type @javax.persistence.OneToMany
Problem with Spring MVC complex object data binding
Problem configuring Spring WebFlow, Spring Boot + Thymeleaf
More...