2017-12-20 1 views
-1

세션 스프링 mvc를 추가하는 방법 내 의도는 로그인 이메일과 비밀번호를 가지고 있으며 id, emai, gender와 같은 사람의 모든 세부 정보를 얻습니다. 그리고 나는 이드를 호출 할 때 세션에 배치합니다. ID가 그런 식으로 추가됩니다세션 봄 mvc를 추가하는 방법

@Controller 
    //@Scope("session") 
    @SessionAttributes("admin") 
    //@RequestMapping("admin") 
    public class AdminController { 
@Autowired 
private EmployeeService employeeService; 

@Autowired 
private EmployeeCredentialsService employeeCredentialsService; 



@RequestMapping(value="adminLogin", method = RequestMethod.GET) 
public String showForm(Map model) { 
    AdminBean adminBean = new AdminBean(); 
    model.put("admin", adminBean); 

    return "adminLogin"; 
} 

@RequestMapping(value="/adminLogin", method = RequestMethod.POST) 
public String adminLogin(@Valid AdminBean adminBean, BindingResult result, Map model,HttpSession session){ 
    if (result.hasErrors()) { 
     return "adminLogin"; 
    } 
    boolean userExists = employeeCredentialsService.checkLogin(adminBean.getEmail(),adminBean.getPassword()); 
    System.out.println(userExists); 
    if(userExists){ 
     //model.put("adminBean", adminBean.getEmail()); 
     //session.setAttribute("adminBean", adminBean); 
     return "adminHome"; 
    }else{ 
     result.rejectValue("email","invaliduser"); 
     model.put("invalid", "Invalid Username or Password!! Please try again!!!"); 
     return "adminLogin"; 
    } 

}

//enter code here 
    public boolean checkLogin(String email, String password){  
    Session session = sessionFactory.openSession(); 
    boolean userFound = false;  
    //String SQL_QUERY ="select * from Oruganti_admin where admin_user_name='"+email+"' and adminpassword='"+password+"'"; 
    String SQL_QUERY ="from AddAdmin as A where A.email=? and A.password=? "; 
    Query query = session.createQuery(SQL_QUERY); 
    query.setParameter(0,email); 
    query.setParameter(1,password); 
    List list = query.list(); 

    if ((list != null) && (list.size() > 0)) { 
     userFound= true; 
    } 

    session.close(); 
    //System.out.println(userFound); 
    return userFound;    

} 나머지 모든

+0

무엇이 문제입니까? – VPK

+0

봄 mvc 프로젝트에서 세션을 추가하는 방법을 혼동? – malli

+0

'session.setAttribute ("adminBean", adminBean);에 대한 주석을 제거하면 어떻게됩니까? – VPK

답변

0

당신이 HTML 페이지의 adminBean 내부의 값을 표시하려는 경우, 당신은 U 수 JSP의 암시 적 객체. 당신이 adminBean1 객체의 내부 속성 adminName을 고려할 때, 당신은 adminBean1.adminName를 사용하여 인쇄 할 수 있습니다

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "w3.org/TR/html4/loose.dtd">; 
<html> 

<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
    <title>admin home page</title> 
</head> 

<body> welcome to ${adminBean1.adminName} 
    <h4 align="left"><a href="add.html">Registration</a></h4> 
    <h4 align="left"><a href="employees.html">List of Students</a></h4> </body> 

</html> 

당신은 당신이 표시하려는 어떤 속성의 값과 재산 adminName를 교체해야합니다. 해당 속성 이름은 AdminBean 클래스와 정확히 같아야합니다.

+0

$ {adminBean1.adminName}을 추가했지만 작동하지 않습니다. 작동하지 않는 것이 내 코드에 다른 문제가 있습니다. – malli

+0

'AdminBean' 클래스의 내용은 무엇입니까? – VPK

+0

package com.dineshonjava.콩; 공용 클래스 AdminBean { \t \t 개인 문자열 전자 메일; \t \t \t 개인 문자열 암호; \t public String getEmail (\t \t return email; \t} \t 공개 무효 setEmail (문자열 전자 메일) { \t \t this.email = email; \t} \t public String getPassword (\t \t return password; \t} \t 공백 void setPassword (문자열 암호) { \t \t this.password = password; \t} } – malli

관련 문제