gpt4 book ai didi

java - Spring MVC错误验证错误

转载 作者:行者123 更新时间:2023-11-29 04:51:50 25 4
gpt4 key购买 nike

索引.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<body>
<div align="center" style="margin-top:100px;">
<font face="verdana" size="2">
${welcomeMessage} <BR><BR>
${result}
<form:form action="${pageContext.request.contextPath}/login.html" method="POST" modelAttribute="loginForm">
<table>
<tr>
<td colspan="2" align="center">Spring MVC Form Demo - Login</td>
</tr>
<tr>
<td>User Name</td>
<td><form:input path="username" /> <form:errors path="username"></form:errors></td>
</tr>
<tr>
<td>Password</td>
<td><form:password path="password" /> <form:errors path="password"></form:errors></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Login" style="background-color:white;" /></td>
</tr>
</table>
</form:form>
<a href="${pageContext.request.contextPath}/welcome">Register if not already registered</a>
</font>
</div>
</body>
</html>

HelloController.java

package java4s;


import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.annotation.Scope;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java4s.model.Login;

@Controller
public class LoginSuccessController {

@Autowired
EmployeeService emp_service;

@RequestMapping(value = "/login", method=RequestMethod.POST)
public ModelAndView loginvalidateForm(ModelMap model, @ModelAttribute("loginForm") Login login, BindingResult result, HttpSession session) {

if(result.hasErrors()){
model.addAttribute("result", "All Fields are neccessary");
return new ModelAndView("index",model);
}
if(emp_service.validateLogin(login.getUsername(), login.getPassword()))
{
List<Employee> user_info = emp_service.getUserinfo(login.getUsername());
session.setAttribute("session_username", login.getUsername()); //Add value to session variable
model.addAttribute("result", "Login Success");
model.addAttribute("user_info", user_info);
return new ModelAndView("LoginSuccess",model);
}
else
{
model.addAttribute("result", "Login Failure");
return new ModelAndView("index",model);
}
}

}

登录.java

package java4s.model;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class Login {

@NotNull
@Size(min = 3, max = 20)
private String username;

@NotNull
@Size(min = 3, max = 20)
private String password;

public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
}

我试图在登录字段为空时对它们进行验证,但在登录字段为空时索引页上未显示错误。代码中有什么问题?

最佳答案

你必须添加注解@Valid(更多细节参见spring doc):

public ModelAndView loginvalidateForm(ModelMap model, @Valid @ModelAttribute("loginForm") Login login, BindingResult result, HttpSession session) {
....
}

关于java - Spring MVC错误验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35172876/

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