Struts2 Validation - No Validation on Input - struts2

Every tutorial I have found on Struts2 Declarative validation explains how to make the fields validated which is nice and easy. But how do you enter the page with out it being validated?
I have the below Action mapped
<package name="admin" namespace="/admin" extends="struts-default">
<action name="display_*" class="action.admin.AdminAction" method="display">
<result name="input">/WEB-INF/pages/secure/admin/adminUsers.jsp</result>
<result name="success">/WEB-INF/pages/secure/admin/adminUsers.jsp</result>
</action>
Shouldn't I be able to call it with admin/display_input.action to skip validation?

You are using defaultStack (it's activated by default), see http://struts.apache.org/2.0.11/docs/interceptors.html (<default-interceptor-ref name="defaultStack"/>)
It contains validation interceptor which will ignore validation only on methods input,back,cancel,browse. So you can action via one of these methods, or you can use interceptor stack without validation interceptor (basicStack, for example).
You also can annotate your action method with #SkipValidation annotation.

Maybe this will help?
http://www.mail-archive.com/user#struts.apache.org/msg69320.html
Try setting the method attribute to method="{1}".

Related

How do you create a Language Selector in Struts 2?

I know I can call a url with the "request_locale" parameter but that won't last long. Maybe storing it in a cookie? Which I don't know how to do.
After many hours of searching I found:
A low of dead links in the apache API
Setting the locale in session attribute "org.apache.struts.action.LOCALE"
... or "request_locale"
... or "request_cookie_locale"
... or "WW_TRANS_I18N_LOCALE"
Setting the locale in ActionContext.getContext()
Something about interceptors that make my application throws exceptions so I don't know if I should pursue...
There's a mix of Struts 1 and Struts 2 in there which is not making things easier...
Here's what I have so far:
In the XML config:
<action name="languageSelection" class="changeSelectionAction">
<result name="input" type="redirect">${redirectUrl}</result>
</action>
I figured I'd use "Input" to go back to the previous page after the user selected a language.
The redirect variable is defined in my Action Class.
In the Web Page:
<webwork:url id="url" action="languageSelection">
<webwork:param name="lang">fr</webwork:param>
</webwork:url>
<webwork:a href="%{url}">French</webwork:a>
This sends "lang=fr" which I can read in my Action Class.
In the Action Class, I have:
HttpServletRequest request = ServletActionContext.getRequest();
String refererUrl = ServletActionContext.getRequest().getHeader("Referer"); // gives me the source page so I can go back to it.
setRedirectUrl(refererUrl);
String[] langs = request.getParameterValues("lang"); // getting the language the user selected
... followed by a lot of garbage that doesn't work and some insults for the compiler that are commented out.
I'm not the best web developer so any basic web advice will be welcome.
You can implement a i18n interceptor which can store your locale in the session or a cookie. See https://struts.apache.org/core-developers/i18n-interceptor.html
<interceptor name="i18nCookie" class="org.apache.struts2.interceptor.I18nInterceptor"/>
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="i18nCookie">
<param name="localeStorage">cookie</param>
</interceptor-ref>
<interceptor-ref name="basicStack"/>
<result name="success">good_result.ftl</result>
</action>
We use the org.apache.struts2.interceptor.I18nInterceptor in our default interceptor stack. After that struts listen to the parameter request_locale and every action that gets a ?request_locale=LANGCODE parameter will change the language for the user. Normally this setting is stored in the session of the user, so make sure user sessions are enabled too.
With the link from #TheSlavMan you can read all the possible options available for this interceptor.
Of course a simple "change language and redirect to a page"-action is possible. You are on the right path, my first guess would be to change the lang-parameter to request_locale.

Is there an equivalent for default-action-ref in Struts2 Conventions Plugin

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 {
}

Default result in struts2

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

Struts2 dispatch request to another application

I'm using Struts 2.1.8.1. I have a requirement to embed some pages from another server on my own app, so the users will access to them through my application, without accessing directly the other server. My idea is to have a package definition for that, so any access to that package would be redirected to the internal server.
<package name="eco-marketing" namespace="/marketing" extends="eco-default">
<action name="*">
<result name="success" type="dispatcher">
<param name="location">http://myotherserver:8080/test/{1}</param>
</result>
</action>
</package>
But it does not work, I got a Error 404--Not Found, so I suppouse is not as easy as it sounds. Any ideas on how to do this?
TIA
I'll assume that you are accessing just html, then see: http://download.oracle.com/javase/tutorial/networking/urls/readingWriting.html
I would recommend creating an action in that package to do the work for you, and use the value of the parameter to get the required data.
You probably already know but an iframe in the consumer action will make this easier to use than trying to parse what you need out.
After you have that figured out, if you decide to create a custom result type, please post it back to us here it would be very interesting.
An example of a custom result type can be found here (4th code block from the top): http://siriwardana.blogspot.com/2008/12/creating-custom-result-type-struts-2.html

When to use redirect and chain result types in struts2

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>

Resources