In my struts2 application i want to define a global configuration part and want to use it where needed.
To clarify let me share one action definiton below;
<action name="do_login" class="xxx.actions.AuthAction" method="doLogin">
<result name="success" type="json">
<param name="noCache">true</param>
<param name="contentType">text/html</param>
<param name="excludeProperties">actionErrors,actionMessages,errorMessages,errors,fieldErrors,locale,texts,action,username,password,errorCode,errorMessage,session</param>
<param name="ignoreHierarchy">false</param>
</result>
<result name="error" type="json">
<param name="noCache">true</param>
<param name="contentType">text/html</param>
<param name="excludeProperties">actionErrors,actionMessages,errorMessages,errors,fieldErrors,locale,texts,action,session</param>
<param name="ignoreHierarchy">false</param>
</result>
</action>
as you can see, in result definitions there are some repeating parts which are;
<param name="noCache">true</param>
<param name="contentType">text/html</param>
<param name="excludeProperties">actionErrors,actionMessages,errorMessages,errors,fieldErrors,locale,texts,action,username,password,errorCode,errorMessage,session</param>
<param name="ignoreHierarchy">false</param>
i want to define this repeating parts globally at the beginning of struts.xml file and use it in each result definition like;
<result name="success" type="json">
{include global definition here}
</result>
i know it is possible to split struts.xml by packages or actions, but i'm wondering it is possible to do above my need?
thx in advance for any response.
Struts2 doesnt have anything for global parameters though they have for global result type,
so you can very well have both success and error as global result and use them.
They have something called as static parameters which is for Action class but not for result type as you need.
Related
I have some variables in my action class.
Now I want to redirect to another action by setting those variable values as request parameters. How I can do this?
I'm trying to do it like this:
<result type="redirect">myProfileShow?param1=${value}¶m2={value2}</result>
It is not working.
<result type="redirectAction">
<param name="actionName">myProfileShow</action>
<param name="param1">${value}</param>
<param name="param2">${value2}</param>
</result>
where value and value2 should be declared in the Action class. You can also set hard-coded values for value & value2.
<param name="param1">10</param> <!-- param1 will be 10 --!>
<param name="param2">15</param> <!-- param2 will be 15 --!>
I have multiple actions that after completion redirect back to a general page (showStuff). I'm looking for a way to NOT repeat the list of parameters for every redirect-action.
What I have is this:
<action name="doThis" class="com.domain.package.MyAction" method="doThis">
<result type="redirectAction">
<param name="actionName">showStuff</param>
<param name="parse">true</param>
<param name="selectedYear">${selectedYear}</param>
<param name="selectedMonth">${selectedMonth}</param>
<param name="selectedDay">${selectedDay}</param>
</result>
</action>
<action name="doThat" class="com.domain.package.MyAction" method="doThat">
<result type="redirectAction">
<param name="actionName">showStuff</param>
<param name="parse">true</param>
<param name="selectedYear">${selectedYear}</param>
<param name="selectedMonth">${selectedMonth}</param>
<param name="selectedDay">${selectedDay}</param>
</result>
</action>
I would like to keep the parameter list within the showStuff action definition, and then use is like so:
<action name="doThis" class="com.domain.package.MyAction" method="doThis">
<result type="redirectAction">
<param name="actionName">showStuff</param>
</result>
</action>
<action name="doThat" class="com.domain.package.MyAction" method="doThat">
<result type="redirectAction">
<param name="actionName">showStuff</param>
</result>
</action>
Is it possible?
There are a few options.
Honestly, I'd skip most of my workarounds, and put them into session.
Once they're in session, create an interceptor and interface (Dateable or something). In the interceptor check the session for the variables (see below) and if the action is a Dateable, set them on the action, and you're done.
Another option is to encapsulate these variables as a date and either use the built-in converter or use your own converter. Then you'd only need a single parameter. This option would work with the interceptor idea as well.
As it turns out, it is very much possible. This is how you do it:
Add a global result:
<global-results>
<result name="show-stats" type="redirectAction">
<param name="actionName">showStats</param>
<param name="parse">true</param>
<param name="selectedYear">${selectedYear}</param>
<param name="selectedMonth">${selectedMonth}</param>
<param name="selectedDay">${selectedDay}</param>
</result>
And then for the actions:
<action name="doThis" class="com.domain.package.MyAction" method="doThis"/>
<action name="doThat" class="com.domain.package.MyAction" method="doThat"/>
Finally in the java code, just:
return "show-stats";
And you're done.
As a sidenote, why do I have to spend so much time trying to adhere to the very basic DRY principle? Aren't all these frameworks supposed to .. you know.. simplify stuff? Just wondering...
I was facing the same problem with an endless list of params getting longer and longer, repeated in several places. What I ended up doing was that I created an external file and declared it in struts.xml as an entity then included it instead of repeating all the params
This goes in the doctype tag
<!ENTITY referenceName SYSTEM "fileName">
Then you include it like so
&referenceName;
Hi any body know how to send parametrs to the url in struts.xml
mycode is
<action name="deleteDocDetails" class="com.myDrDirect.doctor.action.DoctorEditAction"
method="deleteSingleDoctor">
<result name="success" type="redirect">salesManDoctorhome </result>
</action>
i want to pass searchIndicator=Allsearch&search=&pageLink=doctor these variable to this parameter.
i want my url to be like this salesManDoctorhome?searchIndicator=Allsearch&search=&pageLink=doctor
i have already tried this
<action name="deleteDocDetails" class="com.myDrDirect.doctor.action.DoctorEditAction"
method="deleteSingleDoctor">
<result name="success" type="redirect">salesManDoctorhome </result>
<param name="searchIndicator">Allsearch</param>
<param name="pageLink">doctor</param>
</action>
But it is not working..
the variable i am sending is static variable. I am using struts2 in my project
You may want use variable in url, you may have {variable name} this variable must have public get method in action.
In the Below code,
<result type="redirectAction" name="success">
<param name="actionName">
loadManagePrdtprty{1}?prtycrnid=${prtCrnId}&exitWindow=${exitWindow}
</param>
</result>
application is not getting start..What will be the problem.
I don't think that
loadManagePrdtprty{1}?prtycrnid=${prtCrnId}&exitWindow=${exitWindow}
is your Action's name. You can look up on how o use redirectAction here : http://www.vitarara.org/cms/struts_2_cookbook/post_and_redirect
Firstly, I went here ( http://code.google.com/p/struts2-examples/downloads/list and I downloaded Hello_World_Struts2_Mvn.zip) and I run that example.
After that, I went here (http://struts.apache.org/2.x/docs/jfreechart-plugin.html), I add the dependencies for commons-lang-2.5.jar, jcommon-1.0.16.jar and jfreechart-1.0.13.jar and I modify the example downloaded from code.google.com to see how JFreeChart is working, but I receive this error:
Unable to load configuration. - action - file:/C:/.../untitled_war_exploded/WEB-INF/classes/struts.xml:34:67
Caused by: Error building results for action createChart in namespace - action - file:/C:/.../out/artifacts/untitled_war_exploded/WEB-INF/classes/struts.xml:34:67
Caused by: There is no result type defined for type 'chart' mapped with name 'success'. Did you mean 'chart'? - result - file:/C:/.../out/artifacts/untitled_war_exploded/WEB-INF/classes/struts.xml:36:49
At the line 36 in struts.xml is the this code (the code from struts2 website):
<action name="viewModerationChart" class="myapp.actions.ViewModerationChartAction">
<result name="success" type="chart">
<param name="width">400</param>
<param name="height">300</param>
</result>
</action>
What I'm doing wrong?
You need to define Action mappings related to chart differently in Struts.xml.Change your project's struts.xml with Jfreechart related actions and define it in separate package.
For eg.
<package name="struts2" extends="jfreechart-default">
<action name="viewModerationChart" class="myapp.actions.ViewModerationChartAction">
<result name="success" type="chart">
<param name="width">400</param>
<param name="height">300</param>
</result>
</action>
</package>