如何处理Java Spring中的所有其他地址
我想处理我没有制作特定功能的所有地址。
例如,
@GetMapping({"/"}) // handles "/"
public String main(Model model) {
return "homepage";
}
@GetMapping({"/admin"}) // handles "/admin"
public String main(Model model) {
return "admin";
}
如何处理所有其他地址,例如“/asdf”等,并将它们全部发送到特定页面?
I want to handle all the address which I didn't make a specific function for.
For example,
@GetMapping({"/"}) // handles "/"
public String main(Model model) {
return "homepage";
}
@GetMapping({"/admin"}) // handles "/admin"
public String main(Model model) {
return "admin";
}
How can I handle all the other address, such as "/asdf" etc and send them all to a specific page?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(5)
这样的事情可以帮助您:
Something like this could help you:
只要确保这是您的最后一个请求图,我记得在Node.js中,它具有奇怪的结果,直到将其放置在同类的底部,因此要安全起见,但是它在Java类中也放在底部。
Just make sure that this is your last RequestMapping, I remember that in node.js it has weird results until it was placed on the bottom of the class, so just to be safe, but it on the bottom as well in your java class.
您可以使用 @路径范围为此。
You can use @PathVariable for this.
另一个选项是编写自定义处理程序,在此处您将在此处使用逻辑,该逻辑将在用户尝试进入不存在的路径时执行。
Another option is to write custom exception handler, where you will place logic that will execute when user tries to go to path that doesn't exist.
是的,您可以使用这样的通配符
,但我认为将其视为404错误更加优雅,然后您可以按照自己的意愿继续
Yes, you can use wildcards like this
but I think it is more elegant to catch it as a 404 error and then you can continue as you like