简体   繁体   English

基本的 Spring MVC 登录页面功能不起作用

[英]Basic Spring MVC login page functionality not working

Basic Spring MVC login page functionality not working.基本的 Spring MVC 登录页面功能不起作用。 The database connectivity is working fine.数据库连接工作正常。 When I enter the correct username and password it goes to the else condition.当我输入正确的用户名和密码时,它会进入 else 条件。 The if loop functionality is not working. if 循环功能不起作用。 Kindly some one help me请有人帮助我

LoginController

package com.programcreek.helloworld.controller;

import java.sql.SQLException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;



@Controller
public class LoginController {

    @RequestMapping("/login")
    public ModelAndView viewLogin()
    {
        ModelAndView mv = new ModelAndView("login");
        return mv;
    }


    @RequestMapping(value ="/submitlogin", method= RequestMethod.POST)
    public ModelAndView submituserLogin(@RequestParam("userName")String userName, @RequestParam("password")String password ) throws SQLException
    {

        ApplicationContext context = 
                 new ClassPathXmlApplicationContext("Beans.xml");

          LicenseManagementDaoImp LicenseManagementDaoImp = 
          (LicenseManagementDaoImp)context.getBean("LicenseManagementDaoImp");

          UserAccountController formInfo = new UserAccountController(userName,password);                  
          UserAccountController userAcc = LicenseManagementDaoImp.loginInfo(userName);

           if(formInfo.getUserName() == userAcc.getUserName() && formInfo.getPassword() == userAcc.getPassword())
          {
                ModelAndView mv = new ModelAndView("loginsuccess");
                mv.addObject("headermsg","You have successfully logged in");
                mv.addObject("msg1", userAcc.getUserName());
                mv.addObject("msg2", userAcc.getPassword());
                return mv;           
          }

          else 
          {  
                ModelAndView mv = new ModelAndView("login");
                mv.addObject("msg1", "Please enter a Valid Username or Password");
                return mv;

          } 


          }

    }

UserAccountController用户帐户控制器

package com.programcreek.helloworld.controller;

public class UserAccountController

{
  private String userName;
  private String password;

  public UserAccountController() 
  {

  }

  public UserAccountController(String userName, String password) 
  {
    super();
    this.userName = userName;
    this.password = password;
  }

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}


}

LicenseManagementDaoImp

package com.programcreek.helloworld.controller;

import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

public class LicenseManagementDaoImp 
{
    private DataSource dataSource;
    private JdbcTemplate jdbcTemplateObject;

    public void setDataSource(DataSource dataSource)
    {
        this.dataSource = dataSource;
        this.jdbcTemplateObject = new JdbcTemplate(dataSource);
    }


    public  UserAccountController loginInfo(String userName) throws SQLException
    {
        try
        {
        String SQL = "Select strusername, strpassword from user where strusername = ? ";
        UserAccountController userAcc = jdbcTemplateObject.queryForObject(SQL, new LicenseManagementMapper(), userName);        
        return userAcc;

        }
        catch(EmptyResultDataAccessException e)
        {
            return new UserAccountController();     
        }
    }



}

LicenseManagementMapper

package com.programcreek.helloworld.controller;

import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;



public class LicenseManagementMapper implements RowMapper<UserAccountController> 
{
     public UserAccountController mapRow(ResultSet rs, int rowNum) throws SQLException 
     {
         UserAccountController userAcc = new UserAccountController();
         userAcc.setUserName(rs.getString("strusername"));
         userAcc.setPassword(rs.getString("strpassword"));
         return userAcc;                  
       }
}

Few things to note here:这里有几点需要注意:

do not create the whole spring context in submitUserLogin.不要在 submitUserLogin 中创建整个 spring 上下文。

comparing strings using == against 'equals' is incorrect.使用 == 与 'equals' 比较字符串是不正确的。

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

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