Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change URL in spring mvc

I have a login form like this in 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.

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

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>
like image 505
Hayi Avatar asked Oct 02 '22 14:10

Hayi


1 Answers

Your form should thus have login as its action, and the method implementing this action should redirect to welcome if the login is successful.

@RequestMapping(value = "/login", method = ResquestMethod.POST)
public String handleLogin([...]) {
    ...
    if (successful) {
        return "redirect:/welcome"
    }
}
like image 85
JB Nizet Avatar answered Oct 05 '22 13:10

JB Nizet