Struts2: interceptor and parameters - struts2

i have done some pages with Struts 2.(J2EE project)
All was ok until i try to add an interceptor.
It seems that the Interceptor delete all properties of my Class Action and Parameters send by the jsp with url like: action?param=xxx
here is the interceptor:
public class SessionInterceptor extends AbstractInterceptor{
#Override
public String intercept(ActionInvocation invocation) throws Exception {
return invocation.invoke();
}
here is the struts.xml:
<action name="movefc_ShowFjt" class="struts2.ShowFjtAction" method="movefc">
<interceptor-ref name="sessionInterceptor"></interceptor-ref>
<result name="input" type="dispatcher">jsp/showFjt.jsp</result>
<result name="success" type="dispatcher">jsp/showFjt.jsp</result>
</action>
in the class action,
public class ShowFjtAction extends ActionSupport {
private String param;
private Personne p;
param property never receive value from the jsp (it is ok when interceptor is off). Worse, other properties in Class action seems to be erased.
Is that an normal effect of the return invocation.invoke(); of the interceptor ?
Is there anything i can do to fix that ?

y defining your own interceptor are you causing all of the default interceptors to be discarded?
Should you perhaps be defining an interceptor stack which includes your interceptor and the default stack?
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="sessionInterceptor" class="SessionInterceptor"/>
<interceptor-stack name="myStack">
<interceptor-ref name="sessionInterceptor"/>
</interceptor-stack>
</interceptors>
<action name="movefc_ShowFjt"
class="struts2.ShowFjtAction">
<interceptor-ref name="myStack"/>
<result name="input" type="dispatcher">jsp/showFjt.jsp</result>
<result name="success" type="dispatcher">jsp/showFjt.jsp</result>
</action>

The entire concept is explained as follows
1] First when user does not writes any interceptors, then interceptors defined in struts-default.xml will be used. It is defined in struts-core.jar, it is accomplished by extending the "struts-default" extended in our package xml tag.
2] When user writes his own interceptor if you add one mode code block after sessionInterceptor ref name i.e interceptor-ref name="defaultStack" will solve your problem.
Befor trying this try to unzip the struts-core.jar and move forward with your implementation.

Related

struts2 tokensession not setting request in Action excludedMethod [duplicate]

This question already has an answer here:
Prevent same action called twice as long as user is in current session
(1 answer)
Closed 5 years ago.
I'm trying to avoid double-submit problems using tokenSession. My action methods are working fine without tokenSession technique.
I add <s:token/> in upsert_crypto_sources.jsp and tokenSession interceptor in struts.xml but I receive request as null in my action excludedMethod of list().
The list page doesn't need to avoid double submit problem but if I add <s:token/> in view_crypto_sources_list.jsp and remove list() from excludedMethod then I always receive result invalid.token.
My struts.xml is like:
<struts>
<package name="key-manager" namespace="/shared/km" extends="console-default" strict-method-invocation="true">
<action name="manage_cs_*" method="{1}" class="console.shared.km.ASC_ManageCryptoProfilesAction">
<interceptor-ref name="tokenSession">
<param name="excludeMethods">
list, initInsert, load, delete
</param>
</interceptor-ref>
<result name="list">/shared/km/view_crypto_sources_list.jsp</result>
<result name="insert">/shared/km/upsert_crypto_sources.jsp</result>
<result name="update">/shared/km/upsert_crypto_sources.jsp</result>
<result name="load">/shared/km/upsert_crypto_sources.jsp</result>
<allowed-methods>list, insert, load, update, delete, testConnection, forward, cancel</allowed-methods>
</action>
My action implements ServletRequestAware interface therefore it gets the request member variable set using setServletRequest() method.
I added a defaultStack interceptor and it is working fine:
<struts>
<package name="key-manager" namespace="/shared/km" extends="console-default" strict-method-invocation="true">
<action name="manage_cs_*" method="{1}" class="console.shared.km.ASC_ManageCryptoProfilesAction">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="tokenSession">
<param name="excludeMethods">
list, initInsert, load, delete
</param>
</interceptor-ref>
<result name="list">/shared/km/view_crypto_sources_list.jsp</result>
<result name="insert">/shared/km/upsert_crypto_sources.jsp</result>
<result name="update">/shared/km/upsert_crypto_sources.jsp</result>
<result name="load">/shared/km/upsert_crypto_sources.jsp</result>
<allowed-methods>list, insert, load, update, delete, testConnection, forward, cancel</allowed-methods>
</action>

Attribute parameter from <action> element - equivalent in Struts 2

In Struts1 you can use the attribute parameter from element(struts-config.xml) and access it's value within the action class via the actionMapping.getParameter() method. For actions requiring multiple steps, the parameter is often used to indicate which step the mapping is associated
with.
Ex:
<action path="\something\Step1"
type="actions.SomethingAction"
parameter="step1"> ...
<action path="\something\Step2"
type="actions.SomethingAction"
parameter="step2"> ...
Which is the alternative solution for Struts2?
Parameters in the action configuration could be used instead
<package name="something" namespace="/something" extends="struts-default">
<action name="Step1" class="actions.SomethingAction">
<param name="step1" ...
</action>
<action name="Step2" class="actions.SomethingAction">
<param name="step2" ...
</action>
</package>

How can I change action class entry in struts.xml dynamically?

I have an application where I want to modify(or overwrite) action mapping in struts.xml with new entry.
For detailed explanation here is the scenario.
In my application I have different stages like registration, change_data, I am managing these using context Param.
Web.xml
<context-param>
<description>Current stage of the application</description>
<param-name>stage</param-name>
<param-value>registration</param-value>
</context-param>
I have an action which is redirect request for Payment Gateway i.e payment gateway is sending user to this action.
struts2.xml (demo entries)
<!-- for registration -->
<action name="paymentResponse" class="abc.xyz.PaymentResponse">
<result name="input" type="tiles">paymentForReg.tiles</result>
<result name="success" type="tiles">home.tiles</result>
</action>
<!-- for change data -->
<action name="paymentResponse" class="abc.xyz.PaymentResponseForChangeData">
<result name="input" type="tiles">paymentForChangeData.tiles</result>
<result name="success" type="tiles">home.tiles</result>
</action>
So when stage of the application changes, what I am doing is changes stage in web.xml and commenting one entry from of action from struts.xml
So to summarize I have multiple actions with same name and I want to trigger(or change class name of action) action on the basis of context param. is there any way to do it?
If i am not wrong i think you have to use dynamic action like below then you can use the same action and same entry in xml
struts.xml:
<action name="paymentResponse" class="abc.xyz.RedirectPaymentResponse" method="updateCriteria">
<result name="response" type="tiles">paymentForChangeData.tiles</result>
<result name="responsechanged" type="tiles">paymentForReg.tiles</result>
<result name="success" type="tiles">home.tiles</result>
</action>
Action class:
public class RedirectPaymentResponse extends AbstractAppAction {
public String execute () throws Exception
{
// some code
return "success";
}
public String updateCriteria(){
//logic here
if(PaymentResponse){
// code here
return "response";
}
if(PaymentResponseChanged){
// code here
return "responsechanged"
}
}

Create multiple actions in Struts2

How to create multiple actions in Struts2 in single action class?
Please give example ?
I found following code on searching internet but it is giving errors OR Do I need to write separate action class for every request ?
Beside what #Quaternion said, S2 actions are in itself java class and i am not sure what you are asking (multiple actions in Struts2 in single action class).
We have several ways to use a single S2 action class here are few of them
Map single action class with different alias like.
<action name="A" class="MyActionClass">
<result type="redirectAction">Menu</result>
<result name="input">/Logon.jsp</result>
</action>
<action name="B" class="MyActionClass">
<result type="redirectAction">Menu</result>
<result name="input">/Logon.jsp</result>
</action>
But i believe you want to map different action request to different action method. S2 provides a way to define any number of methods and we can tell S2 to call which method on which action class from UI.
for e.g let's say we have a UserAction class which is responsible for handling user interaction like
Login
Logout
Registration
for this we need not to create different Action classes but we can create a single action class say UserAction and can define different methods in side it and can configure S2 to call different methods like
<action name="Logon" class="UserAction" method="logon">
<result type="redirectAction">Menu</result>
<result name="input">/Logon.jsp</result>
</action>
<action name="Logout" class="UserAction" method="logout">
<result type="redirectAction">Menu</result>
<result name="input">/Logon.jsp</result>
</action>
<action name="Register" class="tUserAction" methood="register">
<result type="redirectAction">Menu</result>
<result name="input">/Logon.jsp</result>
</action>
Hope this might help you to clear your doubts
In above use case MyActionClass has been mapped with two alias A and B and you can map to any number.
I got your question exactly that you want to write multiple redirects in single action based on action selected like add/edit/delete code at single place. You should look for DispatchAction for your requirement.
Following are few examples you can look at, all provides how to implement DispatchAction.
Link1
Link2
Link3
Another method:
Here is the action class with 3 actions (execute, action1, action2)
public class MyAction extends ActionSupport{
public String execute(){
return SUCCESS;
}
public String action1(){
return SUCCESS;
}
public String action2(){
return SUCCESS;
}
}
Here is config:
<action name="myAction" class="MyAction">
<result>showMe.jsp</result>
<result name="input">showMe.jsp</result>
</action>
Action "execute" will be called by default.
To call action "action1" or "action2" you must put parameter in your request whith name "method:action1" or "method:action2".
Call default action (execute): /path_to_action/myAction.action
Call action1: /path_to_action/myAction.action?method:action1
Call action2: /path_to_action/myAction.action?method:action2
Your can change default method:
<action name="myAction" class="MyAction" method="action1">
<result>showMe.jsp</result>
<result name="input">showMe.jsp</result>
</action>
So, when your call /path_to_action/myAction.action, action1 will be executed.

Redirecting URL using struts2

How can i redirect www.mysite.com/12345 to www.mysite.com/action?id=12345 using struts2?
I use URL rewriting to get these kind of flexible mappings working (though you could probably do it in struts proper, possibly with your own interceptor or something). There's a great little project, urlrewritefilter that gets the job done. In your URL rewriting configuration you'd have a rule like:
<rule>
<from>^/(\d+)$</from>
<to>/action?id=$1</to>
</rule>
Have a look at the manual to see if it is what you are looking for.
<action name="12345">
<result type="redirect-action">
<param name="actionName">action</param>
<param name="id">12345</param>
</result>
</action>
UPDATE
Ok. Based on the comment below.
I have managed something like this in this way in the past. Create a package in struts with a catch all action.
<package name="numbers">
<action name="*" class="my.package.ActionClass" method="urlrewrite">
<result type="redirect-action">
<param name="actionName">${nextpage}</param>
<param name="id">${id}</param>
</result>
</action>
</package>
Then in the urlrewrite method of the action class:
public String urlrewrite(){
//parse the url and determine the ID and the next page
nextpage = "action";
id = "12345";
return SUCCESS;
}
So in calling the action you will have to do like this:
http://www.mysite.com/numbers/12345.action
If you do not want a new package then you can do this in the default package.

Resources