2013-05-08 38 views
4

我想在我的瓷砖中使用OGNL,但它认为表达式是一个字符串(无论我使用它作为一个字符串计算)。瓷砖无法正常使用OGNL表达式

我应该如何改变它以计算表达式作为页面名称而不是字符串。

我使用下面的代码来嵌入jsp文件名称,当admin用户登录它时,用@com ....表达式替换ROLE_ADMIN,但不加载menuRole_Admin.jsp页面。它只是在浏览器上显示menuRole_Admin.jsp作为字符串。

<put-attribute name="menu" expression="OGNL: 'menu' + 
         @[email protected]() + '.jsp'"/> 

tile.xml

<tiles-definitions> 
     .... 
     <definition name="register1" extends="baseLayout"> 
      <put-attribute name="menu" expression="OGNL: 'menu' + 
         @[email protected]() + '.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> 
      ........... 

四元说:我只是意识到你有Struts2的, Spring-MVC和Struts 1都包含在同一个项目中......这很奇怪。 -

+1

如果你正在寻找的选项,然后考虑瓷砖-3也瓷砖2.2.2是有效的,但如果你要升级到2.2.2干脆迁移到tiles3。一切都应该向后兼容,但你可以使用tiles-3属性(EL和OGNL)中的表达式。除此之外,您还可以将通配符匹配与正则表达式匹配结合使用。这开辟了很多选择。您唯一的当前选择是视图编写器...反正有三个视图我可以告诉您如何动态地使用定义并根据需要加载来自您的动作的值。 – Quaternion 2013-05-08 04:36:21

+0

听起来不错,如何将它升级到3瓦?我应该只更改我的pom.xml中的磁贴版本吗? – J888 2013-05-08 06:36:23

+1

按照这个:http://struts.apache.org/development/2.x/docs/tiles-3-plugin.html让我知道如果你有任何问题,它可能会超过一天,我可以回来然而。 – Quaternion 2013-05-08 07:24:32

回答

2

将/添加到您的地址的开头,并删除除以下内容之外的名称中包含tile的所有依赖项。

<put-attribute name="menu" expression="OGNL: '/menu' + 
         @[email protected]() + '.jsp'"/> 

依赖

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