Java 在 spring mvc 中更改 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21763321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change URL in spring mvc
提问by Youssef
I have a login form like this in URL http://localhost:8080/myproject/login
:
我在 URL 中有一个这样的登录表单http://localhost:8080/myproject/login
:
<form:form method="POST" modelAttribute="auth" action="welcome" id="formlogin">
[...]
</form:form>
and a controller like this:
和这样的控制器:
@RequestMapping(value = "/welcome")
public String welcome([...]) {
[...]
if(logins.size() != 1) {
return "login";
}
[...]
return "welcome";
}
the problem is when the login is incorrect i got this URL http://localhost:8080/myproject/welcome
but i want to get http://localhost:8080/myproject/login
and get welcome
just in case the login is correct.
问题是当登录不正确时,我得到了这个 URL,http://localhost:8080/myproject/welcome
但我想获取http://localhost:8080/myproject/login
并获取welcome
,以防登录正确。
1st UPDATE
第一次更新
In my case the best way to use
在我的情况下,最好的使用方式
return "redirect:/login"
but before i have to add a attribute like this
但在我必须添加这样的属性之前
model.put("errorlogin", true);
To handle this
为了处理这个
<c:if test="${ errorlogin == true }">
<label class="loginerror">Login Error</label>
</c:if>
But the error message doesn't display and instead i got this URL
但是错误消息没有显示,而是我得到了这个 URL
http://localhost:8080/pagesjaunes/login?errorlogin=true
I set a Attribute and I get a Parameter.
我设置了一个属性,我得到了一个参数。
2nd UPDATE
第二次更新
i fixed the problem with this :
我解决了这个问题:
<c:if test="${ param.errorlogin == true }">
<label class="loginerror">Login Error</label>
</c:if>
采纳答案by JB Nizet
Your form should thus have login
as its action, and the method implementing this action should redirect to welcome if the login is successful.
因此,您的表单应具有login
其操作,并且如果登录成功,则实现此操作的方法应重定向到欢迎。
@RequestMapping(value = "/login", method = ResquestMethod.POST)
public String handleLogin([...]) {
...
if (successful) {
return "redirect:/welcome"
}
}