4

I am trying to use OGNL in my tile but it considers the expression as a string ( whatever I use it counts as a string).

How should I change it in a way to count the expression as name of a page not string.

I am using the following code to embed the jsp file name, when admin user is logged in it replace ROLE_ADMIN with the @com.... expression but does not load menuRole_Admin.jsp page. It just shows menuRole_Admin.jsp as a string on browser.

 <put-attribute name="menu" expression="OGNL: 'menu' + 
                         @com.myproject.controller.Default@getRole() + '.jsp'"/> 

tile.xml

 <tiles-definitions>
         ....
        <definition name="register1" extends="baseLayout">
          <put-attribute name="menu" expression="OGNL: 'menu' + 
                      @com.myproject.controller.Default@getRole() + '.jsp'"/> 
            <put-attribute name="body" value="/body.jsp"/>
        </definition>
        ......

basLayout.jsp

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@taglib uri="/struts-dojo-tags" prefix="sx" %>
<html>
    <head>
        ....
    </head>
    <body>
        <div id="container">
            <div id="banner">
                <tiles:insertAttribute name="header" />
            </div>
            <div id="menu">
                <tiles:insertAttribute name="menu"/>
            </div>
            <div id="content">
                <tiles:insertAttribute name="body"/>
            </div>
            <div id="footer">
                <tiles:insertAttribute name="footer" />
            </div>
        </div>  
    </body>
</html>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
       ..........
    <dependencies>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.8</version>
        </dependency>
         <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jsp-2.1-glassfish</artifactId>
            <version>9.1.02.B04.p0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>2.3.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-dojo-plugin</artifactId>
            <version>2.3.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts-taglib</artifactId>
            <version>1.3.10</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.extras</groupId>
            <artifactId>glassfish-embedded-static-shell</artifactId>
            <version>3.1.1</version>
            <scope>system</scope>
            <systemPath>${glassfish.embedded-static-shell.jar}</systemPath>
        </dependency>
        <dependency>
            <groupId>com.kenai.nbpwr</groupId>
            <artifactId>org-apache-commons-dbcp</artifactId>
            <version>1.2.2-201002241055</version>
            <type>nbm</type>
        </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.0.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>3.0.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>3.0.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>3.0.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>3.0.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-dbutils</groupId>
            <artifactId>commons-dbutils</artifactId>
            <version>1.5</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>struts</groupId>
            <artifactId>struts</artifactId>
            <version>1.2.9</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-parent</artifactId>
            <version>3</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>javax.xml</groupId>
            <artifactId>jaxrpc</artifactId>
            <version>1.1</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>fmt</artifactId>
            <version>1.1.2</version>
            <type>tld</type>
        </dependency>
         <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.8.0</version>
        </dependency>
        <dependency>
    <groupId>commons-digester</groupId>
    <artifactId>commons-digester</artifactId>
    <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>net.sf.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>2.3</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-tiles3-plugin</artifactId>
            <version>2.3.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-el</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-ognl</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-api</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-core</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-extras</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-jsp</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-servlet</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>jdom</groupId>
            <artifactId>jdom</artifactId>
            <version>b10</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.6.4</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.2</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
          <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.5.6</version>
        </dependency>
     <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
    <exclusion>
        <groupId>com.sun.jmx</groupId>
        <artifactId>jmxri</artifactId>
    </exclusion>
    <exclusion>
        <groupId>com.sun.jdmk</groupId>
        <artifactId>jmxtools</artifactId>
    </exclusion>
    <exclusion>
            <groupId>javax.jms</groupId>
            <artifactId>jms</artifactId>
    </exclusion>
</exclusions>
</dependency>
            <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.5.6</version>
</dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                   <groupId>javax</groupId>
                                   <artifactId>javaee-endorsed-api</artifactId>
                                   <version>6.0</version>
                                   <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

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">
    <context-param>
        <param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
        <param-value>/WEB-INF/tiles.xml</param-value>
    </context-param>
   <listener>
        <listener-class>org.apache.tiles.extras.complete.CompleteAutoloadTilesListener</listener-
       class>
    </listener>
       <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>
     <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value>
            /WEB-INF/applicationContext.xml
            /WEB-INF/myProject-security.xml 
            /WEB-INF/login-service.xml
        </param-value> 
    </context-param> 
    <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>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-
      class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

applicationContext.xml

<?xml version='1.0' encoding='UTF-8'?> 
<beans xmlns='http://www.springframework.org/schema/beans' 
                xmlns:context='http://www.springframework.org/schema/context' 
                xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
                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'>

    <context:component-scan base-package='com.myProject'/> 
    <bean id='internalResourceResolver' 
                 class='org.springframework.web.servlet.view.InternalResourceViewResolver'> 
        <property name='prefix' value='/Web Pages/'/> 
        <property name='suffix' value='.jsp'/> 
    </bean> 
    <bean class='org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping'/>
    <bean class='org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter'/> 
    <bean id='placeholderConfig' 
                 class='org.springframework.beans.factory.config.PropertyPlaceholderConfigurer'/> 
</beans>

myproject-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns='http://www.springframework.org/schema/security' 
        xmlns:beans='http://www.springframework.org/schema/beans' 
            xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
            xsi:schemaLocation='http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.0.xsd'>
<beans:import resource='login-service.xml'/> 
<http auto-config="true" access-denied-page="/notFound.jsp" use-expressions="true">
    <intercept-url pattern="/search/view*" access="hasRole('ROLE_ADMIN')" /> 
    <form-login login-page="/index" 
                authentication-failure-url="/index?error=1"
                default-target-url="/default"/> 
    <remember-me/>
     <logout logout-success-url="/index.jsp"/>
</http> 
<authentication-manager> 
          ...........

Quaternion commented : I just realized you have Struts2, Spring-MVC AND Struts 1 all included in the same project... that is pretty weird. –

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
J888
  • 1,944
  • 8
  • 42
  • 76
  • 1
    If you are looking for options then consider tiles-3 also tiles 2.2.2 is valid, but if you are going to upgrade to 2.2.2 might as well just migrate to tiles3. Everything should be backwards compatible yet you will be able use expressions within tiles-3 attributes (EL and OGNL). In addition to this you can use wildcard matching in conjunction with regular expression matching. This opens up a lot of options. Your only current option is a view preparer... anyways with tiles three I can show you how to dynamically use a definition and load it with values from your action should you need. – Quaternion May 08 '13 at 04:36
  • Sounds good, how to upgrade it to tile 3? should I just change the version of tile on my pom.xml ? – J888 May 08 '13 at 06:36
  • 1
    Follow this: http://struts.apache.org/development/2.x/docs/tiles-3-plugin.html let me know if you have any issues, it will probably be over a day before I can get back however. – Quaternion May 08 '13 at 07:24
  • 1
    @Quaternion, thanks, I followed the instructions but I cant run the app as it runs into following error, Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.AbstractMethodError: org.apache.tiles.startup.AbstractTilesInitializer.createContainerFactory(Lorg/apache/tiles/TilesApplicationContext;)Lorg/apache/tiles/factory/AbstractTilesContainerFactory;. Please see server.log for more details. – J888 May 09 '13 at 02:02
  • 1
    Can you please create a new question, if possible create a minimal demo... I'll see if I can recreate the issue with your configuration. – Quaternion May 09 '13 at 03:07
  • @Quaternion I have not found any answer here yet. I do not know whether there is any reply for the same question in mailinglist, would you verify it or teach me how to do it? – J888 May 24 '13 at 01:20
  • 1
    There was a reply, someone asked if you had tried appending "/" onto the front of the expression? Simply if you use a plain string that achieves the effect of rendering correctly, and you create a struts2 property tag with the same expression then the tile should render correctly. You are only rendering a String using OGNL... then tiles simply tries to use that String. You'll need to supply more information if more help is to be given. ANOTHER issue is you are using different versions of sturts(1 and 2) please set up a new project from scratch and only include what you need. – Quaternion May 24 '13 at 01:43
  • If you don't know why it is there, don't add it! – Quaternion May 24 '13 at 01:43
  • @Quaternion, ok thanks how do you know I am using both struts1 and 2 ? I do not know how to figure out – J888 May 24 '13 at 02:35
  • @Quaternion what do you mean by If you don't know why it is there, don't add it! – J888 May 24 '13 at 02:36
  • @Quaternion, I have noticed it shows the following message on console while running the application INFO: Tiles2 container initialized , I checked the pom.xml file and find out the only tile related dependancy which its version starts with 2 is struts2-tiles3-plugin which is 2.3.14 how to solve the issue to initialize Tile3 container ? – J888 May 24 '13 at 04:37
  • @Quaternion, Someone's given the correct answer :D – J888 May 24 '13 at 05:58
  • Excellent,with regard to struts1 technology: you are including struts-taglib which is struts1 only. Also I tend to only add top level dependencies... In the case of Mir Moorido's answer I would double check that the later 2 dependencies are required, I think they should be included by default. Didn't check not at dev machine but I would expect it. – Quaternion May 24 '13 at 17:02
  • @Quaternion, so what should I do with the struts-taglib? is there any alternative? – J888 May 26 '13 at 23:25
  • The tag-lib you use with struts2 is part of struts2-core... – Quaternion May 27 '13 at 00:11
  • @Quaternion, i see does that mean I should remove struts-taglib to solve the issue ? is there any other thing that need to be changed ? – J888 May 27 '13 at 00:25

1 Answers1

2

Add / to the begining of your address and remove all the dependencies which include tile in their names except the followings.

<put-attribute name="menu" expression="OGNL: '/menu' + 
                         @com.myproject.controller.Default@getRole() + '.jsp'"/> 

dependencies

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-tiles3-plugin</artifactId>
    <version>2.3.14</version>
</dependency>
<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-el</artifactId>
    <version>3.0.1</version>
</dependency>
<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-ognl</artifactId>
    <version>3.0.1</version>
</dependency>
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243