Announcement Announcement Module
Collapse
No announcement yet.
session creation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • session creation

    Hi,

    I need to create sessions in my web application, for this i am trying to use session scoped beans as:

    1. applicationContext.xml is written as:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
    <beans>
    <bean id="resultBean" class="com.hcl.elsevier.beans.RegistrationBean"/>
    
    
    <bean name="user" class="com.hcl.elsevier.beans.UserDetails.java>
    <property name="result" ref="resultBean" />
    </bean>
    </beans>
    2. web.xml (part to look for different config files) is written as:-

    Code:
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-hibernate.xml</param-value>
      </context-param>
      
        
      <servlet>
        <servlet-name>context</servlet-name>
        <servlet-class>
          org.springframework.web.context.ContextLoaderServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
    
    <servlet>
    		<servlet-name>billingSheet</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<load-on-startup>1</load-on-startup>
        </servlet>
    3. I am required to etrive the bean (scoped as session in the following
    controller:
    Code:
    public class ProjectDetailsController implements Controller{
    	public ModelAndView handleRequest(HttpServletRequest req,HttpServletResponse res)
    	throws ServletException,IOException
    	{
    		
    		ApplicationContext ac = org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(req.getSession().getServletContext());
    		UserDetails user = (UserDetails) ac.getBean("user");
    		ModelAndView mav=new ModelAndView("ProjectDetailsUser");
    		mav.addObject("empDetails", user);
    		
    		return mav;
    	}
    	
    	
    
    }
    but exception is thrown as:-

    Code:
    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'user' is defined
    	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:408)
    	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:350)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    
    
    root cause 
    
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'user' is defined
    	org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:340)
    	org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:906)
    	org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
    	org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:161)
    	org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:642)
    	com.hcl.elsevier.controller.ProjectDetailsController.handleRequest(ProjectDetailsController.java:25)
    	org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:45)
    	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:820)
    	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:755)
    	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:396)
    	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:350)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    please help me to find out why the bean is not able to be found.

  • #2
    I would either add the controller as a bean in your applicationContext.xml and add a valid property pointing to the userBean, oder add the component-scan tag to let spring itself find valid beans.
    (e.g. <context:component-scan base-package="lalup" scoped-proxy="interfaces"/>

    If you use the component-scan take care to add appropriate annotations for the controller and the userBean.

    The controller should be annotated using@Controller, whereas your session-scoped bean annotations should look like:
    @Component
    @Scope("session")

    hope that helps ..
    (thanks to merten who helped me beforehand on this issue)

    Comment


    • #3
      1) Why are you retrievin it? Why not simply inject it?
      2) WebApplicationContextUtils gives you access ONLY to the ROOT application context if you want to access the current DispatcherServlets ApplicationContext use RequestContextUtils. (But if you inject the object into your controller that isn't a problem).
      3) Your object is wrongly configured, I see no scope="session" nor a <aop:scoped-proxy/> element inside your session scoped bean. Both are mandatory for them to work.

      Comment


      • #4
        Hi marten,

        Please explain in more simpler language as i couldn't get it much. by mistake the scope="session" was removed while pasting the code, i have added it.

        Please explain a bit furtehr, i will be of great help.

        Comment


        • #5
          Even simpler. Ehr I explained it quite clearly imho.

          Originally posted by mdeinum
          1) Why are you retrievin it? Why not simply inject it?
          2) WebApplicationContextUtils gives you access ONLY to the ROOT application context if you want to access the current DispatcherServlets ApplicationContext use RequestContextUtils. (But if you inject the object into your controller that isn't a problem).
          You are using spring INJECT your dependencies. Next to that what you are doing is giving you only access to the ROOT applicationcontext, that is the xml files loaded by the ContextLoaderListener. Your session scoped bean is located in the DispatcherServlets application context, accessible only by using the RequestContextUtils to gain access to the applicationcontext.

          However you are using spring so why are you even trying it in this way. Simply inject your session scoped bean.

          Code:
          public class ProjectDetailsController implements Controller{
          	public ModelAndView handleRequest(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException {	
          		ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(req.getSession().getServletContext());
          		UserDetails user = (UserDetails) ac.getBean("user");
          		ModelAndView mav=new ModelAndView("ProjectDetailsUser");
          		mav.addObject("empDetails", user);		
          		return mav;
          	}
          }
          should be

          Code:
          public class ProjectDetailsController implements Controller{
          
            private UserDetails user;
          
            public void setUserDetails(UserDetails userDetails) {
              this.user=userDetails;
            }
          
          	public ModelAndView handleRequest(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException {	
          		ModelAndView mav=new ModelAndView("ProjectDetailsUser");
          		mav.addObject("empDetails", user);		
          		return mav;
          	}
          }
          Use spring to do the wiring don't try to do it yourself.

          Originally posted by mdeinum
          3) Your object is wrongly configured, I see no scope="session" nor a <aop:scoped-proxy/> element inside your session scoped bean. Both are mandatory for them to work.
          billingsheet-servlet.xml
          Code:
          <bean name="projectDetailsController" class="ProjectDetailsController">
            <property name="userDetails" ref="user" />
          </bean>
          applicationContext.xml
          Code:
          <bean name="user" class="com.hcl.elsevier.beans.UserDetails" scope="session">
            <aop:scoped-proxy />
            <property name="result" ref="resultBean" />
          </bean>
          Change your configuration to the xml above. That way you have a session scoped bean AND dependency injection instead of the ugly lookup you have created.

          Comment

          Working...
          X