Spring - 如何将JSP表单中的值与db中的值进行比较? (服务+ DAO)

时间:2016-01-22 00:06:29

标签: java spring jsp spring-mvc

我正在制作一个Web应用程序(虚拟诊所)我为此制作了DAO和服务层(我是初学者),它在Controller中工作正常,但我不知道如何比较{{1在db中的值(登录,密码),我希望,如果输入的值在数据库中,应用程序将重定向到Home.jsp,如果没有 - 那么app将重定向到不同的jsp。有人可以告诉我,我该怎么做呢?

这是一段代码:

的LoginController:

@ModelAttribute("user") User user

的Login.jsp:

@RequestMapping(value="/home.html", method = RequestMethod.POST)
public ModelAndView homePagePost(@ModelAttribute("user") User user)
{   
    setAppContext();         
    clinicService.checkAuthentication(user);     

    ModelAndView home = new ModelAndView("Home");
    return home;
}

在UserDAOImpl:

<form action="/VirtualClinic/home.html" method="post">
  <input type="text" name="login" placeholder="Login"/>
  <input type="password" name="password" placeholder="Password"/>
  <button>Login</button>
</form>

ClinicServiceImpl:

public void checkAuthentication(User user) {
    String query = "SELECT login, password FROM virtualclinic.user WHERE login=? AND password=?";
    Connection con = null;
    PreparedStatement ps = null;
    try{
        con = dataSource.getConnection();
        ps = con.prepareStatement(query);
        ps.setString(1, user.getLogin());
        ps.setString(2, user.getPassword());
        ResultSet out = ps.executeQuery();

    }catch(SQLException e){
        e.printStackTrace();
    }finally{
        try {
            ps.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }// TODO Auto-generated met

}

2 个答案:

答案 0 :(得分:0)

区分两个bean,这是一个实现:

public static List<ChangeItem> getChangeItems(Object oldObj, Object newObj) {
        Class cl = oldObj.getClass();
        List<ChangeItem> changeItems = new ArrayList<ChangeItem>();

        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(cl, Object.class);

            for (PropertyDescriptor propertyDescriptor : beanInfo
                    .getPropertyDescriptors()) {
                String fieldname = propertyDescriptor.getName();
                String oldProp = getValue(PropertyUtils.getProperty(oldObj,
                        fieldname));
                String newProp = getValue(PropertyUtils.getProperty(newObj,
                        fieldname));

                if (!oldProp.equals(newProp)) {
                    ChangeItem changeItem = new ChangeItem();
                    changeItem.setField(fieldname);
                    changeItem.setNewValue(newProp);
                    changeItem.setOldValue(oldProp);
                    changeItems.add(changeItem);
                }
            }
        } catch (Exception e) {
            logger.error("There is error when convert changeset", e);
        }

        return changeItems;
    }

答案 1 :(得分:0)

您需要执行以下更改才能使其正常工作。

1)将字符串返回类型添加到类checkAuthentication中的方法ClinicServiceImpl.java,以确定登录是"success"还是"failure"

2)根据LoginController.java中的此返回值,您可以重定向到相应的页面。

3)在checkAuthentication类的UserDaoImpl方法中,您需要使用输入值username和password检查数据库中是否存在任何记录。

代码应如下所示:

<强> LoginController.java

@RequestMapping(value="/home.html", method = RequestMethod.POST)
public ModelAndView homePagePost(@ModelAttribute("user") User user)
{   
    setAppContext();         
    String result = clinicService.checkAuthentication(user);     

    ModelAndView mav = new ModelAndView();
    if("success".equals(result)) {
       mav.setViewName("Home"); 
    } else {
       mav.setViewName("Login");
    }       
    return mav;
}

<强> ClinicServiceImpl.java:

public String checkAuthentication(User user) {
    ClassPathXmlApplicationContext ctx = new  ClassPathXmlApplicationContext("clinicconfig.xml");
    userDAO = ctx.getBean("userDAO", UserDAO.class);

    user.setLogin(user.getLogin());
    user.setPassword(user.getPassword());

    String result = userDAO.checkAuthentication(user);
    return result;
}

<强>在UserDAOImpl:

public String checkAuthentication(User user) {
    String query = "SELECT login, password FROM virtualclinic.user WHERE login=? AND password=?";
    String result = null;
    Connection con = null;
    PreparedStatement ps = null;
    try{
        con = dataSource.getConnection();
        ps = con.prepareStatement(query);
        ps.setString(1, user.getLogin());
        ps.setString(2, user.getPassword());
        ResultSet out = ps.executeQuery();
        out.last();
        int count  = out.getRow();
        if(count==1) {
           result = "success";
        } else {
           result = "failure";
        }
    }catch(SQLException e){
        e.printStackTrace();
    }finally{
        try {
            ps.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }// TODO Auto-generated met
    return result;
}