当前位置: 首页 > 面试题库 >

Spring MVC自定义范围bean

叶煌
2023-03-14
问题内容

我想创建自己的自定义范围bean,它将使用HTTP会话(类似于Flash作用域)。

根据Spring手册,我需要实现org.springframework.beans.factory.config.Scope接口

public class CustomScope implements Scope {

    @Override
    public Object get(String arg0, ObjectFactory<?> arg1) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public String getConversationId() {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void registerDestructionCallback(String arg0, Runnable arg1) {
        // TODO Auto-generated method stub
    }
    @Override
    public Object remove(String arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public Object resolveContextualObject(String arg0) {
        // TODO Auto-generated method stub
        return null;
    }
}

我的问题是如何在此bean中获取HTTP会话?我知道,如果我在ServletContext范围内创建bean,则将实现ServletContextAware接口。

请帮忙 :)


问题答案:

我希望它对将来的某人有用,所以我想分享一下。

我对此进行了一些研究,发现不幸的是,不可能获得Spring MVC的HTTP会话。

我的目的是使用PRG模式为Spring MVC Controller实现Flash Scope。

在Spring论坛中进行更多研究后,我找到了使用HandlerInterceptor进行编码的方法。

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.util.*;
import java.util.Map.Entry;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class FlashScopeInterceptor implements HandlerInterceptor {

    public static final String DEFAULT_ATTRIBUTE_NAME = "flashScope";
    public static final String DEFAULT_SESSION_ATTRIBUTE_NAME = FlashScopeInterceptor.class.getName();
    public static final int DEFAULT_RETENTION_COUNT = 2;

    private String sessionAttributeName = DEFAULT_SESSION_ATTRIBUTE_NAME;
    private String attributeName = DEFAULT_ATTRIBUTE_NAME;
    private int retentionCount = DEFAULT_RETENTION_COUNT;

    /**
     * Unbinds current flashScope from session. Rolls request's flashScope to
     * the next scope. Binds request's flashScope, if not empty, to the session.
     * 
     */

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        if (request.getSession( false ) != null)
        {
            request.getSession().removeAttribute( this.sessionAttributeName );
        }
        Object requestAttribute = request.getAttribute( this.attributeName );
        if (requestAttribute instanceof MultiScopeModelMap)
        {
            MultiScopeModelMap attributes = (MultiScopeModelMap) requestAttribute;
            if (!attributes.isEmpty())
            {
                attributes.next();
                if (!attributes.isEmpty())
                {
                    request.getSession( true ).setAttribute( this.sessionAttributeName, attributes );
                }
            }
        }
    }

    /**
     * merge modelAndView.model['flashScope'] to current flashScope
     */
    @Override
    public void postHandle( HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        if (modelAndView != null)
        {
            Map<String, Object> modelFlashScopeMap = null;
            for (Iterator<Entry<String, Object>> iterator = ((Map<String, Object>) modelAndView.getModel()).entrySet()
                    .iterator(); iterator.hasNext();)
            {
                Entry<String, Object> entry = iterator.next();
                if (this.attributeName.equals( entry.getKey() ) && entry.getValue() instanceof Map)
                {
                    if (modelFlashScopeMap == null)
                    {
                        modelFlashScopeMap = (Map) entry.getValue();
                    }
                    else
                    {
                        modelFlashScopeMap.putAll( (Map) entry.getValue() );
                    }
                    iterator.remove();
                }
                else if (entry.getKey().startsWith( this.attributeName + "." ))
                {
                    String key = entry.getKey().substring( this.attributeName.length() + 1 );
                    if (modelFlashScopeMap == null)
                    {
                        modelFlashScopeMap = new HashMap<String, Object>();
                    }
                    modelFlashScopeMap.put( key, entry.getValue() );
                    iterator.remove();
                }
            }
            if (modelFlashScopeMap != null)
            {
                MultiScopeModelMap flashScopeMap;
                if (request.getAttribute( this.attributeName ) instanceof MultiScopeModelMap)
                {
                    flashScopeMap = (MultiScopeModelMap) request.getAttribute( this.attributeName );
                }
                else
                {
                    flashScopeMap = new MultiScopeModelMap( this.retentionCount );
                }
                flashScopeMap.putAll( modelFlashScopeMap );
                request.setAttribute( this.attributeName, flashScopeMap );
            }
        }
    }

    /**
     * binds session flashScope to current session, if not empty. Otherwise cleans up empty
     * flashScope
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession( false );
        if (session != null)
        {
            Object sessionAttribute = session.getAttribute( this.sessionAttributeName );
            if (sessionAttribute instanceof MultiScopeModelMap)
            {
                MultiScopeModelMap flashScope = (MultiScopeModelMap) sessionAttribute;
                if (flashScope.isEmpty())
                {
                    session.removeAttribute( this.sessionAttributeName );
                }
                else
                {
                    request.setAttribute( this.attributeName, flashScope );
                }
            }
        }
        return true;
    }
}

现在MultiScopeModelMap.java

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.map.CompositeMap;
import org.apache.commons.collections.map.CompositeMap.MapMutator;

public class MultiScopeModelMap extends CompositeMap implements Serializable, MapMutator
{
    public MultiScopeModelMap(int num)
    {
        super();
        setMutator( this );
        for(int i = 0; i < num; ++i)
        {
            addComposited( new HashMap() );
        }
    }

    /** Shadows composite map. */
    private final LinkedList<Map> maps = new LinkedList<Map>();

    @Override
    public synchronized void addComposited( Map map ) throws IllegalArgumentException
    {
        super.addComposited( map );
        this.maps.addLast( map );
    }



    @Override
    public synchronized Map removeComposited( Map map )
    {
        Map removed = super.removeComposited( map );
        this.maps.remove( map );
        return removed;
    }



    /** 
     * Starts a new scope. 
     * All items added in the session before the previous session are removed.
     * All items added in the previous scope are still retrievable and removable.
     */ 
    public void next()
    {
        removeComposited( this.maps.getFirst() );
        addComposited( new HashMap() );
    }

    public Object put( CompositeMap map, Map[] composited, Object key, Object value )
    {
        if(composited.length < 1)
        {
            throw new UnsupportedOperationException("No composites to add elements to");
        }
        Object result = map.get( key );
        if(result != null)
        {
            map.remove( key );
        }
        composited[composited.length-1].put( key, value );
        return result;
    }

    public void putAll( CompositeMap map, Map[] composited, Map mapToAdd )
    {
        for(Entry entry: (Set<Entry>)mapToAdd.entrySet())
        {
            put(map, composited, entry.getKey(), entry.getValue());
        }
    }

    public void resolveCollision( CompositeMap composite, Map existing, Map added, Collection intersect )
    {
        existing.keySet().removeAll( intersect );       
    }

    @Override
    public String toString()
    {
        return new HashMap(this).toString();
    }


}

用法:

@RequestMapping(value="/login.do", method=RequestMethod.POST)
    public ModelAndView login(@Valid User user){
        ModelAndView mv = new ModelAndView("redirect:result.html");
        if (authService.authenticate(user.getUserName(), user.getPassword()))
            mv.addObject("flashScope.message", "Success");
        //else
            mv.addObject("flashScope.message", "Login Failed");
        return mv;
    }

@RequestMapping(value ="/result.html", method=RequestMethod.GET)
    public ModelAndView result(){
        ModelAndView mv = new ModelAndView("login/loginAction");
        return mv;
    }

在JSP中,用法非常简单:

${flashScope.message}

另外,您需要将FlashScopeInterceptor类配置为拦截器。

<bean id="flashScopeInterceptor" class="x.y.z.FlashScopeInterceptor" />
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
  <property name="interceptors">
    <list><ref bean="flashScopeInterceptor"/></list>
  </property>
</bean>


 类似资料:
  • 我想在JSF应用程序中配置自定义范围。我们在WebSphere8.0/8.5上使用JSF2.0和Primefaces 5.3.17。目前,我们有RequestScope核心和SessionScope模型bean以及命名注释。模型通过注入传递到核心。现在,我们需要提供一种在许多浏览器选项卡上与模型并行工作的方法。我们的想法是使用过滤器将生成的选项卡id注入到响应中,然后,稍后从post请求中提取它,

  • 在尝试为Sublime Text 2编写自己的片段时,我遇到了以下两个问题: > 查找作用域键。我发现我可以逐个查看我的包并找到对声明的“scope”属性的引用。例如,在(我的HTML包中的一个文件)中,有以下两行: 因此,如果我希望我当前的片段在javascript文件上工作,我将我的范围定义为: 我假设所有这些范围键都是根据我安装的软件包即时定义的。Sublime Text是否在我可以更容易引

  • 问题 你想定义一个数组的范围。 解决方案 在 CoffeeScript 中,有两种方式定义数组元素的范围。 myArray = [1..10] # => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] myArray = [1...10] # => [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] 要想反转元素的范围,则可以写成下面这样。 myLargeArray =

  • 我试图https://rpestano.wordpress.com/2013/06/30/cdi-custom-scope/遵循这个指令,但它不起作用,因为我的自定义的方法没有被触发。

  • 我有一个bean,我用它作为传输对象。bean的类定义为- 我正在使用这个bean,在带有Model属性注释的Spring控制器中。对于JSP,我有JSTL。我已经用这样的字段填充了JSP。 当我提交表单时,我得到Java . lang . indexoutofboundsexception:Index:0,Size: 0。

  • 本文向大家介绍JavaScript限定范围拖拽及自定义滚动条应用(3),包括了JavaScript限定范围拖拽及自定义滚动条应用(3)的使用技巧和注意事项,需要的朋友参考一下 两个对象:div1 和 div2,其中div1是div2的父元素,div2只能在div1的范围内拖拽 图中,红点是鼠标的位置,两个绿色箭头相减的结果就是disX,最后oEvent.clientX - disX 就是绿色箭头的