How do I inject the implementation class of a action in struts.xml from struts2.xml?
Suppose struts.xml looks like the following:
<struts>
<action name="EnginesList" class="example.EnginesListAction">
<result name="*">viewengines.jsp</result>
</action>
</struts>
Then I wanted to achieve: when struts2.xml exists(or is not blank), then the class-mappings in struts2.xml overrides those in struts.xml. So if I have the following definition, at runtime EnginesList is mapped to example.EnginesListAction2 instead of example.EnginesListAction.
// struts.xml:
<struts>
<action name="EnginesList" class="example.EnginesListAction">
<result name="*">viewengines.jsp</result>
</action>
<include file="struts2.xml" />
</struts>
// struts2.xml
<struts>
<action name="EnginesList" class="example.EnginesListAction2">
<result name="*">viewengines.jsp</result>
</action>
</struts>
This seems not working due to name conflict. So what's the best way of achieving my end goal?
Background added:
Team-A develops and maintains struts.xml. My team(team-B) uses the codebase of team-A and deploys it in a separate data center.
Team-B needs to have some actions be mapped to different classes. However we cannot directly modify it in struts.xml since it will change the behavior of Team-A's service.
So what I wanted is to have a separate file(struts2.xml) whose class-mappings can override those in struts.xml. No changes in action package name. No changes in JSP code. This struts2.xml will be deployed ONLY in team-B's data center.
The easy way you could do this, is by using multiple ( separate ) struts configuration files and importing them into your main struts.xml file.
In your case rename struts.xml to struts1.xml. Include these two file in your main struts.xml
<include file="struts1.xml"></include>
<include file="struts2.xml"></include>
I am not sure about the order of imports of these file, so cannot comment on whether mappings in 2nd file will shadow the mappings in 1st or vice versa. Try that for yourself to decide which file must be included first.
For more information regarding including configuration refer this.
Refer this link for a full example.
Related
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 really racking my head here with Struts2 - I'm able to access the JSP pages by omitting part of the path. Note the path suppose to include pages/welcome_user.jsp. The key is to look at the word pages in the path.
here's the struts.xml file:
<?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" namespace="/User" extends="struts-default">
<action name="Login">
<result>pages/login.jsp</result>
</action>
<action name="Welcome" class="com.mkyong.user.action.WelcomeUserAction">
<result name="SUCCESS">pages/welcome_user.jsp</result>
</action>
</package>
</struts>
I'm able to access login.jsp via: http://localhost/Struts2Example/User/Login
and welcome_user.jsp via: http://localhost/Struts2Example/User/Welcome
Note that in both URL, I'm able to drop pages, why?
source:
http://www.mkyong.com/misc/how-to-use-mkyong-tutorial/
Can someone go through the above tutorial and tell me what's wrong?
First, you have used URLs that are mapped to the actions in the struts.xml.
The action method is executed and returns a result code SUCCESS. This result you can find in the action config. Then result is executed, if the type of result isn't set the default is dispatcher, and request is forwarded to the location specified in the result config.
If location is relative the final absolute location will be determined by the namespace of the package used for this action.
More detailed example of usage namespaces and explanation you can find in the example Struts 2 Namespace configuration example and explanation.
You can't drop pages if you are using dispatcher result that forwards to JSP. In this case the URL has been rewritten and you can't see the final URL.
I'm developing a simple virtual store for an university project. I'm using struts 1.3. My problem is that I have this:
<action name="ComprarMaisForm" path="/ComprarMais" scope="session" type="com.myapp.struts.ComprarMaisAction">
<forward name="pagar" path="/pago.jsp"/>
<forward name="eliminar" path="/vistaCarrito.jsp"/>
<forward name="comprarmais" path="/index.jsp"/>
</action>
I want to have several actions instead of one. How can I do?
For different type of actions you have to specify different action mappings with different 'paths'
<action path="/goto1" ...> .. </action>
<action path="/goto2" ...> .. </action>
Anyways If You want to have several actions with same name (form-bean name) scope type and forward mappings (probably silly ques), You need the SAME action, just give same action name wherever you want to use it.
If any 1 parameter is different you need to specify different <action/> mappings.
Struts2 newbie here. I've got a JSP file that has an anchor linking to an Action called "authenticate". When I click on the anchor, I get an error message: java.lang.NoSuchMethodException: mypackage.DisplayStuff.authenticate()
java.lang.Class.getMethod(Unknown Source)
Here is my struts.xml
<package name="default" extends="struts-default" namespace="/">
<action name="display" method="authenticate"
class="mypackage.DisplayStuff">
<result name="error">Boo.jsp</result>
<result name="success">Yay.jsp</result>
</action>
</package>
And here is my action file:
package mypackage;
public class DisplayStuff {
public String authenticate() {
return "success";
}
}
And finally, in my JSP, I have this anchor:
click here
The interesting part, is if I rename my method to the default "execute" I don't get this error. Shouldn't I be able to call any method in my DisplayStuff action?
There are a few possibilities.
That the error message references a different package my first guess is that you haven't deployed the latest version of your code: struts.DisplayStuff.authenticate isn't how it's configured.
Second guess is an issue with a Struts 2 plugin (like "convention") mucking with your configuration.
Without more details, however, it's difficult to think much more about it.
This should be
click here
First off, please forgive me for my lack of understanding... I'm still learning :)
I have 2 packages in my struts.xml that extend a base package and I want to be able to access them by typing in my browser something similar to http://site.com/application/1/ThisAction.action and /application/2/ThisAction.action (examples).
I created two directories in my webapp folder, named '1' and '2' and I am able to navigate to both packages using the urls above. What I want to do is actually put my jsps within the jsp directory, instead of webapps. so instead of my two folders residing inside /webapp, they should reside in /webapp/jsp/.
I tried changing the namespace of the two packages to something like /1/jsp/ instead of simply '/1', but i get nothing. It just keeps telling me there is no action mapped to that action name.
Does anyone have any insight on how I can accomplish this? Google's not giving me a lot of help, but it may just be that I'm not searching for the right thing.
Here's a quick sample of what I'm referring to:
<struts>
<!-- Base-->
<package name="base" extends="struts-default" abstract = "true" namespace="/base">
<global-results>
<result name="cancel" type="redirectAction">CancelAction</result>
<result name="close">closewindow.jsp</result>
<result name="error">/jsp/wizard/GeneralError.jsp</result>
</global-results>
</package>
<package name="1" extends="base" namespace="/1">
In Struts 2 you do not need to put your JSP's in various folders according to the URL that you use to access them. Rather the package and the action comes together to create the URL and the result will determine the next view. So you start by writing your action:
public class MyActionClass ...{
...
public String actionMethod() {
//Your action code here
return SUCCESS;
}
}
Next you will create an entry in struts.xml that points to this action.
<package name="default" extends="struts-default">
<!--Interceptors, Global Results etc.-->
<action name="myaction" class="my.package.MyActionClass" method="actionMethod">
<result>/WEB-INF/path/to/yourpage.jsp</result>
</action>
...
</package>
Now, to access this action in the default package you simply use the URL: http://yourserver/myaction.action.
If you create a second package with a different name like this:
<package name="2" extends="default" namespace="/2" >
<action name="myaction" class="my.package.MyActionClass" method="actionMethod">
<result>/WEB-INF/path/to/yourpage.jsp</result>
</action>
...
</package>
Then you can access that action with the URL: http://yourserver/2/myaction.action.
So you can go ahead and put your JSP in the directory called jsp if you wish and you only have to modify the result to point to the correct place.