problems with struts 2 developing login [duplicate] - struts2

This question already has answers here:
"The Struts dispatcher cannot be found" error while deploying application on WebLogic 12.1.3
(2 answers)
Closed 6 years ago.
I have several problems and I would if someone can help me to solve this with Struts 2. This is the problem I have. When I run my simple Login Application I have this mistake:
org.apache.jasper.JasperException: The Struts dispatcher cannot be found.
This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location]
The log is this one:
Advertencia: StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location]
at org.apache.struts2.views.jsp.TagUtils.getStack(TagUtils.java:60)
at org.apache.struts2.views.jsp.StrutsBodyTagSupport.getStack(StrutsBodyTagSupport.java:44)
at org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(ComponentTagSupport.java:48)
at org.apache.jsp.Login_jsp._jspx_meth_s_form_0(Login_jsp.java:99)
at org.apache.jsp.Login_jsp._jspService(Login_jsp.java:72)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:403)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)
The file tree is this one:
src(es.uniway.action.login as a package and inside this class:
LoginAction.java:
package com.uniway.action.login;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
#Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}
As well inside of the src I have the struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="false" />
<package name="default" namespace="/" extends="struts-default">
<action name="Login" class="com.uniway.action.LoginAction">
<result name="input">/Login.jsp</result>
<result name="success" type="redirectAction">/index.jsp</result>
</action>
</package>
<!-- Add packages here -->
</struts>
Then at web-info folder I have the web.xml which I have this thing:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>cloud46</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>Login.jsp</welcome-file>
</welcome-file-list>
</web-app>
And after that at WebContent I have index.jsp and Login.jsp which I have this code:
<%# page contentType="text/html; charset=UTF-8" %>
<%# taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Sign On</title>
</head>
<body>
<s:form action="Login.action">
<s:textfield key="username"/>
<s:password key="password" />
<s:submit method="execute"/>
</s:form>
</body>
</html>
And the libraries at the lib folder inside of the WEB-INF are : asm-3.3.jar, asm-commons-3.3.jar, asm-tree-3.3.jar, commons-fileupload-1.2.2.jar, commons-io-2.0.1.jar, commons-lang3-3.1.jar, freemarker-2.3.19.jar, javassist-3.11.0.GA.jar, ognl-3.0.5.jar
, struts2-core-2.3.3.jar, xwork-core-2.3.3.jar . Please, what I'm doing wrong?

Do this
Change
<action name="Login" class="com.uniway.action.LoginAction">
<result name="input">/Login.jsp</result>
<result name="success" type="redirectAction">/index.jsp</result>
</action>
To
<action name="Login" class="com.uniway.action.LoginAction">
<result name="input">/Login.jsp</result>
<result name="success">/Login.jsp</result>
</action>
Change this
<s:form action="Login.action">
To
<s:form namespace="/" action="Login">
Next
Change
<s:submit method="execute"/>
To
<s:submit value="Login"/>

I had this problem with my 404.jsp page and this was my solution:
We should not use <s: tags in 404.jsp!!!
I don't know why, but this solved my problem!

Related

Include struts 1 and Struts 2 in same web application [duplicate]

I am new to Struts 2 and I have been following a video tutorial on Struts 2(Koushik). I have created the Struts.xml, the action class and JSPs as same as created in the tutorial. But it gives the following exception.
Exception:
Jan 13, 2014 9:30:48 PM org.apache.struts2.dispatcher.Dispatcher warn
WARNING: Could not find action or result: /Struts2Starter/getTutorial.action
There is no Action mapped for namespace [/] and action name [getTutorial] associated with context path [/Struts2Starter]. - [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:552)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
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="default" extends="struts-default">
<action name="getTutorial" class="org.koushik.javabrains.action.TutorialAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Struts2Starter</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>
TutorialAction.java (Action class that I'm using)
package org.koushik.javabrains.action;
public class TutorialAction {
public String execute(){
System.out.println("Hello from execute");
return "success";
}
}
Project structure
success.jsp and error.jsp are normal jsp files with some text. I did a lot of things to resolve this issue by googling. But nothing didn't solve this. Please let me know if anyone knows what's behind this. I highly appreciate it. :)
Renaming Struts.xml naming convention to struts.xml will work.
The message from error clearly says that
no Action mapped for namespace [/] and action name [getTutorial]
associated with context path [/Struts2Starter]
This means that action configuration is not available at runtime. Check out the config-browser plugin to debug configuration issues.
To map correctly url to the action two parameters are required: the action name and namespace.
Struts loads xml configuration on startup and it should know the location of the struts.xml. By default it's looking on classpath to find a file with known name like struts.xml. Then it parses document and creates a runtime configuration. This configuration is used to find appropriate configuration element for the action url. If no such element is found during request, the 404 error code is returned with the message: There is no Action mapped for namespace and action name.
Also this message contains a namespace and action names used to find the action config. Then you can check your configuration settings in struts.xml. Sometimes the action name and namespace, stored in ActionMapping point to the wrong action. These values are determined by the ActionMapper which could have different implementation, used by plugins.
There's also another setting that might affect this mapper and mapping, but the point is the same if you get this message then URL is used didn't map any action config in runtime configuration. If you can't realize which URL you should use, you might try config-browser plugin to see the list of URLs available to use.
change your Struts.xml and put <constant name="struts.devMode" value="true" /> in struts.xml Development mode or devMode it enables extra debugging and helps indebugging
try below code
<?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>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="getTutorial" class="org.koushik.javabrains.action.TutorialAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
U just need to check that action name is constant everywhere...i also had same problem i resolved by removing namespace as it is not necessary which i see u have not mentioned and also i had different action name at loginpage.jsp and struts.xml page.. so just see ur action name
Your JSP file should not be under the WEB-INF folder, but should be under the web folder. I've attached an image to show you the right hierarchy.
Folder System

Call action in web.xml file in Struts 2.3

this is default implementation.
<welcome-file-list>
<welcome-file>Index.jsp</welcome-file>
</welcome-file-list>
i want to change like below
<welcome-file-list>
<welcome-file>login.action</welcome-file>
</welcome-file-list>
<action name="login" class="com.mm.authentication.Login">
<result name="success">/Index.jsp</result>
above implementation not working.
if anyone know how to do this, please let me know.
The welcome file is just a file. You have to rename it to something login.jsp. But create a new file index.jsp with the following content
<% response.sendRedirect(request.getContextPath()+"login.action"); %>
The action configuration
<action name="login" class="com.mm.authentication.Login">
<result name="success">/login.jsp</result>

Struts2: can't call action before rendering index.jsp

I've searched for this all over SO, but didn't find a similar situation with the same symptoms. I'm using Sruts2 and trying to invoke an action before rendering the main page of my application (index.jsp). I truly believe that this action is not being called, because I have a System.out.println() (for debugging purposes) in the beginning of the execute() method of the action that is not being printed. Indeed index.jsp is being presented (because it is the default page), but the part that is related to the action is not being run. In conclusion, I think that the problem may reside in the struts.xml file. Below are both the struts.xml and the action file:
Struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- The core configuration file for the framework is the default (struts.xml) file
and should reside on the classpath of the webapp (generally /WEB-INF/classes). -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- devMode equals debug information and reload everything for every request -->
<constant name="struts.devMode" value="true" />
<constant name="struts.ui.theme" value="simple" />
<package name="faultinjector" extends="struts-default">
<default-action-ref name="loadexperiments" />
<action name="loadexperiments" class="faultinjector.action.LoadExperimentsAction" method="execute">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
LoadExperimentsAction.java:
public class LoadExperimentsAction extends ActionSupport
{
private static final long serialVersionUID = 4L;
private ExperimentService service;
private List <Experiment> experiments;
#Override
public String execute()
{
System.out.println("Hello!");
return SUCCESS;
}
public ExperimentService getService()
{
return service;
}
public void setService(ExperimentService service)
{
this.service = service;
}
public List<Experiment> getExperiments()
{
return experiments;
}
public void setExperiments(List<Experiment> experiments)
{
this.experiments = experiments;
}
}
I finally managed to achieve what I was looking for, without the need of redirecting pages. Basically the solution is to keep my original configuration, but changing the JSP page name from "index.jsp" to another one (in my case, it is now called "user_main".jsp). This way, it oddly works, I guess it has some hidden default configuration parameter when using the default name "index.jsp". Content of my configuration files:
web.xml:
<web-app id="faultinjector" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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>
</web-app>
struts.xml:
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.ui.theme" value="simple" />
<package name="faultinjector" extends="struts-default">
<default-action-ref name="loadexperiments" />
<action name="loadexperiments" class="faultinjector.action.LoadExperimentsAction" method="execute">
<result name="success">/user_main.jsp</result>
</action>
</package>
</struts>

Struts 2 Portlet - redirectAction not working, doView() not called

I'm trying to create a simple Struts 2 JSR 286 Portlet, running on WebSphere Portal 7. I'm able to display a simple JSP that contains a link which calls an action and displays another JSP which accepts input. That works fine.
I start to run into problems, however, when I try to use a redirectAction. I don't see any error messages, but the redirect doesn't seem to work. The Portlet just shows a blank page.
In debugging this I noticed that the doView method of my Portlet class is never called, which seems very suspicious.
If anyone has experience developing Struts 2 Portlets on WebSphere Portal, I would appreciate some help in checking that my config files are correct. Have I missed something?
Here are the details:
WebSphere Portal 7.0.0.2
WebSphere Application Server 7.0.0.25
RAD 8.0.4
Struts 2.3.14.2
Windows 7
portlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<portlet-app id="com.ibm.demo.jsr286.TopUpPortlet.72594d5fe3"
version="2.0"
xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd">
<portlet>
<description xml:lang="EN">Demo JSR 286 Struts Portlet</description>
<portlet-name>Demo Portlet</portlet-name>
<display-name>Demo Portlet</display-name>
<!-- DemoPortlet extends org.apache.struts2.portlet.dispatcher.Jsr286Dispatcher -->
<portlet-class>com.demo.jsr286.DemoPortlet</portlet-class>
<init-param>
<name>viewNamespace</name>
<value>/view</value>
</init-param>
<init-param>
<name>defaultViewAction</name>
<value>index</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
</supports>
<supported-locale>en</supported-locale>
<portlet-info>
<title>Demo Portlet</title>
<short-title>DemoPortlet</short-title>
<keywords>Demo Portlet</keywords>
</portlet-info>
</portlet>
<default-namespace>http://JSR286StrutsDemo/</default-namespace>
</portlet-app>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
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_2_5.xsd">
<display-name>JSR 286 Struts Demo</display-name>
<servlet id="Struts2PortletDispatcherServlet">
<servlet-name>Struts2PortletDispatcherServlet</servlet-name>
<servlet-class>org.apache.struts2.portlet.dispatcher.DispatcherServlet</servlet-class>
</servlet>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
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>
<include file="struts-plugin.xml"/>
<package name="view" extends="struts-portlet-default" namespace="/view">
<!-- This action works -->
<action name="index">
<result>/html/view/index.jsp</result>
</action>
<!-- This action works -->
<action name="startDemo">
<result>/html/view/demo.jsp</result>
</action>
<!-- This action does not work -->
<action name="redirectToIndex">
<result type="redirectAction">
<param name="actionName">index</param>
<param name="namespace">/view</param>
<param name="portletMode">view</param>
</result>
</action>
</package>
</struts>
* Update *
I've narrowed the problem down slightly. It looks like the action is being interpreted as a file location rather than a struts action. So, when I call action "redirectToIndex" it tries to display a page called "/view/index.action". I verified this by creating a file with that path and sure enough, the contents of that file are displayed in the portlet.
I feel that I'm probably missing some configuration option, but I'm not sure what. Servlet filter maybe? Can anyone help?
Actually you don't need doView method, because Jsr286Dispatcher is just a dispatcher. You can use actions like in ordinary Struts2 application.
From the documentation:
The portlet-class element is always org.apache.struts2.portlet.dispatcher.Jsr168Dispatcher (or a subclass, if you have added some custom functionality). This is the portlet that acts as the dispatcher for the Struts 2 framework, and translates incoming user interaction to action requests that Struts 2 understands.
For jsr286 specification <portlet-class> should be org.apache.struts2.portlet.dispatcher.Jsr286Dispatcher and defaultViewAction init-param will call Struts2 action. And in struts.xml file, like usually, you can define action class + method to call.
So you need to define Jsr286Dispatcher as <portlet-class> and create action which you will use in struts.xml action definitions.
Also see this two links: http://struts.apache.org/development/2.x/docs/struts-2-portlet-tutorial.html and http://struts.apache.org/development/2.x/docs/portlet-plugin.html.

OGNL and wildcards working in tiles definitions with struts2-tiles-plugin?

I want Tiles to resolve ognl from the struts2 value stack. How to do this?
Using tiles 2.2.2 (although if a later version such as 3 could be used that is fine)
Here it mentions the new feature:
http://tiles.apache.org/2.2/framework/whats-new.html
Here it shows how to implement it: http://tiles.apache.org/2.2/framework/tutorial/advanced/el-support.html#OGNL_Support
But I'm not certain how to go about that in my project. Does anyone have tiles3 working in their struts2 project? I remember reading about some way to turn on all new features in tiles 3 by default but I can't find a link to that page.
If configuration can be done in anyway with spring that is fine (if that helps, if not it is not an issue).
I love the tiles tempate system because it is simple and straight forward, with Tiles 2.2.2 you can have wild card support to push your struts2 conventions right into the template system and now there is OGNL too!
Steps to add tiles 2.2.2 with OGNL support within tiles (Currently using Struts 2.3.1.2):
1) Add the struts2-tiles-plugin
2) Add the tiles 2.2.2 jars including the jars which add ognl support. Assumes maven: ALL group ids are: org.apache.tiles ALL versions are 2.2.2, the following are artifact ids :
tiles-api
tiles-core
tiles-extras
tiles-jsp
tiles-ognl
tiles-servlet
Also needed to add the following:
log4j, log4j, 1.2.14 (Should already be part of the basic setup: http://struts.apache.org/2.x/docs/create-struts-2-web-application-using-maven-to-manage-artifacts-and-to-build-the-application.html)
slf4j-api, org.slf4j, 1.6.4
slf4j-simple, org.slf4j, 1.6.4
By using the org.apache.tiles.extras.complete.CompleteAutoloadTilesListener you get Wild Cards, EL, OGNL, MVEL support in your tiles.xml
Example web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<listener>
<listener-class>org.apache.tiles.extras.complete.CompleteAutoloadTilesListener</listener-class>
</listener>
<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>
</web-app>
struts.xml for a demo: The demo uses OGNL from struts to display a hard coded greeting message. Three action mappings exist "", "test", and "package/*" the value that follows the slash will be used by tiles to set a new title. A very trivial example but does show some useful features.
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.ui.theme" value="simple" />
<constant name="struts.enable.SlashesInActionNames" value="true" />
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
<package name="basicstruts2" namespace="" extends="tiles-default">
<result-types>
<result-type name="tiles" default="true" class="org.apache.struts2.views.tiles.TilesResult"/>
</result-types>
<action name="/**" class="com.kenmcwilliams.tiles.action.Test">
<result>{1}</result>
</action>
</package>
</struts>
tiles.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
"http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
<definition name="baseLayout" template="/WEB-INF/content/tiles/template.jsp">
<put-attribute name="title" value="Default Title"/>
<put-attribute name="header" value="/WEB-INF/content/tiles/header.jsp"/>
<put-attribute name="body" value="/WEB-INF/content/tiles/body.jsp"/>
<put-attribute name="footer" value="/WEB-INF/content/tiles/footer.jsp"/>
<put-attribute name="variable" expression="OGNL:greeting"/>
</definition>
<definition name="test" extends="baseLayout">
<put-attribute name="title" value="Test Title"/>
</definition>
<definition name="WILDCARD:package/*" extends="baseLayout">
<put-attribute name="title" value="{1}" type="string"/>
</definition>
<definition name="" extends="baseLayout">
</definition>
</tiles-definitions>
/WEB-INF/content/tiles/body.jsp
<div>
This is the default body.
</div>
/WEB-INF/content/tiles/footer.jsp
<div>
This is the default footer.
</div>
/WEB-INF/content/tiles/header.jsp
<div>
This is the default header.
</div>
/WEB-INF/content/tiles/template.jsp
<%#taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<%#taglib prefix="s" uri="/struts-tags"%>
<%#page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><tiles:insertAttribute name="title"/></title>
</head>
<body>
<tiles:insertAttribute name="header"/>
<tiles:insertAttribute name="body"/>
<tiles:insertAttribute name="footer"/>
<tiles:insertAttribute name="variable"/>
</body>
</html>
Test.java Action
package com.kenmcwilliams.tiles.action;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;
import java.util.logging.Logger;
import org.apache.struts2.interceptor.SessionAware;
public class Test extends ActionSupport implements SessionAware{
private static final Logger log = Logger.getLogger(Test.class.getName());
#Override
public String execute(){
log.info("Test execute");
return SUCCESS;
}
private String greeting = "Hello, World from Action!";
public String getGreeting(){
return greeting;
}
#Override
public void setSession(Map<String, Object> session) {
session.put("greeting", "Greetings from session!");
}
}
That should now be enough to get tiles working for your application.

Resources