How to integrate Struts 2 with Tiles 3 - struts2

How do we integrate Struts 2 with Tiles 3? The struts2-tiles-plugin currently (2.3.4.1) works with an older version of tiles (version 2.0.6) this can be a bit of a nuisance.
This is a self-answer, to help others with their integration.

The solution is to add the required dependencies, load tiles with an appropriate listener and create a custom result type. Fortunately these steps are quite easy, once done you can follow normal tiles 2 examples for a how to define templates.
1) Dependencies (start with basic struts project but in this example I'll use conventions so it might just be best to add struts2-conventions-plugin, it will include struts2-core et al):
DO NOT INCLUDE struts2-tiles-plugin
groupId: org.apache.tiles, artifiactId: tiles-extras, version: 3.0.1
groupId: org.slf4j, artifiactId: jcl-over-slf4j, version: 1.5.8
groupId: org.slf4j, artifiactId: slf4j-jdk14, version: 1.5.8
Note: A higher version of the slf4j dependencies may work I have not tested this.
2) load tiles with an appropriate listener
This example includes the full web.xml, lines 3-5 are the only thing that should be new to someone familiar with struts2.
<?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>
3) create a custom result type
We need to define a custom result type for use with our actions:
package com.quaternion.result;
import com.opensymphony.xwork2.ActionInvocation;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.ServletDispatcherResult;
import org.apache.tiles.TilesContainer;
import org.apache.tiles.access.TilesAccess;
import org.apache.tiles.request.ApplicationContext;
import org.apache.tiles.request.servlet.ServletRequest;
import org.apache.tiles.request.servlet.ServletUtil;
public class TilesResult extends ServletDispatcherResult {
public TilesResult() {
super();
}
public TilesResult(String location) {
super(location);
}
#Override
public void doExecute(String location, ActionInvocation invocation) throws Exception {
//location = "test.definition"; //for test
setLocation(location);
ServletContext context = ServletActionContext.getServletContext();
ApplicationContext applicationContext = ServletUtil.getApplicationContext(context);
TilesContainer container = TilesAccess.getContainer(applicationContext);
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
ServletRequest servletRequest = new ServletRequest(applicationContext, request, response);
container.render(location, servletRequest);
}
}
4) We also need to tell struts2 about our result type:
<?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" />
<constant name="struts.ui.theme" value="simple" />
<package name="tiles-package" namespace="" extends="struts-default">
<result-types>
<result-type default="true" name="tiles-result" class="com.quaternion.result.TilesResult"/>
</result-types>
</package>
</struts>
With that out of the way we can now use tiles in our projects, assume we have created a tiles definition called "test.definition" we can tell our action to use that definition by doing the following:
package com.quaternion.demo.action.test;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
#ParentPackage("tiles-package")
#Result(type="tiles-result", location="test.definition")
public class QuaternionResultTest extends ActionSupport{}
That's it, this will let you configure any version of struts2 with tiles 3+, please see http://tiles.apache.org/framework/index.html for further configuration details.

Thanks to Ken a new plugin was added to Struts 2 to support Tiles 3 result type, it should be available with upcoming new release - Struts 2.3.9
https://cwiki.apache.org/confluence/display/WW/Tiles+3+Plugin

<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
use mentioned doctype in your tiles.xml

Related

Issue while upgrading to struts version 2.5.17

I am trying to upgrade struts version from 2.3.35 to 2.5.17 but I encountered an issue as below:
java.lang.NullPointerException
at com.opensymphony.xwork2.util.fs.StrutsJarURLConnection.getInputStream(StrutsJarURLConnection.java:170)
at com.opensymphony.xwork2.util.fs.JarEntryRevision.needsReloading(JarEntryRevision.java:84)
at com.opensymphony.xwork2.util.fs.DefaultFileManager.fileNeedsReloading(DefaultFileManager.java:65)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.needsReload(XmlConfigurationProvider.java:428)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.needsReload(StrutsXmlConfigurationProvider.java:163)
I have been using this guide to migrate to struts version 2.5.17:
https://cwiki.apache.org/confluence/display/WW/Struts+2.3+to+2.5+migration
I suspect it to be issue with tiles.
I have upgraded all struts related jar to version 2.5.17 including struts2-tiles-plugin. I have also upgraded all tiles related jars to 3.0.7.
Also I have removed the Xwork-core jar as from 2.5 xwork being merged to struts2-core jar.
Am I doing anything wrong.
Please note: I have not done any code changes as of now. The code works perfectly with struts version 2.3.35. But as soon as I have upgraded the struts version along with tiles version I started getting this issue.
Can some one please suggest If I am doing anything wrong?
Yes, there are supposed to be errors without code changes.
I don't think you are doing something wrong.
After adding the new .jars and removing the old ones it will only work when the code is compliant with the new framework.
Code changes would be:
.xml
Add the following <code> to web.xml.
<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>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
Change struts-config.xml to struts.xml and make following changes:
You can remove struts-config.xml completely and use annotations instead of .xml file.( since struts 2.5.17 )
<?xml version="1.0" encoding="ISO-8859-1" ?> <!-- change to UTF-8 -->
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd "> <!-- change to struts-2.5.dtd -->
<struts-config> <!-- change to <struts> -->
<!-- add <include file="struts-default.xml"/> -->
<form-beans>
<form-bean name="MyClassForm" type="forms.MyClassForm">
</form-bean>
</form-beans>
<action-mappings> <!-- change to <package name="hello-default" extends="struts-default"> -->
<action path="/MyClass" name="MyClassForm" type="actions.MyClassAction"
validate="false">
<action name = “MyClass” class = “actions.MyClass”>
<forward name="success" path="/Index.jsp"/>
<result> /Index.jsp </result>
</action>
</action>
</action-mappings> <!-- change to </package> -->
<message-resources parameter="resources"/>
</struts-config> <!-- change to </struts> -->
.java
Remove ActionForm.java files.
Properties are included in ActionSupport class which our Action class is supposed to extend.
Change Action.java
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class MyClassAction extends Action // change to ActionSupport {
//fields are now a property of the ActionSupport class
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
// change to public String execute() throws Exception {
MyClassForm input = (MyClassForm) form; // don't need this
input.setField1(“Hello”); // change to setMessage(“Hello”);
return mapping.findForward(“success”); // change to return Action.SUCCESS;
.jsp
Actions to be performed in this JSP are:
Replace <%# taglib %> directive
Use new set of tags defined by the struts-tags.tld
<%# taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello!</title>
</head>
<s:form action="submit.action" method="post">
<body>
<s:textfield label="Name" name=" field1" />
<s:property value="field1"/>
<s:submit" />
</body>
</s:form>
</html>
Cheers.
I got this resolved by upgrading struts version to 2.5.18. It also worked fine when I downgraded the struts version to 2.5.13.
But it is not recommended to use struts version between 2.5.16 and 2.3.36 (inclusive), so I have upgraded it to 2.5.18

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

#Secured is not working while integrating Spring Security in Jersey project

I have a demo JAX-RS project using Jersey. Now I am trying add Spring Security's method level security but unfortunately its not working although intercept-url xml way is working fine.
Added all the dependency in my pom.xml
Updating web.xml as
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/security.xml,
/WEB-INF/beans.xml
</param-value>
</context-param>
<!-- this is default security impl name used by deletetingFiterProxy -->
<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>
Updating /WEB-INF/security.xml
<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.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<!-- kind of authentication applied 1) Basic 2) form-based etc.. auto-config="true" use-expressions="true"-->
<http auto-config="true">
<http-basic />
</http>
<!-- this allow to enable security annotations in restful resoruces -->
<global-method-security secured-annotations="enabled" />
<!-- for defining users and roles -->
<authentication-manager>
<authentication-provider>
<user-service>
<user name="admin" password="admin" authorities="ROLE_CUSTOMER,ROLE_ADMIN"/>
<user name="student" password="student" authorities="ROLE_CUSTOMER"/>
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
Annotating service inteface methods
public interface StudentServiceInterface {
#GET
#Path("/students")
#Secured("ROLE_CUSTOMER")
public Response getStudents();
#GET
#Path("/students/{id}")
#Secured("ROLE_CUSTOMER")
public Response getStudent(#PathParam("id") int id);
#POST
#Path("/students")
#Consumes(MediaType.APPLICATION_JSON)
#Secured("ROLE_ADMIN")
public Response addStudent(Student stu);
}
Now when I try to access the resource student (/student) class it opens without asking password.
http://localhost:3126/securitydemo/webapi/db/students
StudentServiceInterface interface implementation
#Path("/db")
#Produces(MediaType.APPLICATION_JSON)
public class StudentService implements StudentServiceInterface{
static StudentDao data= new StudentDaoImpl();
#Override
public Response getStudents(){
GenericEntity<List<Student>> entity = new GenericEntity<List<Student>>(data.getAllStudents()){};
return Response.ok(entity).build();
}
#Override
public Response getStudent(#PathParam("id") int id){
return Response.ok(data.getStudent(id)).build();
}
#Override
public Response addStudent(Student stu) {
data.addStudent(stu);
return Response.ok(stu).build();
}
}
You have to use the extention for Spring DI, see Jersey 2.25.1 User Guide:
Jersey provides an extension to support Spring DI. This enables Jersey to use Spring beans as JAX-RS components (e.g. resources and providers) and also allows Spring to inject into Jersey managed components.
The Spring extension module configuration is based on annotations. Spring beans are injected and JAX-RS classes are made Spring managed using annotations. Injected Spring beans can have further dependencies injected using Spring XML configuration. Spring singleton and request scopes are supported.
To enable JAX-RS resources to work Spring functionality that requires proxying, such as Spring transaction management (with #Transactional), Spring Security and aspect oriented programming (such as #Aspect), the resources must themselves be managed by Spring, by annotating with #Component, #Service, #Controller or #Repository:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.springframework.stereotype.Component;
#Component
#Path("/")
public class SomeResource {
#Transactional
#GET
public void updateResource() {
// ...
}
}
Limitations:
Spring beans can't be injected directly into JAX-RS classes by using Spring XML configuration
25.1. Dependencies
If you want to use Jersey Spring DI support you will need to add the jersey-spring3 module into the list of your dependencies:
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.25.1</version>
</dependency>
The above module adds transitive dependencies on Spring modules. See jersey-spring3 module dependencies for more details about list and scope of dependencies. Please note the module depends on The Spring/HK2 Bridge that is used to inject Spring services into HK2 services or inject HK2 services into Spring services.

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.

Managed Bean Annotation doesn't work in JSF 2.0

I've encoutered strange issue with annotations in bean.
index.xhtml content:
<h:outputText value="#{ejb.helloWorld}" />
Ejb.java content:
package bean;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean(name="ejb")
#SessionScoped
public class Ejb implements Serializable
{
public String getHelloWorld()
{
return "Hello World";
}
}
From what we see above i should be able to call bean method... but i can't, it will work only if i manage bean in faces-config.xml file with content:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config 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-facesconfig_2_0.xsd"
version="2.0">
<managed-bean>
<managed-bean-name>ejb</managed-bean-name>
<managed-bean-class>bean.Ejb</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
Questions:
Why i have encountered such situation ?
How to resolve this issue ?
I'm using:
JSF 2.0 (imported to lib)
JBoss Server 4.2
My wild guess is that JSF 1.2 bundled by default with JBoss 4.2 is used (JBoss might not care about JSF version). As a result annotations are ignored, faces-config.xml file is used.
Try to use configuration suggested here (thats's google cached page).

Resources