HOWTO point an <action> in struts.xml to a different <action>? - struts2

Basically when the user goes to example.com I want to serve it the same content I would have, if it went to example.com/news. Below is the code snippet.
Current COnfig
<action name="" class="action.public.news">
<result>/jsp/labs/listLabs.jsp</result>
</action>
<action name="news" class="action.public.news">
<result>/jsp/labs/listLabs.jsp</result>
</action>
Desried COnfig
<action name="" class="action.public.news">
Use Action Named "news" instead
</action>
<action name="news" class="action.public.news">
<result>/jsp/labs/listLabs.jsp</result>
</action>

If i understand it right you want that when user hit the base domain example.com it should fetch the content of the action news one way to do this is.
create an empty file name welcome in your web-content folder.Add following entry to your web.xml
<filter>
<filter-name>action2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>action2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
replace your welcome list file in web.xml as
<welcome-file-list>
<welcome-file>welcome</welcome-file>
</welcome-file-list>
and finally in your strus.xml do something like
<action name="welcome" class="action.public.news">
<result>/jsp/labs/listLabs.jsp</result>
</action>
what we are trying to do is that when we hit example.com instead of showing the welcome jsp file we are hitting the action and using its result

Most people create a index.jsp page in the webapp folder containing this:
<% response.sendRedirect("index.action"); %>
This will redirect the visitor to the index.action when they arrive at your domain. Then, in struts.xml:
<action name="index" class="action.public.news">
<result>/jsp/labs/listLabs.jsp</result>
</action>
Cheers,
Christian

Related

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>

Tuckey UrlRewriteFilter and Struts2 gives 404 on first request

About 30 of my rewritten links work fine but I have a couple that don't work the first time they are called but they do work the second time!
This is the web.xml snippets:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>uk.co.prodia.prosoc.struts2.action</param-value>
</init-param>
</filter>
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
and here is the urlrewrite.xml that gives a 404 the first time it is called:
<rule>
<from>^/forgotten-password$</from>
<to>/unsecured/forgotten-password!input.action</to>
</rule>
and the struts2 config for that action:
<package name="unsecured" extends="struts-default" namespace="/unsecured">
<action name="forgotten-password" class="actionForgottenPassword">
<result name="input">/WEB-INF/view/unsecured/forgotten-password.jsp</result>
<result name="passwordNotFound">/WEB-INF/view/unsecured/forgotten-password.jsp</result>
</action>
</package>
However if I make the urlrewrite.xml pass through the unsecured package which contains the forgotten-password action then it works:
<rule>
<from>^/unsecured/forgotten-password$</from><!--works through /unsecured -->
<to>/unsecured/forgotten-password!input.action</to>
</rule>
It seems that missing of the Struts2 package name from the urlrewrite causes the 404 the first time the URI is used.
Does this make any sense to anyone or shall I just stay in crazy town?
The problem seems to be caused by the jsessionid in the URL on the first page load of the context. I am a bit useless with regexp but this seems to work:
<rule>
<from>^/forgotten-password(;jsessionid=.*)?$</from>
<to>/unsecured/forgotten-password!input.action</to>
</rule>
I added the (;jsessionid=.*)? to the end of each of the rules and it seems to work.

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.

problems with struts 2 developing login [duplicate]

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!

Listener class org.apache.struts2.tiles.StrutsTilesListener

I have been trying to get through an example of tiles based program in struts.
I am using a very simple program. Inside web.xml, I am calling a listener class as shown.
<listener>
<listener-class>
org.apache.struts2.tiles.StrutsTilesListener
</listener-class>
</listener>
Whenever I include this lines in web.xml, my program shows a HTTP status 404 error.
Can anyone tell me what is wrong with this listener class?
Another problem is I am unable to map from struts.xml to tiles.xml.
I am not 100% sure whether this latter problem is because of non-inclusion of that listener class so as to allow my program to run. Just to clarify that doubt also, consider my part of struts.xml file.
Struts.xml
<action name="login" class="Myclass">
<result name="success" type="tiles">/WelcomeFile.tiles</result>
</action>
tiles.xml
<definition name="/WelcomeFile.tiles" extends="BaseLayout">
<put-attribute name="body" value="myFile.jsp" />
So is it necessary that files should be extended by .tiles. Please clarify on this.

Resources