简体   繁体   English

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

[英]Spring - How can I compare values from JSP form to values in db? (Service+DAO)

I'm making a web application (Virtual Clinic)I made DAO and Service layers (I'm beginner at it) for that and it works fine in Controller, but I don't know how can I compare @ModelAttribute("user") User user to values (login, password) which are in db, I want in order to application will redirect to Home.jsp if entered values are in database, if not - then app will redirect to different jsp. 我正在制作一个Web应用程序(虚拟诊所),为此我制作了DAO和Service层(我是新手),它在Controller中可以正常工作,但是我不知道如何比较@ModelAttribute("user") User user要使用db中的值(登录名,密码),如果输入的值在数据库中,我想为了应用程序将重定向到Home.jsp,否则-应用程序将重定向到其他jsp。 Could somebody show me how should I do it properly? 有人可以告诉我如何正确执行吗?

Here is a code: 这是一个代码:

LoginController: 的LoginController:

@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;
}

Login.jsp: Login.jsp页面:

<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>

UserDaoImpl: 在UserDAOImpl:

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

}

ClinicServiceImpl: ClinicServiceImpl:

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

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

    userDAO.checkAuthentication(user);

}

diff the two bean, here is an implementation: diff两个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;
    }

You need to do the below changes to make it work. 您需要进行以下更改以使其起作用。

1) Add a string return type to method checkAuthentication in the class ClinicServiceImpl.java , to identify whether login is "success" or "failure" . 1)一个串返回类型添加到方法checkAuthentication在类ClinicServiceImpl.java ,以确定登录是否是"success""failure"

2) Based on this return value in the LoginController.java you can redirect to the appropriate page. 2)根据LoginController.java返回值,您可以重定向到相应的页面。

3) In the checkAuthentication method of UserDaoImpl class, you need to check whether any record exist in the DB with the input value username and password. 3)在UserDaoImpl类的checkAuthentication方法中,需要检查输入值username和password在数据库中是否存在任何记录。

Code should be as below: 代码应如下所示:

LoginController.java 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: 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: 在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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM