Java 使用 Spring Web MVC 的 ModelAndView 返回相同的视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1642784/
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
Return same view controller using ModelAndView of Spring Web MVC
提问by Nirmal
I am using Spring Web MVC and Hibernate for developing my application.
我正在使用 Spring Web MVC 和 Hibernate 来开发我的应用程序。
My login.jsp page has following code :
我的 login.jsp 页面有以下代码:
<form:form method="post" commandName="User">
User Name :
<form:input path="email"/>
Password :
<form:input path="password"/>
<input type="submit" align="center" value="Execute">
Now, My servlet.xml file has following code :
现在,我的 servlet.xml 文件有以下代码:
<bean name="/uservalidate.htm" class="com.sufalam.mailserver.presentation.web.UserValidateFormController">
<property name="sessionForm" value="true"/>
<property name="commandName" value="User"/>
<property name="commandClass" value="com.sufalam.mailserver.bean.User"/>
<property name="formView" value="login"/>
<property name="successView" value="layout.jsp"/>
<property name="userSecurityProcessor" ref="IUserSecurityProcessor"/>
</bean>
My UserValidateFormController has following code :
我的 UserValidateFormController 有以下代码:
public class UserValidateFormController extends SimpleFormController {
/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());
private IUserSecurityProcessor userSecurityProcessor;
public ModelAndView onSubmit(Object command)
throws ServletException, SufalamException {
ModelAndView mav = new ModelAndView();
Map model = new HashMap();
String username = ((User) command).getEmail();
String password = ((User) command).getPassword();
List userChecking = new ArrayList();
userChecking = userSecurityProcessor.findByAll(username, password, 0.0);
System.out.println("userChecking length = "+userChecking.size());
if (userChecking.size() == 1) {
return new ModelAndView("layout");
//return new ModelAndView(new RedirectView(getSuccessView()));
}
return new ModelAndView("login", model);
}
protected Object formBackingObject(HttpServletRequest request) throws ServletException {
User user = new User();
return user;
}
public void setUserSecurityProcessor(IUserSecurityProcessor userSecurityProcessor) {
this.userSecurityProcessor = userSecurityProcessor;
}
In my UserValidateFormController at the time of handing submit event, i am checking that username and password are correct or not..
在处理提交事件时,在我的 UserValidateFormController 中,我正在检查用户名和密码是否正确..
It's working fine & if both are matching then its redirecting to layout.jsp, that's also fine.
它工作正常,如果两者都匹配,则重定向到 layout.jsp,这也很好。
But if username or password are incorrect then i want to redirect to same login.jsp page and display appropriate error..
但是如果用户名或密码不正确,那么我想重定向到相同的 login.jsp 页面并显示适当的错误..
Please suggest me the solution that what to do redirecting to same view controller..
请建议我如何重定向到同一个视图控制器的解决方案..
Thanks in advance..
提前致谢..
采纳答案by Nirmal
Finally solve this issue with following line of code :
最后使用以下代码行解决此问题:
return new ModelAndView(new RedirectView(getSuccessView()));
or
或者
return new ModelAndView(new RedirectView("success.htm");
Thanks...
谢谢...
回答by vector
... not sure if this is what you're looking for, but is this how I solve your problem:
...不确定这是否是您要找的,但这就是我解决您问题的方式:
else { return new ModelAndView( "login", model ); }
... otherwise I missed something in your question. It seems to me you're pretty far to get stuck like this.
...否则我在你的问题中遗漏了一些东西。在我看来,你要陷入这样的困境还差得很远。
回答by Triqui
I would say all you have to do is populate your model before using it again:
我会说你所要做的就是在再次使用之前填充你的模型:
if (userChecking.size() == 1) {
return new ModelAndView("layout");
//return new ModelAndView(new RedirectView(getSuccessView()));
}
model.put("User", command);
return new ModelAndView("login", model);
回答by juan
If you have an InternalResourceViewResolverconfigured, you can do it like this:
如果您配置了InternalResourceViewResolver,您可以这样做:
return new ModelAndView("redirect:success.htm");
In my opinion, this is clearer.
在我看来,这更清楚。