Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: Problem with @SessionAttributes and @ModelAttribute

  1. #1

    Default Problem with @SessionAttributes and @ModelAttribute

    I have a create account controller:

    Code:
    @Controller
    @SessionAttributes(CreateAccountController.MODEL_ATTRIBUTE)
    public class CreateAccountController {
    and I have a a method on that controller:

    Code:
    @RequestMapping(value=URL_CREATE_ACCOUNT, method=RequestMethod.GET)
    public String createAccount(@ModelAttribute(MODEL_ATTRIBUTE) CreateAccount createAccount, Map<String,Object> map, SessionStatus sessionStatus) {
    However when I attempt to access this url, I get the following exception:
    Code:
    org.springframework.web.HttpSessionRequiredException: No session found - session required for attribute 'createAccount'
    How do I force a session to be made?

  2. #2

    Default

    i think with:

    <property type session=true />

    something like that. google it

  3. #3

    Default

    Even if that were valid XML, it wouldn't matter. I'm using annotations, which mean my controllers are not the old school spring controller like you're thinking, they are just regular classes.

  4. #4

    Default

    Bump ... anyone?

  5. #5

    Default

    I'm having a simliar problem right now.

    I'm getting the error right after a "redirect:/some/url" if that helps.

  6. #6
    Join Date
    Feb 2007
    Location
    germany
    Posts
    40

    Default

    can you post your whole controller code?

    In order to use the sessionAttribute you still have to create/resolve it somehow. It's easy to see how it's done when looking at the PetClinic sample.

    Code:
    package org.springframework.samples.petclinic.web;
    
    import [...] (removed by me to save space)
    /**
     * JavaBean Form controller that is used to edit an existing <code>Pet</code>.
     *
     * @author Juergen Hoeller
     * @author Ken Krebs
     */
    @Controller
    @RequestMapping("/editPet.do")
    @SessionAttributes("pet")
    public class EditPetForm {
    
    	private final Clinic clinic;
    
    	@Autowired
    	public EditPetForm(Clinic clinic) {
    		this.clinic = clinic;
    	}
    
    	@ModelAttribute("types")
    	public Collection<PetType> populatePetTypes() {
    		return this.clinic.getPetTypes();
    	}
    
    	@RequestMapping(method = RequestMethod.GET)
    	public String setupForm(@RequestParam("petId") int petId, Model model) {
    		Pet pet = this.clinic.loadPet(petId);
    		model.addAttribute("pet", pet);
    		return "petForm";
    	}
    
    	@RequestMapping(method = RequestMethod.POST)
    	public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) {
    		new PetValidator().validate(pet, result);
    		if (result.hasErrors()) {
    			return "petForm";
    		}
    		else {
    			this.clinic.storePet(pet);
    			status.setComplete();
    			return "redirect:owner.do?ownerId=" + pet.getOwner().getId();
    		}
    	}
    
    }

  7. #7

    Default

    While I can't speak for Eric, I've got something similar to this:

    Code:
    @Controller
    @RequestMapping("/home")
    @SessionAttributes("account")
    public class AccountHome {
    	
    	@RequestMapping(method = RequestMethod.GET)
    	public String handleHome(@ModelAttribute("account") Account account) {
    		// do some stuff
                    return "home.jsp";
    	}
    
    }
    And this throws rg.springframework.web.HttpSessionRequiredExceptio n: No session found - session required for attribute 'account'

    I really don't understand how sessions are handled w/ Annotations. The reference documentation is like

    it's as easy as this: @SessionAttributes("something")

    but that doesn't give me much insight

  8. #8

    Default

    My problem is I want to store items in the session but I don't want them to be required, but from the looks of things, I would have to put placeholder data in the session to make this work, which seems pointless to me.

  9. #9

    Default

    Why would you be trying to access a session form object on the Request/GET side of the transaction?

    The way I understand @SessionAttributes in conjunction with @ModelAttribute is that you create the actual attribute on the GET side of things (by putting it in your ModelMap) and then retrieving it on the POST side.

    Are you using @SessionAttributes like you would by using HttpSession.setAttribute() ? Because that's not what @SessionAttributes does.

  10. #10
    Join Date
    Dec 2008
    Posts
    3

    Default

    If you do:

    sessionStatus.setComplete() then @SessionAttributes() will be cleaned up.

    See my next post for the URL (wouldn't let me put it in this one because this is my first post)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •