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