与Struts 2注释的Tiles集成

时间:2013-07-21 00:10:59

标签: struts2 annotations tiles tiles2

我一直在尝试将Tiles与基于Struts 2注释的操作集成,但它无法正常工作。

由于我没有struts-config.xml,并且在网络上提供的每个教程中,他们都使用struts-config.xml引用它。

首先,可以将基于注释的struts动作与切片集成。如果是,那么如何?

@Action(value="/login",results={@Result(name="success",location="/home",type=TilesResult.class),
            @Result(name="login",location="/jsp/userLogin.jsp")})
    public String execute() {

这是我的代码,但它始终在TilesResult.class的MyEclipse中给我错误

Type mismatch: cannot convert from Class<TilesResult> to String

我对我的pom依赖:

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-tiles-plugin</artifactId>
    <version>2.1.8</version>
</dependency>

任何人都可以帮助我如何在基于注释的操作中添加切片


我使用type="tiles"代替type=TilesResult.class,然后它给了我以下异常

Caused by: The Result type [tiles] which is defined in the Result annotation on the class [class com.actions.LoginAction] or determined by the file extension or is the default result type for the PackageConfig of the action, could not be found as a result-type defined for the Struts/XWork package [com.actions#convention-default#] - [unknown location]
    at org.apache.struts2.convention.DefaultResultMapBuilder.createResultConfig(DefaultResultMapBuilder.java:422)
    at org.apache.struts2.convention.DefaultResultMapBuilder.createFromAnnotations(DefaultResultMapBuilder.java:394)
    at org.apache.struts2.convention.DefaultResultMapBuilder.build(DefaultResultMapBuilder.java:202)
    at org.apache.struts2.convention.PackageBasedActionConfigBuilder.createActionConfig(PackageBasedActionConfigBuilder.java:800)
    at org.apache.struts2.convention.PackageBasedActionConfigBuilder.buildConfiguration(PackageBasedActionConfigBuilder.java:586)
    at org.apache.struts2.convention.PackageBasedActionConfigBuilder.buildActionConfigs(PackageBasedActionConfigBuilder.java:318)
    at org.apache.struts2.convention.ClasspathPackageProvider.loadPackages(ClasspathPackageProvider.java:53)
    at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:204)
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:55)

2 个答案:

答案 0 :(得分:4)

试试这些:

  1. 使用type="tiles"代替type="TilesResult.class"

  2. 在结果位置使用目标图块定义location="tiles-definition-name",而不是JSP页面location="/jsp/userLogin.jsp"

  3. 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> <listener> <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class> </listener>

  4. struts.xml中有以下内容(如果您单独使用注释而没有struts.xml,那么您必须为此创建一个最小的注释,因为没有可用于定义自定义结果的注释型)

    <struts> <constant name="struts.convention.default.parent.package" value="codeoftheday.blogspot.com"/> <package name="codeoftheday.blogspot.com" extends="struts-default"> <result-types> <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" /> </result-types> </package> </struts>

  5. 注意:我已就此问题撰写了详细的博客文章 - Maven, Struts2 Annotations and Tiles Integration Example via Convention / Codebehind / Zero Config plugin using Eclipse IDE

答案 1 :(得分:0)

  1. &#34; org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG&#34; Strut 2.5.10.1
  2. 不提供此定义
  3. 我在我的项目中使用了以下jar。
    • struts2的核 - 2.5.10.1
    • struts2的-公约-插件-2.5.10.1
    • struts2的瓷砖-插件-2.5.10.1
    • 的javax.servlet-API-3.0.1
  4. 请将您的web.xml与以下代码进行比较。

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
            <param-name>actionPackages</param-name>
            <param-value>com.demo.action</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <context-param>
        <!-- <param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name> -->
        <param-name>org.apache.tiles.definition.DefinitionsFactory.DEFINITIONS_CONFIG</param-name>
        <param-value>/WEB-INF/config/tiles.xml</param-value>
    </context-param>
    
    <listener>
        <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
    </listener>