2017-01-28 72 views
-1

基本的Spring MVC登錄頁面功能不起作用。 數據庫連接正常。 當我輸入正確的用戶名和密碼時,它將轉到else條件。 if循環功能不起作用。請有人幫我基本的Spring MVC登錄頁面功能不起作用

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

回答

0

很少有東西這裏要注意:

不創建submitUserLogin整個Spring上下文。

比較使用==對'equals'的字符串是不正確的。