Announcement Announcement Module
Collapse
No announcement yet.
Form is null on a post request after a period of time Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Form is null on a post request after a period of time

    Hi there,
    I have a question regarding forms, when being null, i.e., not in session anymore.

    Basically, I have a LogInController that creates a new form (logInForm) on
    a get request.

    On the post request, it uses that form (logInForm) to validate fields and
    other business logic functions.

    Here is the problem, on a post request, the logInForm can be null after a period of time, i.e., not in session anymore.
    As you can see from the code at the bottom of this message, the logInForm is stored in session, @SessionAttributes("logInForm").
    If the logInForm is null an exception is obviously thrown when doing the form validation.

    The simple answer would be to redirect to the get request, if the logInForm is null:
    Code:
    	@RequestMapping(value="/login", method=RequestMethod.POST)
    	public String processSubmit(@ModelAttribute("logInForm") LogInForm logInForm, BindingResult result, SessionStatus status) throws RemoteException {
    		if (logInForm == null) {
                        return "redirect:login";
    		}
    	}

    But, I have never seen any code examples that check whether the form(s) is null or not.
    Are there any best practices, better way of doing this?

    Code:
    @Controller
    @SessionAttributes("logInForm")
    public class LogInController {
    	
    	private UserWebService userService;
    	
    	@Autowired	
    	public LogInController(UserWebService userService) {
    		this.userService = userService;
    	}
    	
    	@RequestMapping(value="/login", method=RequestMethod.GET)
    	public String setupForm(ModelMap model) {
    		model.addAttribute("logInForm", new LogInForm());
    		return "login";
    	}
    	
    	@RequestMapping(value="/login", method=RequestMethod.POST)
    	public String processSubmit(@ModelAttribute("logInForm") LogInForm logInForm, BindingResult result, SessionStatus status) throws RemoteException {
    		
                    /* The below code will throw an exception if logInForm is not in session, if the logInform is null */
    		new LogInValidator().validate(logInForm, result);
    		if (result.hasErrors()) {
    			return "login";
    		}
    		
    		/* Do business logic here */
    		
    		return "redirect:myadmin";
    	}	
    }

    Thanks,
    Sinoe

  • #2
    Hi,
    does anyone know the answer to the question above?

    Thanks,
    Sinoe

    Comment


    • #3
      No news anyone?

      Comment

      Working...
      X