everyone.
I'm having problem on struts2 namespace..
First of all, this is my developing environment.
server : tomcat
(currently my project is in ROOT folder in /tomcat/webapps/ROOT)
framework : struts2
Here is my problem.
lets say there are two pages. admin_index.jsp and front_index.jsp
when i want to call admin_index.jsp from action. i use
<package name="admin" namespace="/dl_adm" extends="struts-default">
<action name="/index" method="index"class="kr.co.www2.controller.front.AdminMainController">
<result name="success">/WEB-INF/jsp/admin/admin_index.jsp</result>
</action>
</package>
and it works fine by calling http://.../dl_adm/index.do
and to call this has problem for me.
<package name="front" namespace="/" extends="struts-default">
<action name="/index" method="index"class="kr.co.www2.controller.front.FrontMainController">
<result name="success">/WEB-INF/jsp/admin/front_index.jsp</result>
</action>
</package>
when i go for the http://.../index.do, it gives 404...
although i aware that namespace="/" is for default namespace...
QUESTIONS:
Is there anyway to ignore the default namespace? because i want to use that / because i just want to go through http://.../ and action name without namespace...
or if there isn't a way to do that. any suggestions?
Is there anyway to ignore the default namespace? because i want to use that / because i just want to go through http://.../ and action name without namespace..
No, you can't ignore the default namespace. The default namespace is empty and it's used if you omit namespace attribute in the package declaration.
or if there isnt a way to do that. any suggestions?
I would not use slashes in action name using xml configuration. The action mapper might incorrectly add an additional slash to the action name to infer the mapping from the URL.
So you should use
<package name="front" namespace="/" extends="struts-default">
<action name="index" method="index"class="kr.co.www2.controller.front.FrontMainController">
<result name="success">/WEB-INF/jsp/admin/front_index.jsp</result>
</action>
</package>
Related
I want to have an action default but not in XML because I want to use annotations using the Struts2 Conventions Plugin.
So I want to replace
<package abstract="true" namespace="/" name="mypackage" extends="struts-default">
<default-action-ref name="index"/>
</package>
with something in annotations, so I don't have to use a struts.xml file but also redirect to a specific action when an unknown action is specified in the URL.
Is there support for this in the Struts2 Convention Plugin or are there any good workarounds that are annotation based?
Currently there is no support but you can request at here.
I don't think that there is a simple tip or workaround!
This answer may come a bit late but still be useful to someone.
As mentioned in Struts documentation, an alternative to default actions is using wildcards:
<action name="*">
<result>/index.jsp</result>
</action>
Or using an annotation:
#Action(value = "*")
#Result(location = "/index.jsp")
public class HomeAction extends ActionSupport {
}
I want to use a struts2 interceptor for all actions which start with the specific name:
Say I want to intercept all actions which start with Module. Is there any way I can configure an interceptor something like the Spring AOP does e.g: com.acme.web.actions.Module*
I can't use AOP because it breaks all page params (As I guess there is no way the params interceptor is invoked there).
is this possible?
In Struts you can define different interceptor stacks and apply different stacks to different actions.
So in your situation you can define a different stack like this:
<interceptors>
<interceptor name="myInterceptor" class="com.company.security.MyInterceptor"/>
<interceptor-stack name="myStack">
<interceptor-ref name="myInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
And, using wildcard mappings, the configuration bellow is essentially "map all actions named ModuleSomething to a class com.acme.web.actions.ModuleSometing, and apply myStack of interceptors to it:
<action name="Module*" class="com.acme.web.actions.Module{1}">
<result>{1}.jsp</result>
<interceptor-ref name="myStack"/>
</action>
Im wondering if it's possible to set a default result that would apply to every action in struts2.
For example to say that result "FOO" will always redirect to action BAR or to jsp "BAR.jsp" instead of adding <result name="FOO">xxx</result> to every action I define ...
That would help me to create a generic error page
Thanks
Yes, you can set global results for a package.
<global-results>
<result name="error">/Error.jsp</result>
<result name="invalid.token">/Error.jsp</result>
<result name="login" type="redirectAction">Logon!input</result>
</global-results>
For more details read Result Configuration Page
One quick solution that comes to my mind is global result configuration.Most often, results are nested with the action element. But some results apply to multiple actions. In a secure application, a client might try to access a page without being authorized, and many actions may need access to a "logon" result.
If actions need to share results, a set of global results can be defined for each package. The framework will first look for a local result nested in the action. If a local match is not found, then the global results are checked.
<global-results>
<result name="error">/Error.jsp</result>
<result name="invalid.token">/Error.jsp</result>
<result name="login" type="redirectAction">Logon!input</result>
</global-results>
For more details refer to the official doc
result-configuration
I am developing a web application in struts2. I have declared global properties in struts.xml like:
<constant name="struts.custom.i18n.resources" value="LableResources,MessageResources" />
How do I use those properties in struts.xml itself? I want to implement a system in which request URI names come from global properties file.
As far as I know, you can't do that. I18n resources are for messages an runtime stuff not for configuration.
You can use parameters in the configuration but at runtime they have to be available as request parameters or in the value stack.
Like this:
<result name="input" type="redirectAction">
<param name="actionName">${from}</param>
<param name="id">${id}</param>
</result>
In my struts 2 project when using redirect action i m loosing all my values such as action error and field errors.
I looked it up on net and found 2 options
Chain - This isn't used much i donno why ..
MessageStoreInterceptor - This needs to be placed in every action
So can any one please let me know when is redirect(or RedirectAction) preferred and when is chain preferred.
Redirecting an action looses the current value stack (anything in request scope) you can of course set up your action to preserve these values by passing them as parameters to the next action, but it is a bit of a pain.
Chain preserves the value stack, so the next action can work on parameters created from the previous action without needing to explicitly pass them, also since there is this snow ball effect you can use all the parameters in the view.
But it is generally recognized that a top down solution (maybe top down isn't the best word... 'structured') is better than building a maze of spaghetti actions.
So when you're under pressure to get something working and not overly familiar with struts2 then use chain or redirection, and then definitely come back and fix it! In general you should use an interceptor.
In the event of an action that routes to other actions based on some condition it would be better to make that an interceptor apply that to a package and put all actions which require this interesting behavior in that package. Then it is very clear which actions this applies to.
First option
<action name="remove" class="com.action.firstAction" method="remove">
<result name="success" type="redirectAction">
secondaction
<param name="actionName">secondaction</param>
<param name="namespace">/</param>
<param name="param name">${param value}</param>
</result>
</action>
<action name="secondaction" class="com.action.secondAction" method="result">
<result name="success">result.jsp</result>
</action>
Another option
<action name="remove" class="com.action.firstAction" method="remove">
<result name="success" type="chain">secondaction</result>
</action>
<action name="second action" class="com.action.secondAction" method="result">
<result name="success">result.jsp</result>
</action>