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

时间:2017-01-28 06:24:09

标签: spring spring-mvc spring-boot spring-security spring-jdbc

Basic Spring MVC登录页面功能不起作用。 数据库连接正常。 当我输入正确的用户名和密码时,它会转到其他条件。 if循环功能不起作用。请有人帮助我

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

1 个答案:

答案 0 :(得分:0)

这里有几点需要注意:

不要在submitUserLogin中创建整个spring上下文。

使用==对比&#39; equals&#39;来比较字符串是不正确的。