使用Spring Boot,我的RegisterController
中有几种处理新用户注册的方法。
createNewUser
方法负责将新用户保存到数据库并发送包含具有唯一令牌的链接的确认电子邮件。
confirmUser
方法处理处理确认链接的GET请求。
createNewUser
方法是否有办法将@RequestMapping
值分配给confirmUser
?我想使用此值生成确认链接,而不是对其进行硬编码。
// Process form input data
@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView createNewUser(@Valid User user, BindingResult bindingResult) {
}
// Process confirmation link
// Link in confirmation e-mail will be /registerConfirmation?token=UUID
@RequestMapping(value="/registerConfirmation", method = RequestMethod.GET)
public ModelAndView confirmUser( @RequestParam("token") String token) {
}
答案 0 :(得分:2)
我不知道从@RequestMapping
值获取它的方法,但您有几个不同的选项。
选项1:为映射创建一个常量,并使用它允许您在两种方法中引用它。
private final static String REGISTER_CONF_VAL = "/registerConfirmation";
@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView createNewUser(@Valid User user, BindingResult bindingResult) {
}
// Process confirmation link
// Link in confirmation e-mail will be /registerConfirmation?token=UUID
@RequestMapping(value=REGISTER_CONF_VAL, method = RequestMethod.GET)
public ModelAndView confirmUser( @RequestParam("token") String token) {
}
选项2:不太理想,但如果您将registerConfirmation
添加到配置文件中,则可以像以下一样访问它:
@RequestMapping(value="${register.conf.val}", method = RequestMethod.GET)
public ModelAndView confirmUser( @RequestParam("token") String token) {
}
这不理想的原因是因为您可能不希望它与环境不同。也就是说,它会起作用。
答案 1 :(得分:0)
如果需要根据用户请求生成链接,可以使用控制器中的Path Variable。 U可以获取路径变量并使用某种机制来验证路径。
将registerConfirmation替换为{registerConfirmation},并在方法中使用@PathVariable注释来获取路径。使用该变量检查路径是否有效。