I want to call to different actions in the same form. In a previous app I developed it works nice,(see code below), but now I have switched versions from Struts 2.1.6 to 2.5.8 and it's not working.
In the answer of this question, the use of different actions in the same form is discouraged. Instead, the author proposes to call different methods inside the same action. That's fine, but in my app I need to call these actions/methods from several places, not only this form, so I would prefer to separate the action calls in the struts.xml file.
Note: I'm calling "action" from the struts.xml point of view. Each action calls to a different method from the same class *Action.java. All actions of this .java class are grouped in the same package of the struts.xml
Form in list.jsp:
<s:form name="changeStatusForm" theme="simple" id="formList">
<s:hidden id="idSelectedRow" name="idSelectedRow"/>
<s:submit key="global.showMore" action="showMore" />
<s:submit key="global.edit" action="edit"/>
<s:submit key="global.delete" action="delete"/>
</s:form>
The jsp is a list of objects. When I click in one row, a menu with several options appear (the ones of the form). The id of the desired object is gathered in the variable idSelectedRow.
struts.xml
<package name="object" namespace="/object" extends="authenticate-default">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
</result-types>
<global-results>
<result name="error" type="tiles">error</result>
<result name="errorLogin" type="tiles">errorLogin</result>
</global-results>
<action name="list" method="list" class="actions.ObjectAction">
<result name="success" type="tiles">listObject</result>
<result name="input" type="redirect">list</result>
<result name="error" type="tiles">listObject</result>
</action>
<action name="showMore" method="showMore" class="actions.ObjectAction">
<result name="success" type="tiles">showMore</result>
</action>
<action name="edit" method="edit" class="actions.ObjectAction">
<result name="success" type="tiles">edit</result>
</action>
<action name="delete" method="delete" class="actions.ObjectAction">
<result name="success" type="tiles">list</result>
</action>
.
.
.
</package>
Object.java:
public class ObjectAction extends BaseActionCRUD implements ModelDriven<ObjectDTO> {
...
public String showMore() {
...
return SUCCESS;
}
public String edit() {
...
return SUCCESS;
}
public String delete() {
...
return SUCCESS;
}
public String list() {
...
return SUCCESS;
}
...
}
So, how can I do this same thing in Struts 2.5.8?
Right now, the method executed in ObjectAction.java is always list() instead of the selected one. That's because the action that redirects to list.jsp is list.
Thanks!
Extremely important: migrate to 2.5.10.1, not to 2.5.8. Any 2.5 version prior to 2.5.10.1 (and any recent 2.3 version prior to 2.3.32) is vulnerable to a critical security issue, S2-045.
Since 2.3.15.3, you need to explicitly enable the action: prefix (that is generated by the action="" attribute in the <s:submit> tags) in struts.xml with:
<constant name="struts.mapper.action.prefix.enabled" value="true"/>
The method="" prefix, called DMI (Dynamic Method Invocation), which usage is suggested in the 2011's answer you've linked, it is now deprecated and completely discouraged.
Also the action: prefix method is discouraged against more robust solutions (like changing the target of the <form> with Javascript according to which of the <submit> buttons has been pressed) but, if you want, you can still enable and use it, there are no big problems with it.
Related
I'm currently playing around with my Struts2 config for wildcard testing and I'm stuck with this one.
<action name="/*/*" class="checkBlogUrl" method="testing">
<param name="blogSiteUrl">{1}</param>
<param name="test">{2}</param>
<result name="success">/WEB-INF/jsp/cmsPages/index.jsp</result>
</action>
<action name="/*/postPreview1" class="blogPostAction" method="test">
<param name="blogSiteUrl">{1}</param>
<result name="success">/WEB-INF/jsp/cmsPages/templatePicker.jsp</result>
</action>
If I access myurl.com/hello/hi I will be redirected to index.jsp.
But if I access myurl.com/hello/postPreview1 I will also be redirected to index.jsp instead of templatePicker.jsp.
Am I doing something wrong here? The struts wildcard doc said that the last one will win
EDIT:
Just tried to switch them around and it worked! Am I misreading the doc?
You are using slashes in action name, that incorrectly works with wildcard mapper. As I said in the linked answer, the best pattern matcher in this case is the regex pattern matcher.
<constant name="struts.patternMatcher" value="regex"/>
See Advanced Wildcards.
<action name="/{blogSiteUrl}/{test}" class="checkBlogUrl" method="testing">
<result name="success">/WEB-INF/jsp/cmsPages/index.jsp</result>
</action>
<action name="/{blogSiteUrl}/postPreview1" class="blogPostAction" method="test">
<result name="success">/WEB-INF/jsp/cmsPages/templatePicker.jsp</result>
</action>
About docs for wildcard mapper. Lets look at the example blank application:
<package name="example" namespace="/example" extends="default">
<action name="HelloWorld" class="example.HelloWorld">
<result>/WEB-INF/jsp/example/HelloWorld.jsp</result>
</action>
<action name="Login_*" method="{1}" class="example.Login">
<result name="input">/WEB-INF/jsp/example/Login.jsp</result>
<result type="redirectAction">Menu</result>
</action>
<action name="*" class="example.ExampleSupport">
<result>/WEB-INF/jsp/example/{1}.jsp</result>
</action>
<!-- Add actions here -->
</package>
So URLs will be matched in the order:
http://localhost:8080/example/HelloWorld
http://localhost:8080/example/Login_input
http://localhost:8080/example/Register
I would say that more specific mapping goes before less specific/common mapping and it wins because it's found first in the order of action configs. Everything that doesn't match the ordered configs fall into last mapping which is less specific.
In your case,from the official docs,
Mappings are matched against the request in the order they appear in the frameworkâs configuration file. If more than one pattern matches the last one wins, so less specific patterns must appear before more specific ones. However, if the request URL can be matched against a path without any wildcards in it, no wildcard matching is performed and order is not important.
https://struts.apache.org/core-developers/wildcard-mappings.html#wildcards
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 the Struts2 framework to create a webapp. I have an interceptor that should have different behaviour depending on which action is invoked. For example, a login interceptor that should always allow some actions to execute, but it should block other actions if the user is not yet logged in.
The way I solve this now is by manually checking the name (and/or namespace) of the action in the interceptor, and determine my behaviour based on that. The downside of this "hardcocded" logic is that it is hard to maintain if I edit my struts.xml file, and it is also not obvious what is going on for other developers.
I would like to know if these is some way to add 'metadata' INSIDE the struts.xml file (or other file?) to "mark" certain actions as being certain "type". For example, something like this:
Struts.xml
<action name="loginPage" types="login, user, viewpage" class="login.controller.LoginPage">
<result name="success">/login/jsp/Login.jsp</result>
</action>
And then in my interceptor class:
#Override
public String intercept(ActionInvocation invocation)
throws Exception
{
Set<String> actionTypes = invocation.getInvocationContext().getTypes();
if(actionTypes.contains("login")
{
doSomething();
}
else
{
doSomethingElse();
}
}
Is this possible, or is the hardcoded parsing of name(space) the only way?
Obviously you can't do that because the DTD won't allow you.
Another good example of the XY problem:
The XY problem is asking about your attempted solution rather than your actual problem.
That is, you are trying to solve problem X, and you think solution Y would work, but instead of asking about X when you run into trouble,
you ask about Y.
What you really need is to define a group of actions running free, and other groups running under login control.
For this, you can manually include the Interceptor in each single action (useful when using the Convention plugin, a waste of time otherwise), or configuring the actions logically in the struts.xml.
The <package> is your friend here: define two (or more) packages, one running with the default settings, the other running your custom Interceptor for each action of the package:
<package name="unsecure-package" namespace="/unsecure" extends="struts-default">
<action name="login" class="org.foo.bar.actions.LoginAction">
...
</action>
<action name="askHelp" class="org.foo.bar.actions.AskHelpAction">
...
</action>
</package>
<package name="secure-package" namespace="/secure" extends="struts-default">
<interceptors>
<interceptor name="authInterceptor"
class="org.foo.bar.interceptor.AuthInterceptor"/>
<interceptor-stack name="securedStack">
<interceptor-ref name="authInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="securedStack"/>
<action name="write" class="org.foo.bar.actions.WriteAction">
...
</action>
<action name="delete" class="org.foo.bar.actions.DeleteAction">
...
</action>
</package>
This way every time you or somebody else will add an Action in struts.xml, it will just need to drop it in the right package, and it will work automatically.
It's better to keep the Interceptor action-agnostic whenever possible ;)
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.
I have the following actions defined in my struts.xml
<action name="Search" method="prepareLookUpvalues" class="com.mycompany.actions.FrSearchAction">
<result name="success" type="tiles">search.layout</result>
</action>
<action name="List" class="com.mycompany.actions.FrSearchAction">
<result name="success" type="tiles">results.layout</result>
<result name="input" type="tiles">search.layout</result>
</action>
<action name="SearchDetails" class="com.mycompany.actions.FrSearchDetailsAction">
<result name="success" type="tiles">details.layout</result>
</action>
<action name="Logoff" class="com.mycompany.actions.LogoffAction" >
<result name="success" type="tiles">logoff.layout</result>
</action>
Assuming that a user goes directly to my page home http://localhost:8080/fr/Search.action everything works OK, but it has been discovered hat some users are accessing http://localhost:8080/fr/List.action directly without first going to the search page which is causing problems.
When a user goes to the search page and enters criteria and submits, it is only then that the "List" action should be called via the struts form's action attribute. I basically want to stop users from being able to access the "List", "SearchDetails", and "Logoff" actions directly unless those actions are invoked from my JSPs or code.
I'm new to maintaining/developing Struts2 applications and I haven't found clear answers to this. Any suggestions would be greatly appreciated!
There's a few details missing so the answer will be a bit vague, but the list action probably pulls values from a form submission to search? Or pulls state from session? Or...?
Anyways, however that may be stored, simply check and then redirect the user to Search if the state is not set as expected.
For details on doing redirects in struts2, see, for e.g. http://www.roseindia.net/struts/struts2/actions/struts-2-redirect-action.shtml
This isn't an elegant solution but you could try checking for the referer to see who called the action. You action class will need to implement ServletRequestAware.
String referrer = request.getHeader("referer");
if (referrer.equals("http://localhost:8080/fr/Search.action")) {
// do the action
} else {
// handle unwanted access
}
Remember that the referer is a client-controlled value and can be spoofed or removed.