weixin_45137110 2021-02-11 00:10 采纳率: 50%
浏览 110
已采纳

控制台可以打印出从数据库查询到的记录,但是从页面获取参数时显示查询不到记录

练习的JavaWeb小案例。用的是JdbcTemplate.

这是servlet:

@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //接收参数并封装对象
        Map<String, String[]> parameterMap = request.getParameterMap();
        Admin loginadmin = new Admin();
        String verifycode = request.getParameter("verifycode");
        try {
            BeanUtils.populate(loginadmin,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //判断验证码
        HttpSession session = request.getSession();
        String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");
        if (checkcode_server.equalsIgnoreCase(verifycode)){
            //验证码正确
            //调用service层
            AdminLoginService adminLoginService = new AdminLoginServiceImpl();
            Admin admin_log = adminLoginService.login(loginadmin);
            if (admin_log != null){
                //登录成功
                response.sendRedirect("/index.jsp");
            }
            else {
                //登录失败
                request.setAttribute("login_error","用户名或密码错误");
                request.getRequestDispatcher("/login.jsp").forward(request,response);
            }
        }else {
            //验证码错误
            request.setAttribute("cc_error","验证码错误");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

这是Dao层:

public class AdminLoginDaoImpl implements AdminLoginDao {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public Admin login(Admin loginAdmin) {
        //查询管理员数据库
       try{
           String sql = "select * from t_admin where admin_name = ? and admin_password = ?";
           Admin admin = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Admin>(Admin.class), loginAdmin.getAdmin_name(), loginAdmin.getAdmin_password());
           return admin;
       }catch (Exception e){
           e.printStackTrace();
           return null;
       }

    }
}

单元测试显示可以查询到数据:

public class test {
    @Test
    public void testAdminLogin(){
        Admin admin = new Admin();
        admin.setAdmin_name("admin");
        admin.setAdmin_password("123456");
        AdminLoginService adminLoginService = new AdminLoginServiceImpl();
        Admin admin1 = adminLoginService.login(admin);
        System.out.println(admin1);
    }



"test.test,testAdminLogin
二月 11, 2021 12:04:36 上午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
Admin{id=1, admin_name='admin', admin_password='123456'}

Process finished with exit code 0

报错信息:

  • 写回答

1条回答 默认 最新

  • 邓国强-cd 2021-02-19 15:55
    关注

    BeanUtils.populate(loginadmin,parameterMap); 写反了,BeanUtils一般src为第一个参数,因该写成BeanUtils.populate(parameterMap,loginadmin);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 如何判断设置快捷键来实现隐藏/显示窗口
  • ¥15 关于#材料工程#的问题:有没有具有电子阻挡层和空穴阻挡层的电池仿真silvaco代码例子或者其他器件具有阻挡层例子的silvaco代码(最好还有相关文献)
  • ¥60 基于MATLAB的TAOD算法
  • ¥15 Groimp使用疑问
  • ¥15 MDK–ARM里一直找不到调试器
  • ¥15 oracle中sql查询问题
  • ¥15 vue使用gojs3.0版本,在nodeDataArray中的iconSrc使用gif本地路径,展示出来后动画是静态的,不是动态的
  • ¥100 代写个MATLAB代码,有偿
  • ¥15 ansys electronics 2021 R1安装报错,错误代码2,如图
  • ¥15 Dev-c++打字不出现中文,但出现日文