Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Component
- public class BranchParser implements Parser<Branch> {
- /**
- * Convert id as string to Branch entity.
- * In templates use th:value with an id.
- *
- * Example:
- * <option th:each="item : ${items}" th:value="${item.getId}"
- * th:text="${item}">Value</option>
- */
- private final BranchRepo branchRepo;
- public BranchParser(BranchRepo branchRepo) {
- this.branchRepo = branchRepo;
- }
- @Override
- public Branch parse(String id, Locale locale) throws ParseException {
- Branch branch;
- try {
- branch = this.branchRepo.findById(Integer.parseInt(id)).get();
- } catch (NoSuchElementException | NumberFormatException e) {
- throw new ParseException(0, "Branch not found with id: " + id);
- }
- return branch;
- }
- }
- package org.springframework.format;
- @FunctionalInterface
- public interface Parser<T> {
- /**
- * Parse a text String to produce a T.
- * @param text the text string
- * @param locale the current user locale
- * @return an instance of T
- * @throws ParseException when a parse exception occurs in a java.text parsing library
- * @throws IllegalArgumentException when a parse exception occurs
- */
- T parse(String text, Locale locale) throws ParseException;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement