I am receiving the following error on submitting my form:
org.springframework.web.HttpSessionRequiredException: Session attribute 'rulesForm' required - not found in session
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.raiseSessionRequiredException(AnnotationMethodHandlerAdapter.java:722)
My JSP contains the following:
<form:form id="rulesForm" modelAttribute="rulesForm" action="save.do">
...
</form>
My Controller contains the following:
#Controller
#RequestMapping("/rules")
#SessionAttributes({"rulesForm", "deskForm"})
public class RulesController {
.
.
.
#RequestMapping(value = "/save.do")
public ModelAndView saveRuleAttributesAndRules(#Valid
#ModelAttribute("rulesForm")
RulesFormDTO rulesForm, BindingResult bindingResult, HttpSession session, Principal principal) {
It seems that if I leave my browser open for a while with my form displaying and then I attempt to perform a submit after some time I get this error.
Really what I want to happen in this case is for the new "rulesForm" object to be created...how can I achieve this?
Thanks
As the Javadoc on #SessionAttribute indicates use of the annotation means you want store the specified model attributes in the session, which means you need to add them to the model first. Spring MVC will not create them.
In other words when you add a #ModelAttribute("rulesForm") controller method argument you're telling Spring MVC to look for it in the model or create a new instance if not found. However if you also add #SessionAttributes, Spring MVC will not attempt to create a new instance and will expect the object to be either in the model or in the session. You can use a #ModelAttribute method to add your object initially.
Related
A spring controller method returns ModelAndView and I want to write the response to a file, how can this be done ?
ModelAndView mv = new ModelAndView(viewName);
mv.addObject("foo",foo);
return mv;
can the to be rendered content accessed in the same controller method ?
The rendered view is not available at the controller level. If you need to access the rendered version in the controller, you could render the content yourself and return the rendered String from the controller method.
You could also get in further up in the stack and use a ServletFilter and wrap the output stream, e.g. with a TeeOutputStream from Apache commons. At the point you do however not really know which controller method was called. What you could do to signal this is to set a RequestAttribute in the controller method that the filter checks to decide whether to write to the file.
What is the different between flash and model attribute?
I want to store an object and display it in my JSP as well as reuse it in other controller. I have use sessionAttribute and it works fine in the JSP, but the problem is when I try to retrieve that model attribute in other controller.
I lose some data. I searched around and found that flash attribute allows to past past value to different controller, doesn't it?
If we want to pass the attributes via redirect between two controllers, we cannot use request attributes (they will not survive the redirect), and we cannot use Spring's #SessionAttributes (because of the way Spring handles it), only an ordinary HttpSession can be used, which is not very convenient.
Flash attributes provide a way for one request to store attributes intended for use in another. This is most commonly needed when redirecting — for example, the Post/Redirect/Get pattern. Flash attributes are saved temporarily before the redirect (typically in the session) to be made available to the request after the redirect and removed immediately.
Spring MVC has two main abstractions in support of flash attributes. FlashMap is used to hold flash attributes while FlashMapManager is used to store, retrieve, and manage FlashMap instances.
Example
#Controller
#RequestMapping("/foo")
public class FooController {
#RequestMapping(value = "/bar", method = RequestMethod.GET)
public ModelAndView handleGet(Model model) {
String some = (String) model.asMap().get("some");
// do the job
}
#RequestMapping(value = "/bar", method = RequestMethod.POST)
public ModelAndView handlePost(RedirectAttributes redirectAttrs) {
redirectAttrs.addFlashAttribute("some", "thing");
return new ModelAndView().setViewName("redirect:/foo/bar");
}
}
In above example, request comes to handlePost, flashAttributes are added, and retrieved in handleGet method.
More info here and here.
I have an action method on a Spring MVC controller that has an argument annotated with #ModelAttribute. However, I don't know at compile time what the type of this parameter will be - I know the abstract base type but not the derived type.
At runtime, I will be able to decide what class I am expecting and I will be able to get a new'd up instance of this class. However, I have no idea what code I should be calling to parse the request data in the same fashion that #ModelAttribute does.
I've looked around and it seems that if i can get a hold of a WebRequestDataBinder I can use that to populate my object, but for that I need a BinderFactory and this is where I kind of get lost.
Can anyone give me some pointers here - or tell me that I am looking at it the wrong way and need to do something else?
you can inject the model itself in your controllers method and access the attribute yourself.
#RequestMapping(...)
public void doStuff(ModelMap model) {
Object attr = model.get("nameOfAttribute");
// ...
}
I have read the Spring documentation and I was not able to find relevant information regarding the expected behavior of a Spring MVC controller method returning null (with a return type of String).
Can someone please provide a reply or direct me to relevant documentation? Or to put it another way, what would be the benefit of returning null from a Spring MVC controller method?
In Spring 2, when you returned null from a controller you were saying to the Spring dispatcher that you don't want it to search for a view.
You did this if you were handling the response yourself by writing the response content directly and then flushing the output stream (you were managing a file download for example).
If you didn't return null, Spring would have forwarded to a view who would try to write to the response also, messing up your already written data or resulting in an exception if the response was already commited.
Returning null was a way of saying back off to Spring's view resolver.
A lot of things changed in Spring 3 and now the same can be obtained by having an #RequestMapping annotated method that returns void.
If you have a return type of String but you return null I think that it uses the default RequestToViewNameTranslator for translating an incoming HttpServletRequest into a logical view name when the view name wasn't explicitly supplied.
Return type String in spring mvc generally returns your view resolver, it can be your JSP, html or any other view page.
http://www.mkyong.com/spring3/spring-3-mvc-hello-world-example/
Suppose you want to return a normal String like "hi", you can use #ResponseBody annotation to do this.
I have created a controller that does some business logic and creates a model. If I pass this model directly to view by returning ModelAndView with view name and model - everything working great. But now I want to display results at another page. So I use "redirect:" prefix to redirect to another controller, but the model is lost.
What Im missing?
Regards,
Oleksandr
you can use the forward: prefix which ultimately does a RequestDispatcher.forward()
insted of The redirect: prefix.
Option 1 :
You might put the model in session and get it back in the controller and nullify it in session.
Option 2 :
You said, you are having two controllers, first one would retrieve the user input and do some business logic and redirect to other one. My suggestion is to move the business logic which is placed in both controllers to a class and have only one controller which would return the model and view to the user.
During redirect: request is sent using GET method with parameters appended to the url. You can right a new method in the controller to receive the request parameters with #RequestParameter annotation.
While redirecting the request simple add all the parameters as string
e.g ModelAndView mv = new ModelAndView();
mv.setViewName(redirect:/your/url);
mv.addObject("param1", string_param);
.
.
.
This way you can redirect successfully.
Since the redirect: prefix runs another controller, you will lose whatever you had in the ModelAndView in the previous controller. Perhaps the business logic controller should not be a controller at all -- maybe you should just make it a regular class, and have the results controller call it. Then the results controller could save the data to the model.
i would not use #SessionAttributes as it may change in future releases of spring. I would stick with the old fashioned way that Oleksandr showed you above
somehow an old post but maybe someone else will find it usefull