I know the “pet” object below is stored to session by using @SessionAttributes, but can I use session.getAttribute ? (such as .. Pet pet = (Pet)session.getAttribute(“pet”)). I am not sure why I do not need a “Session Key”. (We have to define the key when using session.setAttribute(“id”, id))
@Controller
@RequestMapping("/editPet.do")
@SessionAttributes("pet")
public class EditPetFormController {
@Autowired
private final Clinic clinic;
@RequestMapping(method = RequestMethod.GET)
public String setupForm(@RequestParam("petId") int petId, ModelMap model) {
Pet pet = this.clinic.loadPet(petId);
model.addAttribute("pet", pet);
return "petForm";
}
Session attributes configured via
@SessionAttributesare intended to be accessed as model attributes rather than by calling methods ofSessiondirectly, therefore their names are generated from the model attribute names automatically.By default, session attribute name is the same as the model attribute name, so you can access it as
session.getAttribute("pet")(seeDefaultSessionAttributeStore).