我有以下 Controller :
@Controller
@RequestMapping(value = "/admin/comments", name = "AdminCommentsController")
@Slf4j
public class CommentsController {
@Autowired
private PlaceService placeService;
@Autowired
private CommentRepository commentRepository;
@GetMapping("/awaiting-approval")
public String listAwaitingApproval(@PageableDefault(size = 50, page = 0) Pageable pageable, Model model) {
Page<Comment> comments = commentRepository.findByApprovedIsFalseOrApprovedIsNull(pageable);
model.addAttribute("page", comments);
return "admin/comments/awaitingApproval";
}
}
我想通过映射名称来在 View 中构建一个 URL:
<li>
<a th:href="${#mvc.url('AdminCommentsController#listAwaitingApproval')}" href="add.html">
Awaiting approval
</a>
</li>
但我遇到了一个异常(exception):
java.lang.IllegalArgumentException: Mapping mappingName not found: AdminCommentsController#listAwaitingApproval
从类型级别 @RequestMapping 中删除 name
属性将使 CC#listAwaitingApproval
工作,但它不应该与指定名称一起工作吗?
最佳答案
来自documentation :
public abstract String name
Assign a name to this mapping. Supported at the type level as well as at the method level! When used on both levels, a combined name is derived by concatenation with "#" as separator.
关键时刻是在两个级别上使用时
因此,您还应该为 @GetMapping
指定名称,它应该开始工作
关于spring - 通过 @RequestMapping 名称构建的 URL 无法按自定义名称的预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46766797/