我建立了一个简单的登录页面,其中使用用户提供的用户名从数据库中获取所需行,并对照用户提供的值检查凭据。
我面临的问题是,如果用户输入数据库中不存在的用户名值,我将遇到此错误
org.springframework.dao.EmptyResultDataAccessException:不正确 结果大小:预期1,实际0
这意味着我的数据库没有包含所需用户名的任何行。
代码:
@Controller
public class LoginController {
@Autowired
UserDao dao;
@RequestMapping(value="/login",method = RequestMethod.POST)
public ModelAndView loginResult(HttpServletRequest req,HttpServletResponse res,RedirectAttributes redir,Model model) {
String uname=req.getParameter("username");
String pwd=req.getParameter("pwd");
String dept = req.getParameter("dept");
LoginInfoAdmin admin = dao.getEmpById(uname);
if(uname.equals(admin.getUsername())&&pwd.equals(admin.getPassword())&&dept.equals(admin.getDept()))
{
req.getSession().setAttribute("uname",admin.getName());
if(dept.equals((String)admin.getDept())){
return new ModelAndView("adminLoginResult", "message", message1);
}
else if(dept.equals((String)admin.getDept())){
return new ModelAndView("EmployeeLoginResult","message",message1);
}
else{
return new ModelAndView("ManagerLoginResut","message",message1);
}
}
else
{
return new ModelAndView("forward:index.jsp","message","Sorry Username Password Error");
}
索引页面:
<body>
<b><span class="heading">LOGIN USER</span></b>
<div class="container">
<form action="login.html" method="Post">
<div class="form_style">
<input type="text" name="username" placeholder="Enter Username"/>
<input type="password" name="pwd" placeholder="Enter password"/>
<select name="dept">
<option>IT</option>
<option>ADMIN</option>
<option>HR</option>
<option>MARKETING</option>
</select>
<input type="Submit" value="submit">
<span class="disp">${message}</span>
</div>
</form>
</div>
</body>
UserDao (其中我使用模板连接到数据库并启动sql)
public class UserDao {
JdbcTemplate template;
public void setTemplate(JdbcTemplate template) {
this.template = template;
}
public LoginInfoAdmin getEmpById(String username){
String sql="select * from ADMINLOGINDETAILS where username=?";
return template.queryForObject(sql, new Object[]{username},new BeanPropertyRowMapper<LoginInfoAdmin>(LoginInfoAdmin.class));
}
}
我的问题是在触发查询后如何检查行是否存在。
答案 0 :(得分:1)
有很多方法可以解决您的问题。一种方法是捕获在最高级别抛出的异常:
body .main {
flex: 1;
background: url("https://i.imgur.com/AsapsWY.png");
background-size: 100% auto;
background-repeat: no-repeat;
width: 100%;
position: relative;
min-width: 600px;
height: 100%;
}
body .main--flower img {
position: absolute;
bottom: 200px;
right: 200px;
max-width: 100px;
}
body .main--bin img {
position: absolute;
top: 400px;
left: 200px;
max-width: 100px;
}
另一种方法是将其向下捕获,如果缺少记录则返回null(然后在顶层检查null):
try {
LoginInfoAdmin admin = dao.getEmpById(uname);
if(uname.equals(admin.getUsername()) && pwd.equals(admin.getPassword()) && dept.equals(admin.getDept()))
{
req.getSession().setAttribute("uname",admin.getName());
if(dept.equals((String)admin.getDept())){
return new ModelAndView("adminLoginResult", "message", message1);
}
else if(dept.equals((String)admin.getDept())){
return new ModelAndView("EmployeeLoginResult","message",message1);
}
else{
return new ModelAndView("ManagerLoginResut","message",message1);
}
}
else
{
return new ModelAndView("forward:index.jsp","message","Sorry Username Password Error");
}
} catch (EmptyResultDataAccessException erdae) {
return new ModelAndView("forward:index.jsp","message","Sorry Username Password Error");
}
编辑:对于您的DAO,最好返回public LoginInfoAdmin getEmpById(String username){
try {
String sql="select * from ADMINLOGINDETAILS where username=?";
return template.queryForObject(sql, new Object[]{username},new BeanPropertyRowMapper<LoginInfoAdmin>(LoginInfoAdmin.class));
} catch (EmptyResultDataAccessException erdae) {
return null;
}
}
。这样,如果没有匹配的记录,您可以返回Optional<LoginInfoAdmin>
。