我目前正在通过一个小型数据库项目使用spring mvc进行自我教育,但我被困在登录验证的初始步骤中。
package Spittles.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import Spittles.services.LoginService;
import SpringForm1.model.Spittler;
public class SpittlerDao implements LoginService {
private DataSource dataSource;
private JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource)
{
this.dataSource=dataSource;
this.jdbcTemplate=new JdbcTemplate(dataSource);
}
public Spittler checkSpittlerLogin(Spittler spittlerLogin)
{
String sql="SELECT * FROM spittler where spittler_email=? and spittler_password=?";
return jdbcTemplate.queryForObject(sql,new ParameterizedRowMapper<Spittler>(){
public Spittler mapRow(ResultSet rs,int rowNum) throws SQLException
{
Spittler spittler = new Spittler();
spittler.setSpittler_name(rs.getString("spittler_name"));
spittler.setSpittler_email(rs.getString("spittler_email"));
spittler.setSpittler_email(rs.getString("spittler_id"));
return spittler;
}
},spittlerLogin.getSpittler_email(),spittlerLogin.getSpittler_password() );
}
}
我的 LoginController 如下。
package SpringForm1.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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 Spittles.dao.SpittlerDao;
import Spittles.services.LoginService;
import SpringForm1.model.Spittler;
@Controller
public class LoginController {
@Autowired
private LoginService loginService;
@RequestMapping(value="/login.html",method=RequestMethod.GET)
public String loginPage(@ModelAttribute("loginData")Spittler spittler)
{
return "login";
}
@RequestMapping(value="/logindo.html",method=RequestMethod.POST)
public String logindo(@ModelAttribute("loginData")Spittler spittler)
{
Spittler spittlerData = loginService.checkSpittlerLogin(spittler);
if(spittlerData!=null)
{
return "home";
}
else
{
return "login";
}
}
}
现在我面临的问题是,当使用输入正确的电子邮件和密码时,我会被重定向到所需的主页。但是如果电子邮件或密码不正确,我的要求是重定向回登录页面,但我得到以下异常。
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0
那么可能是什么问题?我是春天的新手,所以任何建议都会教会我一些新东西。