我正在编写代码,用户Customer
(Person
的子类)可以登录到网站。用户登录后,我需要:将登录用户的Customer
实例添加到会话中,在随后的页面中从会话中检索该Customer
实例,并读取成员属性值。
我已经尝试过以下操作,但是它在我的标签中显示null
。
Person
类:
@MappedSuperclass
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "personid")
private int personid;
@Column(name = "firstname")
private String firstname;
@Column(name = "lastname")
private String lastname;
@Column(name = "email")
private String email;
@Column(name = "password")
private String password;
public int getPersonid() { return personid; }
public void setPersonid(int personid) { this.personid = personid; }
public String getFirstname() { return firstname; }
public void setFirstname(String firstname) { this.firstname = firstname; }
public String getLastname() { return lastname; }
public void setLastname(String lastname) { this.lastname = lastname; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
}
Customer
类:
@Entity
@Table(name = "customer")
public class Customer extends Person {
@Column(name = "username")
private String username;
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
}
CustomerController
类:
@Controller
@RequestMapping(value = "/customer")
@SessionAttributes("customer")
public class CustomerController {
@Autowired
CustomerService customerService;
@Autowired
CustomerRepositary customerRepositary;
@GetMapping(value="/viewCustomer")
public ModelAndView viewCustomer() {
ModelAndView modelAndView = new ModelAndView();
Customer customer = new Customer();
modelAndView.addObject("customer", customer);
modelAndView.setViewName("view_profile");
return modelAndView;
}
@PostMapping(value="/addCustomer")
public ModelAndView addCustomer(@ModelAttribute("customer") Customer customer) {
ModelAndView modelAndView = new ModelAndView();
..........................................
if (existingCustomer != null) {
............................................
ModelAndView modelandView =
new ModelAndView("redirect:/customer/saveCustomer");
return modelAndView;
} else {
....................................
}
}
@PostMapping(value="/customerLogin")
public ModelAndView customerLogin(@ModelAttribute("customer") Customer customer) {
ModelAndView modelAndView = new ModelAndView();
Customer existingCustomer = customerRepositary.findByEmail(customer.getEmail());
if(existingCustomer != null) {
if(customer.getPassword().equals(existingCustomer.getPassword())) {
ModelAndView modelandView = new ModelAndView("redirect:/customer/viewCustomer");
return modelandView;
}
else {
modelAndView.addObject("message","Wrong Password!!");
modelAndView.setViewName("error");
//ModelAndView modelandView = new ModelAndView("redirect:/customer/customerLogin");
return modelAndView;
}
}
else {
modelAndView.addObject("message","No such email found!");
modelAndView.setViewName("error");
//ModelAndView modelandView = new ModelAndView("redirect:/customer/saveCustomer");
return modelAndView;
}
}
}
这是我试图显示数据的方式:
<label><%=session.getAttribute("customer.getEmail()")%></label>
如何获得此标签的Customer
email
属性的值?
作为替代,您可以将HttpSession
对象注入控制器方法,并将Customer
对象添加到Session
属性,然后从所需的任何位置检索它。希望这会有用。