java.lang.IllegalStateException:没有为范围'session'注册的Scope

时间:2015-04-13 03:45:51

标签: spring model-view-controller

我开始使用Spring,我想创建一个相当简单的webapp。

首先我有一个 web.xml

   <servlet>
        <servlet-name>MyServletController</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyServletController</servlet-name>
        <url-pattern>/submitQuery</url-pattern>     
          <url-pattern>/saveTextAttributes</url-pattern>
          <url-pattern>/saveTextLinks</url-pattern>
    </servlet-mapping>

此配置 MyServletController-servlet.xml

<context:component-scan
    base-package="world.hello.mycontrollers"/>

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

控制器 MyServletController.java 的java实现:

package world.hello.mycontrollers;

@Controller
public class MyServletController
{

private QueryRunner queryRunner;
@RequestMapping("/submitQuery")
    public ModelAndView submitQuery(HttpServletRequest request) 
    {
     ApplicationContext context = 
                        new ClassPathXmlApplicationContext("springBeans.xml");

    this.queryRunner = (QueryRunner)context.getBean("queryRunner"); 


    Query myQuery = new Query(request.getParameter("name"));
    ResponseCode rc = queryRunner.runQuery(myQuery);
    String json = "not yet implmented";

    try {
        json = mapper.writeValueAsString(rc);
    } catch (JsonProcessingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return new ModelAndView("text", "model", json);


    }

}

最后是 springBeans.xml

<bean id="queryRunner"
    class="world.hello.business.QueryRunner"
    scope = "session">
     <aop:scoped-proxy/>
</bean>

运行我访问此servlet,我得到java.lang.IllegalStateException: No Scope registered for scope 'session'

这是什么意思?

1 个答案:

答案 0 :(得分:0)

您可以尝试将ClassPathXmlApplicationContext替换为XmlWebApplicationContext,可能是吗?由于会话是与网络相关的范围。