繁体   English   中英

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

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

我正在制作一个Web应用程序(虚拟诊所),为此我制作了DAO和Service层(我是新手),它在Controller中可以正常工作,但是我不知道如何比较@ModelAttribute("user") User user要使用db中的值(登录名,密码),如果输入的值在数据库中,我想为了应用程序将重定向到Home.jsp,否则-应用程序将重定向到其他jsp。 有人可以告诉我如何正确执行吗?

这是一个代码:

的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页面:

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

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:

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两个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)一个串返回类型添加到方法checkAuthentication在类ClinicServiceImpl.java ,以确定登录是否是"success""failure"

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

3)在UserDaoImpl类的checkAuthentication方法中,需要检查输入值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;
}

暂无
暂无

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

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