gpt4 book ai didi

java - 使用 Spring Web MVC 的 ModelAndView 返回相同的 View Controller

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:48:14 25 4
gpt4 key购买 nike

我正在使用 Spring Web MVC 和 Hibernate 来开发我的应用程序。

我的 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">

现在,我的 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>

我的 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;

}

在处理提交事件时,在我的 UserValidateFormController 中,我正在检查用户名和密码是否正确..

它工作正常,如果两者都匹配,则重定向到 layout.jsp,这也很好。

但如果用户名或密码不正确,那么我想重定向到相同的 login.jsp 页面并显示相应的错误。

请建议我如何重定向到同一 View Controller 的解决方案..

提前致谢..

最佳答案

最后用下面一行代码解决了这个问题:

return new ModelAndView(new RedirectView(getSuccessView()));

return new ModelAndView(new RedirectView("success.htm");

谢谢...

关于java - 使用 Spring Web MVC 的 ModelAndView 返回相同的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1642784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com