`
sillycat
  • 浏览: 2552006 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

tiles2的布局管理(一)spring+struts2+tiles2

    博客分类:
  • JAVA
阅读更多
tiles2的布局管理(一)spring+struts2+tiles2

official web site
http://tiles.apache.org/

the document is here
http://tiles.apache.org/framework/index.html

The Composite View Pattern
Using the composite view pattern, the other part of the page have been reused, and the layout consistence has been preserved.

Tiles is a composite view framework, it allows to reuse page pieces across the application. But another approach to achive the same result is using the Decorator pattern. For example, Sitemesh is based on the Decorator pattern.

Configuring Tiles
I use spring to integrate struts2 to work as command actions. The configuration file web.xml:
<context-param>
<param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
<param-value>/WEB-INF/tiles.xml</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:main-context.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
  <listerner-class>org.apache.struts2.tiles.StrutsTilesListener</listerner-class>
</listener>

the tiles configuration file tiles.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
        "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
        "http://struts.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>  
    <definition name="base.definition" template="/layout/layout.jsp"> 
        <put-attribute name="title"  value="当前客户起始页面"/> 
        <put-attribute name="banner"  value="/content/top.jsp"/> 
        <put-attribute name="menu"  value="/content/menu.jsp"/> 
        <put-attribute name="sidebar"  value="/content/sidebar.jsp"/> 
        <put-attribute name="hintbar"  value="/content/error.jsp"/> 
        <put-attribute name="body"  value="/screen/body.jsp"/> 
    </definition> 
     
    <definition name="index.definition" extends="base.definition">  
        <put-attribute name="body" value="/screen/index.jsp"/>  
    </definition>
     
</tiles-definitions>  

we defined one template here named base.definition and every time we can use <put-attribute> to replace one part to form a new page.

the struts2 configuration file struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="testp" namespace="/testn" extends="tiles-default">
<action name="hello" method="hello" class="helloAction">
<result name="hello" type="tiles">base.definition</result>
</action>
<action name="ads" method="ads" class="helloAction">
<result name="hello" type="tiles">index.definition</result>
</action>
</package>
</struts>
If we call the method in action, we will render to different tiles template page.

the struts.properties file here is just the same as in spring+struts2:
struts.i18n.encoding=utf-8
#spring configuration
struts.objectFactory=spring
#dev mode flag
struts.devMode=true
struts.action.extension=do

ok, every thing goes well and We can try the urls index.jsp:
you can see this page<br />
<a href="testn/testp/hello.do">hello</a><br />
<a href="testn/testp/ads.do">ads</a><br />

Problem One:
Struts Problem Report

Struts has detected an unhandled exception:

Messages:
File: org/apache/struts2/views/tiles/TilesResult.java
Line number: 105
Stacktraces

java.lang.NullPointerException
    org.apache.struts2.views.tiles.TilesResult.doExecute(TilesResult.java:105)
    org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
    com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:362)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:266)
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)

Solution:
check out the java source file TilesResult.java and view the codes:
TilesContainer container = TilesAccess.getContainer(servletContext);
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
container.render(location, request, response);

the problem is here, the contrainer is null from the serveletContext, and when the web start, we just put the container in listener.but I don't know why, but the  org.apache.struts2.tiles.StrutsTilesListener never be called in web.xml, so that may be the problem that we can not have two listeners in one web.xml. So I just recode all the method and function in startupListener.java extends ContextLoaderListener, I just used to do this in spring2.5.6 and spring mvc controller,and it works well.
but here, since we use spring 3.0 + struts2, it still cause problem.

Problem Two:
严重: ********** FATAL ERROR STARTING UP STRUTS-SPRING INTEGRATION **********
Looks like the Spring listener was not configured for your web app!
Nothing will work until WebApplicationContextUtils returns a valid ApplicationContext.
You might need to add the following to web.xml:
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
2010-7-13 17:09:32 com.opensymphony.xwork2.util.logging.commons.CommonsLogger error
严重: Dispatcher initialization failed
java.lang.NullPointerException
at com.opensymphony.xwork2.spring.SpringObjectFactory.getClassInstance(SpringObjectFactory.java:209)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyResultType(XmlConfigurationProvider.java:519)

it seems that the xwork2.spring.SpringObjectFactory just knows to find org.springframework.web.context.ContextLoaderListener,even my StartupListener is extending the ContextLoaderListener, it is really silly. So I just have to code a new java file  org.springframework.web.context.ContextLoaderListener with the same name and same package.But I add the tiles codes there:
package org.springframework.web.context;

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;

@SuppressWarnings("unchecked")
public class ContextLoaderListener extends ContextLoader implements
ServletContextListener {
protected static final Log LOG = LogFactory
.getLog(ContextLoaderListener.class);
private ContextLoader contextLoader;
private static final Map INIT;
static {
INIT = new HashMap();
INIT.put(TilesContainerFactory.CONTAINER_FACTORY_INIT_PARAM,
StrutsTilesContainerFactory.class.getName());
}

public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
if (this.contextLoader == null) {
this.contextLoader = this;
}
this.contextLoader.initWebApplicationContext(event.getServletContext());
ServletContext servletContext = event.getServletContext();
try {
TilesContainer container = createContainer(servletContext);
TilesAccess.setContainer(servletContext, container);
} catch (TilesException e) {
throw new IllegalStateException("Unable to instantiate container.");
}
}

@Deprecated
protected ContextLoader createContextLoader() {
return null;
}

@Deprecated
public ContextLoader getContextLoader() {
return this.contextLoader;
}

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.");
}
if (this.contextLoader != null) {
this.contextLoader.closeWebApplicationContext(event
.getServletContext());
}
ContextCleanupListener.cleanupAttributes(event.getServletContext());
}

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);
}
}

then it works fine.
分享到:
评论

相关推荐

    spring+struts+hibernate框架

    同时,Spring的事务管理可以跨多个服务(包括Struts的Action和Hibernate的DAO)进行统一管理,确保了事务的一致性。 在实际项目中,SSH框架通常结合其他技术,如JSF、AJAX、Tiles等,以增强用户体验和页面动态性。...

    spring+struts1+hibernate+jbpm实例

    标题中的"spring+struts1+hibernate+jbpm实例"揭示了这是一个基于Java技术栈的Web应用程序开发实例,其中涉及四大核心技术:Spring、Struts1、Hibernate和JBPM。接下来,我们将深入探讨这些技术及其在实际项目中的...

    Spring + Struts +Hibernate简单学习源码

    Spring、Struts和Hibernate是Java开发中非常经典的三大框架,它们各自负责应用程序的不同层面:Spring作为全面的容器和依赖注入框架,负责管理对象和提供事务处理;Struts则是一个MVC(模型-视图-控制器)框架,主要...

    hibernate+spring+struts2

    (2)运用struts1.2+hibernate+spring 框架,数据库连接池,事务管理; (3)Struts 应用国际化,Struts 标签库与Tiles框架, JSTL标签库,Spring IOC; (4)采用优化性能技术,采用oscache缓存,freemarker静态页面生成; (5)...

    struts2.2+velocity+tiles+spring3+mybatis3.05整合

    项目整合完成后,开发者可以利用这个框架快速构建功能丰富的Web应用,同时享受到各组件带来的优势,如Struts2的控制层灵活性、Velocity的模板渲染能力、Tiles的页面布局管理、Spring的全面服务以及MyBatis的数据库...

    jpetstore4.0 (spring+struts+ibatis)

    2. **视图管理**:Struts的Tiles框架允许组合不同的JSP页面来创建复杂的视图,提高了界面的可复用性和可维护性。 3. **业务逻辑封装**:Action类作为业务逻辑的载体,调用Service层方法,处理请求并返回结果。 **...

    spring+struts9篇技术文档

    - **插件扩展**:Struts 社区提供了大量插件,如Tiles、Struts Tiles2等,增强了布局和展示能力。 3. **Spring 和 Struts 集成**: - **Spring 作为依赖注入容器**:Spring 可以管理Struts中的Action和Service类...

    Struts1+Spring2+Hibernate2整合详细例子

    5. **集成Struts1和Spring2**:使用Spring的Struts插件,将Spring管理的Bean注入到Struts的Action中,实现依赖注入。 6. **集成Spring2和Hibernate2**:在Spring的配置文件中,配置Hibernate的SessionFactory,并...

    Spring + struts2

    Spring的视图解析器(如Velocity或Tiles)可以与Struts2配合,实现更复杂的视图结构和模板复用。 6. **测试和优化**:整合后的系统需要进行充分的单元测试和集成测试,确保各个组件协同工作。此外,还可以根据需求...

    基于Spring+Struts+Hibernate的学生课程管理系统 数据库用SQL Server 2005

    《基于Spring+Struts+Hibernate的学生课程管理系统及SQL Server 2005数据库应用详解》 在现代软件开发中,企业级应用框架的使用已成为常态,以提高开发效率和系统稳定性。Spring、Struts和Hibernate这三大框架的...

    Hibernate+spring+struts2分页

    "spring07"可能包含了一些关于Spring框架的第七个版本相关的配置文件或代码示例,这些内容可能涉及Spring的事务管理、AOP配置、数据源配置等,有助于更深入地理解如何在Spring环境中整合Hibernate和Struts2实现分页...

    Spring + Struts +Hibernate

    在SSH框架集成的项目中,Spring通常作为核心,负责管理其他组件,包括Struts的Action和Hibernate的SessionFactory。Struts处理HTTP请求,调用由Spring管理的业务服务层,这些服务又通过Hibernate与数据库进行交互。...

    ssm整合例子(spring3 + struts2 + mybatis3 + tiles + dwr3注解)

    整合spring3 + struts2 + mybatis3 + tiles + dwr3 这几个流行的框架。 spring3,struts2,dwr3都是用的注解,tiles与mybatis是采用的xml配置。

    Hibernate+Spring+Struts2+extjs开发的图文管理系统,完美运行

    一个典型的案例是使用Hibernate、Spring、Struts2和ExtJS这四大技术栈来构建一个图文管理系统。这个系统通常具备强大的数据持久化、依赖注入、MVC架构以及丰富的前端交互特性,从而实现高效且用户友好的功能。 ...

    spring+struts集成学习笔记和项目源码

    3. **Tiles**:Struts 提供了Tiles插件,允许开发者创建可重用的页面布局,提高了视图设计的灵活性。 **SSH集成**: Spring 和 Struts 的集成通常通过Spring的Web MVC实现,Spring作为控制层,负责业务对象的管理和...

    spring+struts+hibernate(简单入门实例)

    - **Spring管理Struts和Hibernate**:Spring可以作为容器来管理和初始化Struts的Action及Hibernate的SessionFactory,实现统一的事务管理。 - **DispatcherServlet**:Spring MVC的前端控制器,负责拦截所有HTTP...

    Struts+Spring+Hibernate+Ajax的Demo

    通过Struts处理请求,Spring管理依赖,Hibernate处理数据,Ajax提供异步交互,DWR和Dojo作为Ajax的工具库,共同构建出一个高效、动态的应用程序。这样的架构设计使得代码结构清晰,可维护性强,同时也提供了优秀的...

    Spring+struts+hibernate的整合.

    为了在项目中使用 Struts,你需要添加 Struts 的 JAR 包到项目的类路径中,包括 struts-core、struts-tiles、struts-taglib 等。同时,你需要配置 Struts 的核心配置文件 `struts-config.xml`,在这个文件中定义 ...

    Spring+Struts+Hibernate

    Struts提供了一组控制器组件和可重用的UI组件,如ActionForm、Action、Tiles等,使得页面跳转和请求处理更加规范和便捷。 **J2EE**(Java 2 Platform, Enterprise Edition)是Java平台的一个版本,专为构建分布式、...

Global site tag (gtag.js) - Google Analytics