2

最近春を使い始めました。ユーザー セッションを実装しようとしていますが、常に失敗します。答えを探すのに 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>

その後、その問題がなくなり、幸せになりましたが、幸せは短かったです... PC でアプリケーションをテストし、ネットワークで別のアプリケーションをテストして、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、および以前のバージョンの春で確認しましたが、何も確認しませんでした。私は 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> 

結果、プロジェクトは実行されません。2 番目のフィルター:

  <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>

結果: プロジェクトは実行されますが、スコープ Bean はまだシングルトンであり、または同等のアノテーションを付けると、プロジェクトは実行されません。

助けてください。答えが見つかりませんでした。そして、表現を書いてすみません、私は通常英語で書きません。ありがとう

4

0 に答える 0