2

我最近开始使用弹簧。我正在尝试实现用户会话,但我总是失败。我花了两天时间寻找答案,但我找不到。我使用了注释和配置 xml 文件,但什么也没有。首先,我尝试在 applicationContext.xml 中写这个:

<bean id="usuarioLogueado" class="modelos.Usuarios" scope="session">
<!-- Inyected in next bean placed in dispatcher-servlet.xml -->
<bean name="registroController" class="controladores.RegistroController">
   <property name="logueado" ref="usuarioLogueado"/>
</bean>

错误是:“范围'会话'对于当前线程不是活动的”。我找到了解决方案。我不得不将下一个代码放在 web.xml 文件中:

<listener>    
<listenerclass>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>

之后,这个问题消失了,我变得快乐,但快乐是短暂的......在我的电脑和网络中的其他应用程序中测试应用程序我检查了 bean (usuarioLogueado) 仍然表现得像一个单例 bean。我在博客中看到需要使用作用域代理,如下所示:

<bean id="usuarioLogueado" class="modelos.Usuarios" scope="session">
        <aop:scoped-proxy/>
</bean>

在 ApplicationContext.xml 文件中。命名空间是:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

现在,应用程序没有运行。如果我写任何 aop 元素,这不会运行。我已经检查了其他名称空间、3.1 和以前版本的 spring,但什么也没有。我使用 netbeans ide,当应用程序不运行时,这个 ide 不会提供很多信息。它只是说“上下文无法运行”。如果我删除 aop:scoped-proxy 标记并使用等效注释 (@Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES)),我会遇到同样的问题。所以,我认为这个问题是由于 web.xml 中描述的上下文。我把这个 web.xml 文件放在这里:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <!-- Por defecto en web.xml -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- Lo siguiente es para que la aplicación reconozca los scope request y session -->
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <!-- Por defecto en web.xml -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.htm</welcome-file>
    </welcome-file-list>
    <!-- Lo siguiente es para que el contexto de la aplicación reconozca la codificación UTF-8 -->
    <filter>  
        <filter-name>encodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>encodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping> 
</web-app>

我也尝试过使用过滤器。我测试的第一个过滤器:

   <filter>
     <filter-name>springSecurityFilterChain</filter-name>
     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
     </filter>
     <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
     </filter-mapping> 

结果,proyect 没有运行。第二个过滤器:

  <filter>
        <filter-name>requestContextFilter</filter-name>
        <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>requestContextFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

结果: proyect 运行,但作用域 bean 仍然是单例的,当我放置或等效注释时,proyect 不运行。

请帮忙。我找不到答案。请原谅我写的表达,我通常不写英文。谢谢

4

0 回答 0