struts 2.0.11 - Currently I'm in my local machine and have two different web applications running under tomcat (struts 2.0.11) and weblogic respectively.
Tomcat:
Link: localhost:8080/retail/order.action
struts.xml:
<action name="order" method="execute" class="com.retail.action.OrderAction">
<result name="redirect" type="redirect">
<param name="location">${weblogicURL}?user=${user}</param>
</result>
</action>
I have necessary attributes like 'weblogicURL' and 'user' in OrderAction in Tomcat Web app. Now this is working and weblogic resource is getting accessed as GET method like the following,
localhost:7020/retail/orderPortal?user=test
I would like to access weblogic using POST method from tomcat, is there any way to achieve that?
This is what the redirect documentation says
session or with web parameters (url?name=value) which can be OGNL expressions.
Which indicates that we can only send data only by get method and what you are trying to do is not possible as of now.
One possible solution seems to use Action Chaining.Please go through the official document for details
Action-Chaining
Related
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.
In struts2, I took advantage of built-in OGNL in struts2, naming my inputs like <input name='bag["item"].property'>
Which went to getters/setters getBag().get("item").setProperty(value)
I've upgraded to struts 2.2.1, and suddently those no longer work: the getter never gets called.
The internet is utterly silent on using OGNL in parameters, as if nobody ever made complex forms.
How do I get my map-parameters back?
It turns out that they hardened restrictions on parameter names to boost security.
So I had to add to my struts.xml:
<interceptor-stack name="defaultStack">
<interceptor-ref name="params">
<!-- For maps to work -->
<param name="acceptParamNames">
[a-zA-Z0-9\.\]\[\(\)_'\s"/]+
</param>
</interceptor-ref>
</interceptor-stack>
(I had "s and /s in my parameter names)
File upload ceased working after that (interceptor stacks are madness), so I had to add it explicity either.
Update: These days I strongly suggest using JSON to pass complex structures instead of rich OGNL forms. Of course you would need some JS.
We're using Struts 2 in our web application. There are bunch of action mappings defined already, I want to implement a feature where any Urls starting with /buy and not mapped to any of the existing action mappings e.g. /buy/new or buy/old should be redirected to buy/index action. For example if someone is trying to go to /buy/bla1 or /buy/bla2 or buy/bla1/bla2/bla3 should go to buy/index.
Define the following action in your /buy package:
<action name="*">
<result type="redirectAction">index</result>
</action>
Alternatively you can just use:
<default-action-ref name="index" />
But then you will get no redirect. Instead the index page will show under the nonexisting address.
You also need to set the following parameter in your struts.properties:
struts.action.extension = action,,
You can add other extensions to the parameter, but as far as i know there is no wildcard for all extensions. The blank string covers directories like /buy/bla but /buy/bla.x won't be covered and will produce a 404 error unless you add x to the list of extensions.
If this is not good enough you might be better off solving this by using redirect rules in the webserver.
You can use default action reference and provide a JSP success path to it.
If any unmatched action is received then it will call the given action and forward it to the result JSP page.
I'm developing a Struts 2 project where I need to implement 2 friendly URLs. Since my 2 URLs need to be something like URL/name/id and URL/id/title, the only way I found to manager this was to use an 1st step action to process the request which then forwards to the correct Action1 or Action2, like this:
<action name="/*/*" class="web.ProcessRequestAction">
<param name="firstParam">{1}</param>
<param name="secondParam">{2}</param>
<result name="action1" type="chain">Action1</result>
<result name="action2" type="chain">Action2</result>
</action>
And have both actions defined as well like this:
<action name="Action1" class="web.Action1" method="execute">
<result name="success">/WEB-INF/content/Action1.jsp</result>
<result name="input">/WEB-INF/content/Action1.jsp</result>
<result name="error">/WEB-INF/content/Action1.jsp</result>
</action>
However, if I invoke URL/parameter/parameter, and then on ProcessRequestAction I return "Action1", I'll get the Action1.jsp (as intended) but all messy, seems like all the CSS is missing.
First of all, is my approach correct? If so, what I'm I doing wrong here, what can I improve?
Thansk in advance!
As to why your CSS isn't working, we have no way of knowing--a JSP is a JSP, and follows normal JSP/HTML rules.
As for the first issue: I'd recommend against action chaining; it's actively discouraged, and usually just makes a mess of things.
Without knowing more about your application architecture, it's difficult to give targeted advice, but you may find parameters in namespaces helpful.
Even if it's not immediately applicable, you may be able to play games with a PatternMatcher or ActionMapper that's more targeted to your actual needs--the implementation would depend on a variety of factors, particularly the format(s) of the name, id, and title URL components.
Here are a few resources that may help you achieve the clean URLs you're looking for:
Better URLs with Struts2
https://stackoverflow.com/questions/4772737/hidden-features-of-struts-2-framework/4837917#4837917
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