Java 从 Spring MVC 中的控制器操作重定向到 JSP 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22097251/
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
Redirect to a JSP file from controller action in Spring MVC
提问by Kleber Mota
In my spring mvc application, I have the following login form:
在我的 spring mvc 应用程序中,我有以下登录表单:
<!--LOGIN FORM-->
<form name="login-form" class="login-form" action="usuario_login.html" method="post">
<!--HEADER-->
<div class="header">
<!--TITLE--><h1>Login Form</h1><!--END TITLE-->
<!--DESCRIPTION--><span>Fill out the form below to login to my super awesome imaginary control panel.</span><!--END DESCRIPTION-->
</div>
<!--END HEADER-->
<!--CONTENT-->
<div class="content">
<!--USERNAME--><input name="username" type="text" class="input username" value="Username" onfocus="this.value=''" /><!--END USERNAME-->
<!--PASSWORD--><input name="password" type="password" class="input password" value="Password" onfocus="this.value=''" /><!--END PASSWORD-->
</div>
<!--END CONTENT-->
<!--FOOTER-->
<div class="footer">
<!--LOGIN BUTTON--><input type="submit" name="submit" value="Login" class="button" /><!--END LOGIN BUTTON-->
</div>
<!--END FOOTER-->
</form>
the page usuario_login.html is configured in the DispatcherServlet in this way:
页面 usuario_login.html 在 DispatcherServlet 中是这样配置的:
@RequestMapping(value="/usuario_login", method=RequestMethod.POST)
public ModelAndView usuario_login(@RequestParam("username") String username, @RequestParam("password") String password) {
UsuarioDAO usuario = new UsuarioDAO(username, password);
if(usuario.getUsuario() == null) {
String message = "oops! não foi possivel efetuar o login. Confira seu login e senha e tente novamente.";
return new ModelAndView("","message",message);
}
else {
this.sessao = new Sessao(usuario.getUsuario());
return new ModelAndView("usuario_start","usuario",usuario.getUsuario());
}
}
My question is: In case of login fail (the condition usuario.getUsuario() == nullhow i can redirect to my file index.jsp, where the login form is placed? I want send a message explaining the situation, to be displayed in this page, too.
我的问题是:如果登录失败(条件usuario.getUsuario() == null我如何重定向到我的文件 index.jsp,登录表单所在的位置?我想发送一条消息来说明情况,以显示在这个页面,也是。
采纳答案by hectorg87
I would seriuosly recommend using Spring Security for this, it is very easy to implement. Take a look at it here: Spring Security
我强烈建议为此使用 Spring Security,它很容易实现。在这里看一下:Spring Security
Still, what you want to do can me done by doing
尽管如此,你想做的事我可以做
new ModelAndView("redirect:YOUR_REQUEST_MAPPING_HERE");
new ModelAndView("redirect:YOUR_REQUEST_MAPPING_HERE");
Also, for the message, I would recommend taking a look at Redirect Attributes
另外,对于该消息,我建议您查看重定向属性
From the link above:
从上面的链接:
@RequestMapping(value = "/accounts", method = RequestMethod.POST)
public String handle(Account account, BindingResult result, RedirectAttributes redirectAttrs) {
if (result.hasErrors()) {
return "accounts/new";
}
// Save account ...
redirectAttrs.addAttribute("id", account.getId()).addFlashAttribute("message", "Account created!");
return "redirect:/accounts/{id}";
}
After the redirect, flash attributes are automatically added to the model of the controller that serves the target URL.
重定向后,flash 属性会自动添加到为目标 URL 提供服务的控制器的模型中。
That means that for the example above, you can show the message in the JSP by doing ${message}, bacause a variable with name message is added to the model.
这意味着对于上面的示例,您可以通过执行 ${message} 在 JSP 中显示消息,因为将名称为 message 的变量添加到模型中。