I'm using struts and spring integrated frameworks in my project. I have an interceptor before every struts action call. I need to access my action name and am doing so with the following piece of code:
actionInvocation.getProxy().getActionName();
and my struts action in struts.xml is :
<action name="uploadDocument" class="commonAction" method="uploadDocument">
<interceptor-ref name="sessionStack"/><interceptor-ref name="cachingStack"/>
<interceptor-ref name="basicStack"/>
<result name="success" type="stream">
<param name="contentType">text/html</param>
<param name="result">inputStream</param>
</result>
</action>
I need to access the parameters under the result tag. Is that possible?
Sure.
You can read the result configuration in a ResultConfig object, like described here, that will expose a Map of its params, like shown by the source code.
Something like:
// Get the action configuration defined in struts.xml
ActionConfig config = invocation.getProxy().getConfig();
// Get the SUCCESS result configured for that Action
ResultConfig success = config.getResults().get("success");
// Iterate the Params, friendly printing :)
for (Map.Entry<String, String> entry : success.getParams().entrySet()) {
System.out.println("<param name=\""
+ entry.getKey()
+ "\">"
+ entry.getValue()
+ "</param>");
}
Related
I have an action defined in struts.xml which deletes a record on the database. However, I would like to map this action using annotations. This contains a "wildcard" parameter (the id of the record to be deleted). I am not quite sure how could this be when using annotations.
The action in struts.xml is as follows:
<action name="delete/*" class="RecordAction" method="deleteRecord">
<param name="record.id">{1}</param>
<result type="redirect">/table.html</result>
</action>
In plain old servlets I can use doGet and doPost methods. Where in doGet i'm forwarding user to some page and in doPost i'm proccessing data entered from the page that I gave. That all happening in one servlet.
But the Struts2 works on Front Controller pattern and instead doGet/doPost I have only execute method. So how can I properly give user some page, so then he can see it, enter data, submit and application as result proccess it in execute ?
From what I know I have two options (example on registration form):
Map page to another url:
<action name="register_display">
<result name="success" type="dispatcher">register.jsp</result>
</action>
<action name="register"
class="magazine.action.client.RegisterClientAction"
method="execute">
<result name="success" type="redirectAction">/index</result>
<result name="error" type="redirectAction">register_display
</result>
</action>
Create whole package named display and place there all view from which POST can be performed:
<package name="display" namespace="/display" extends="struts-default">
<action name="register">
<result name="success" type="dispatcher">register.jsp</result>
</action>
...
</package>
Is there any other options ? Which one is prefered ?
In the standard Struts2 style, an Action class has only one work method, this is the execute method. However, you do not necessary have to follow this. You can define multiple actions in a single Action class.
For example you make a GET request to users, which is handled in the default execute method of UsersAction.
#Override
public String execute() {
// fetch the list of users
return SUCCESS;
}
Let's suppose you would like to add a new user in this same action, by POSTing to user_add. So you define an add method:
public String add() {
// add the user
return SUCCESS;
}
The struts.xml would look similar to this:
<package name="users" extends="defaultPackage">
<action name="users" class="com.example.UsersAction">
<result>users.jsp</result>
</action>
<action name="user_add" class="com.example.UsersAction" method="add">
<result type="redirect">users</result>
</action>
</package>
In your scenario, you would render your page, which the user should see after the run of the (maybe empty) execute method. Then, you would make the post request, which would be mapped to the other method of the Action class.
I use Struts 2.3.16.3. I want an action from webapp 1 to pass parameters to an action in webapp 2. In the struts.xml of webapp 1 I define the following result:
<result name="success" type="redirect">
<param name="location">http://localhost:8080/Webapp2/index.action</param>
<param name="testParam">testValue</param>
</result>
I expect my browser to redirect me to this webpage (a page in webapp2) when the result equals 'success':
http://localhost:8080/Webapp2/index.action?testParam=testValue
However, my browser takes me to:
http://localhost:8080/Webapp2/index.action
completely ignoring the parameter.
If I change my result to have everything inside the location param then it works, but you can see this gets very clunky with multiple params:
<result name="success" type="redirect">
<param name="location">http://localhost:8080/Webapp2/index.action?testParam=${testValue}</param>
</result>
This correctly redirects my browser to the url:
http://localhost:8080/Webapp2/index.action?testParam=testValue
Why does the first method not work?
If the location starts with http:, https:, mailto:, file:, ftp: then it's used as a final location to redirect using response.sendRedirect(). Parameters in the result using <param> tag in this case are ignored.
I was wondering, is it possible to dynamically set value to static parameter
<action name="TestApp_*" class="test.TestApp" method="{1}">
<param name="app_Id">Dynamic value here</param>
<result name="input">WEB-INF/jsp/test/testView.jsp</result>
</action>
I tried
<action name="TestApp_*" class="test.TestApp" method="{1}">
<param name="app_Id">${app_Id}</param>
<result name="input">WEB-INF/jsp/test/testView.jsp</result>
</action>
Not working. It shows ${app_Id} as regular text.
Note sure what exactly you mean by It shows ${app_Id} as regular text.Struts2 provide this flexibility where you can set param values dynamically.
All you need to set these values in your action class and you are all set to use them as a place holder.
public class MyAction extends ActionSupport {
private int app_Id;
public String execute() {
// you execute logic
this.app_Id= 123;
return SUCCESS;
}
// getter and setter for app_Id
}
And you can use this app_Id in your result configuration
<action name="TestApp_*" class="test.TestApp" method="{1}">
<param name="app_Id">${app_Id}</param>
<result name="input">WEB-INF/jsp/test/testView.jsp</result>
</action>
you can get more details parameters-in-configuration-results
You probably have written (erroneusly) the getter by hand, instead of letting the IDE do it for you.
A variable app_Id; must have a getter getApp_Id(), while you could have something like getapp_Id() or get_app_Id()... but how are we supposed to know it if you don't post your Action code ?!
Also, consider to change your variables (from now on) to respect the convention, that wants them camelCased, instead that underscore-separated... then appId for the variable and getAppId() for the getter.
try to do it like this way..
<result type="redirect">
<param name="location">WEB-INF/jsp/test/testView.jsp</param>
<param name="inputName">app_Id</param>
</result>
in the jsp page you can directly use this value like
<s:properties value="app_Id">
try this.
Look at the staticParams interceptor.
This interceptor populates the action with the static parameters defined in the action configuration. If the action implements Parameterizable, a map of the static parameters will be also be passed directly to the action. The static params will be added to the request params map, unless merge is set to false.
Parameters are typically defined with <param> elements within struts.xml.
It shows how to configure this interceptor to your action configuration.
For example:
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="defaultStack">
<param name="staticParams.parse">true</param>
<param name="staticParams.overwrite">false</param>
</interceptor-ref>
<param name="num">${numValue}</param>
<result name="success">good_result.ftl</result>
</action>
This enables the interceptor to parse parameter values for OGNL expression from the action configuration.
I'm having an issue while trying to redirect mapping with dynamic parameters.
The way I'm mapping in Struts2:
<action name="Delete" class="templateLalaAction" method="remove">
<result name="success" type="redirect-action">
<param name="actionName">LalaTemplatesDisplay</param>
<param name="buId">${buId}</param>
</result>
<result name="failure" type="redirect-action">
LalaTemplatesDisplay
</result>
</action>
The method "remove" in the action:
remove() {
putRequestAttribute("buId",Long.valueOf("1111"));
return SUCCESS;
}
if I do this, I'm setting the buId=1111, but when I run the app, the url ends with buId= (it's empty), i.e., no parameter is being passed.
if I comment the putRequestAttribute method, and set struts passing buId parameter as a static value:
<action name="Delete" class="templateLalaAction" method="remove">
<result name="success" type="redirect-action">
<param name="actionName">LalaTemplatesDisplay</param>
<param name="buId">1111</param>
</result>
<result name="failure" type="redirect-action">
LalaTemplatesDisplay
</result>
</action>
It works and the url ends with buId=1111.
I also read this question where the accepted answer teaches us to do the same I did, but if we read the comments the user did, we'll see he has the same problems I have. What am I possibly doing wrong?
Inside your method just assign buId variable and you need getter/setters for it in your action class.
public String remove() {
buId = 1111l;
return SUCCESS;
}
Also you are using old syntax for redirect-action, use camel case redirectAction.