首页 新闻 赞助 找找看

BeanUtils怎么没用啊

0
悬赏园豆:40 [已解决问题] 解决于 2018-01-13 14:59

这是我的包结构

 1 package cn.login.service;
 2 
 3 import cn.login.bean.User;
 4 import cn.login.imp.UserImp;
 5 
 6 public class LoginService {
 7     //这是我的业务层
 8     // 调用dao包中的方法
 9     private UserImp dao;
10 
11     public User login(User u) {
12         User existu = dao.denlu1(u.getUsername(), u.getPassword());
13         if (existu == null) {
14             throw new RuntimeException("没有该用户");
15         }
16         if (!existu.getPassword().equals(u.getPassword())) {
17             throw new RuntimeException("密码不正确");
18         }
19 
20         return existu;
21 
22     }
23 }
package cn.login.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import cn.login.bean.User;
import cn.login.service.LoginService;

public class LoginServlet extends HttpServlet {
    private LoginService dao;
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
        
        //1.处理中文乱码这是我的web层
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        //2.封装数据以及对数据进行强转
        User u = new User();        
        try {
            BeanUtils.populate(u, request.getParameterMap());
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //调用service
        User existu = null;
        //调用dao包中的方法
        try {
            existu =dao.login(u);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("error", e.getMessage());
            request.getRequestDispatcher("/index.jsp").forward(request, response);
            return;
        }
        // 4.向session中加入登录标识
        request.getSession().setAttribute("user", existu);
    
    }

}

JDBC操作没错可是Beanutils数据好像没封装进去求大佬解答!

 
问题补充:

报空指针异常

这是我的包结构

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'index.jsp' starting page</title>
13     <meta http-equiv="pragma" content="no-cache">
14     <meta http-equiv="cache-control" content="no-cache">
15     <meta http-equiv="expires" content="0">    
16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17     <meta http-equiv="description" content="This is my page">
18     <!--
19     <link rel="stylesheet" type="text/css" href="styles.css">
20     -->
21   </head>
22   
23   <body>
24   <form action="servlet/LoginServlet" method="post">
25            <p align="center">用 户 名: <input type="text" name="username"></p><br><font color="red" >${requestScope.errors}</font>
26            <p align="center">密 码: <input type="password" name="password"></p><br>
27            <p align="center"><input type="submit" value="登陆"></p><br>
28   </form>
29   <font color="red">${requestScope.user}</font>
30   </body>
31 </html>

这是我的登陆页面

古叶丶扁舟的主页 古叶丶扁舟 | 初学一级 | 园豆:9
提问于:2018-01-05 18:22
< >
分享
最佳答案
0

刚好你,报错的图片。看不见!!!我把你的代码在我的机器上跑了一遍。一切正常!

收获园豆:40
David5201 | 小虾三级 |园豆:534 | 2018-01-06 16:50

我怎么就看不到你的图片呢?

David5201 | 园豆:534 (小虾三级) | 2018-01-06 16:52

<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>这是我用的beanutils的版本

David5201 | 园豆:534 (小虾三级) | 2018-01-06 17:24

@David5201: 我是想用下这个工具写了个简单的登录功能首先是写了个 登录查询操作=>然后给业务层做一些简单的检测=>再就是给servlet调可是beanutils好像没把数据封装到bean对象中我的beanutils的版本是1.8.3 这个47行就是existu =dao.login(u);

古叶丶扁舟 | 园豆:9 (初学一级) | 2018-01-06 17:53

@751638623:  呵呵,这个NullPointerExcetion 是说的dao.  你看看你的类你里面,都没有对dao实例化。

David5201 | 园豆:534 (小虾三级) | 2018-01-06 21:56

@751638623: 

我的变量名字叫Ls,你把Ls 换成Dao就可以了达到你的目标了。

David5201 | 园豆:534 (小虾三级) | 2018-01-06 22:05

@David5201: 抱歉这么晚才回

User existu = dao.denlu1(u.getUsername(), u.getPassword());

existu =dao.login(u);

可能一楼说的对没分装进去

古叶丶扁舟 | 园豆:9 (初学一级) | 2018-01-09 15:43

@751638623: 你的DAO在哪里初始化的?如果没有初始化,他的值是不是NULL

David5201 | 园豆:534 (小虾三级) | 2018-01-09 22:13

@751638623: 一楼回答的不正确。地方都是错误的

David5201 | 园豆:534 (小虾三级) | 2018-01-09 22:14

@David5201: loginservice里调了dao数据库操作啊我把demo发你看下吧 能帮我看看吗 谢谢

古叶丶扁舟 | 园豆:9 (初学一级) | 2018-01-10 00:20

@751638623: 

//调用service User existu = null; //调用dao包中的方法 try { if (dao==null){ dao =new LoginService(); } existu =dao.login(u); } catch (Exception e) { e.printStackTrace(); request.setAttribute("error", e.getMessage()); request.getRequestDispatcher("/index.jsp").forward(request, response); return; }

David5201 | 园豆:534 (小虾三级) | 2018-01-10 09:40

@751638623: 初始化dao就OK了

David5201 | 园豆:534 (小虾三级) | 2018-01-10 09:40

@David5201: 我是找你这样写的还是有错啊,错误就我回你的上一条service 里调dao操作是填参数getusername getpassword 为null

古叶丶扁舟 | 园豆:9 (初学一级) | 2018-01-10 17:18

@751638623: 你把你的程序发到我的箱吧。。。把数据库与发一下。306184384@qq.com  ,或者你加断点调试一下

David5201 | 园豆:534 (小虾三级) | 2018-01-10 22:25

@David5201: 谢谢问题解决了

古叶丶扁舟 | 园豆:9 (初学一级) | 2018-01-13 14:57
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册