Tiles Integration with Struts 2 Annotation

ε祈祈猫儿з 提交于 2019-12-08 00:55:35

问题


I have been trying to integrate Tiles with Struts 2 annotation based action but it's not working correctly.

As I don't have struts-config.xml and in every tutorial available at web they are referencing it with struts-config.xml.

First is it possible to integrate annotation based struts action with tiles. If yes then how?

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

This is what my code is but it always gives me error in MyEclipse at TilesResult.class that

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

I have dependency in my pom:

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

Can anyone help me how to add tiles in annotation based actions


I used type="tiles" instead of type=TilesResult.class then it has given me below exception

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)

回答1:


Try these :

  1. Use type="tiles" instead of type="TilesResult.class"

  2. Use your target tile definition, location="tiles-definition-name", instead of JSP page, location="/jsp/userLogin.jsp", in your result location

  3. Have following in your 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. Have following in your struts.xml (If you are using annotations alone and no struts.xml, then you have to create a minimal one for this because there's no annotation available to define a custom result type)

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

NOTE: I've written a detailed blog post here on this issue - Maven, Struts2 Annotations and Tiles Integration Example via Convention / Codebehind / Zero Config plugin using Eclipse IDE




回答2:


  1. "org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG" this definition is not available with Strut 2.5.10.1
  2. I have used following jars in my project.
    • struts2-core-2.5.10.1
    • struts2-convention-plugin-2.5.10.1
    • struts2-tiles-plugin-2.5.10.1
    • javax.servlet-api-3.0.1

Please once you compare your web.xml with following code.

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


来源:https://stackoverflow.com/questions/17767895/tiles-integration-with-struts-2-annotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!