strut2笔记

1:plugin-tiles在jdk1.4下面有问题,ServletContextListener重写一下可以解决问题。代码如下:(注意使用 RETROTRANSLATOR转换jdk5 编译的包时需要指明classpath="jdk1.4/rt.jar的路径")
https://issues.apache.org/struts/browse/WW-2897
package co.ntelagent.client.web.ps.application;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.tiles.ConfiguredServletContext;
import org.apache.struts2.tiles.StrutsTilesContainerFactory;
import org.apache.tiles.TilesContainer;
import org.apache.tiles.TilesException;
import org.apache.tiles.access.TilesAccess;
import org.apache.tiles.factory.TilesContainerFactory;
public class MyStrutsTilesListener implements ServletContextListener
{
    /**
     * Log instance.
     */
    protected static final Log LOG =
        LogFactory.getLog(MyStrutsTilesListener.class);
    private static final Map INIT;
    static
    {
        INIT = new HashMap();
        INIT.put(TilesContainerFactory.CONTAINER_FACTORY_INIT_PARAM,
                 StrutsTilesContainerFactory.class.getName());
    }
    /**
     * Initialize the TilesContainer and place it
     * into service.
     *
     * @param event The intercepted event.
     */
    public void contextInitialized(ServletContextEvent event)
    {
        ServletContext servletContext = event.getServletContext();
        try
        {
            TilesContainer container = createContainer(servletContext);
            TilesAccess.setContainer(servletContext, container);
        }
        catch (TilesException e)
        {
            throw new IllegalStateException("Unable to instantiate container.");
        }
    }
    /**
     * Remove the tiles container from service.
     *
     * @param event The intercepted event.
     */
    public void contextDestroyed(ServletContextEvent event) {
        ServletContext servletContext = event.getServletContext();
        try
        {
            TilesAccess.setContainer(servletContext, null);
        }
        catch (TilesException e) {
            LOG.warn("Unable to remove tiles container from service.");
        }
    }
    /**
     * Creates a Tiles container.
     *
     * @param context The servlet context to use.
     * @return The created container
     * @throws TilesException If something goes wrong during creation.
     */
    protected TilesContainer createContainer(ServletContext context)
        throws TilesException
        {
        if(context.getInitParameter(TilesContainerFactory.CONTEXT_FACTORY_INIT_PARAM) == null) {
            context = decorate(context);
        }
        else
        {
            LOG.warn("Tiles container factory is explicitly set.  Not injecting struts configuration.");
        }
        TilesContainerFactory factory =
            TilesContainerFactory.getFactory(context);
        return factory.createContainer(context);
    }
    protected ServletContext decorate(ServletContext context)
    {
        return new ConfiguredServletContext(context, INIT);
    }
}
2:plugin-tiles此时还不支持velocity视图,但支持freemarker(必须以ftl做扩展名)和jsp混用
3:action 配置后不需要明确指明method,使用时直接指向这个action,再指定method名称即可,调用时是只调用method方法。注意如果form指明了action,那么这个form的excute会执行,但是不会影响其内部元素的method的最终结果(而且会抛异常),这样不好,一般不要再 form上指明action


Total views.

© 2013 - 2024. All rights reserved.

Powered by Hydejack v6.6.1