Announcement Announcement Module
Collapse
No announcement yet.
org.springframework.web.HttpSessionRequiredExcepti on: Expected session attribute Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • org.springframework.web.HttpSessionRequiredExcepti on: Expected session attribute

    I am getting the following error:
    Code:
    org.springframework.web.HttpSessionRequiredException: Expected session attribute 'bulletin'
    	at org.springframework.web.method.annotation.ModelFactory.initModel(ModelFactory.java:103)
    	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:639)
    	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:603)
    	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
    	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:859)
    	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:883)
    	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:792)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
    	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)
    The work flow goes like this. I start up my application, which hits the home controller:
    Code:
    @Controller
    public class HomeController {
    	@RequestMapping(value = "/")
    	public String home(Model model) {
    		model.addAttribute("bulletin", new Bulletin());
    		return "Welcome";
    	}
    }
    The controller sends me to a home page with this form:
    Code:
    <form:form modelAttribute="bulletin" action="getApprovedBulletins" method="post">  
        <input type="submit" name="click" value="Click here" />  
    </form:form>
    This is supposed to be the method that gets called, but I get the above error instead:
    Code:
    @Controller
    @SessionAttributes("bulletin")
    public class BulletinsController {
    	private BulletinDAO bulletinDAO;
    
    	@RequestMapping(value = "/getApprovedBulletins", method = RequestMethod.POST)
    	public ModelAndView getApprovedBulletins(
    			@ModelAttribute("bulletin") Bulletin bulletin, BindingResult result) {
    		ModelAndView mav = new ModelAndView();
    
    		try {
    			List<Bulletin> bulletins = bulletinDAO.getApprovedBulletins();
    			mav.setViewName("EnterBulletin");
    			if (bulletins != null) {
    				mav.addObject("bulletins", bulletins);
    			}
    		} catch (Exception e) {
    			System.out.println(e.getMessage());
    			mav.setViewName("FailurePage");
    		}
    
    		return mav;
    	}
    I don't know if it's important, but Bulletin is defined like this:
    Code:
    package com.model;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity
    @Table(name="bulletins")
    public class Bulletin {
    	private int id;
    	private java.sql.Date date;
    	private String subject;
    	private String name;
    	private String note;
    	boolean approved;
    
    	@Id
    	@GeneratedValue
    	@Column(name="id")
    	public int getId() {
    		return this.id;
    	}
    
    	public void setId(int id) {
    		this.id = id;
    	}
    
    	@Column(name="date")
    	public java.sql.Date getDate() {
    		return this.date;
    	}
    
    	public void setDate(java.sql.Date date) {
    		this.date = date;
    	}
    
    	@Column(name="subject")
    	public String getSubject() {
    		return subject;
    	}
    
    	public void setSubject(String subject) {
    		this.subject = subject;
    	}
    
    	@Column(name="name")
    	public String getName() {
    		return this.name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	@Column(name="bulletin")
    	public String getNote() {
    		return this.note;
    	}
    
    	public void setNote(String note) {
    		this.note = note;
    	}
    	
    	@Column(name="approved")
    	public boolean isApproved() {
    		return this.approved;
    	}
    
    	public void setApproved(boolean approved) {
    		this.approved = approved;
    	}
    }

  • #2
    IN controller 1st you put an object in the model (not in the session) and in the 2nd controller you expect a modelattribute to come from the session... That obviously isn't going to work, next to that the @SessionAttributes isn't designed to be used between controllers but for use in a single controller.

    So either remove @SessionAttributes or move the code from your HomeController into your BulletinsController .

    Comment


    • #3
      I added the following code to getApprovedBulletins(), which solved this particular issue.

      mav.addObject("command", bulletins);

      Comment


      • #4
        Hi

        Can you explain me how it is resolved? Because I am also running with same issue

        Comment

        Working...
        X