My question is:
I have one pop up window to validate some content, and i use action class to do the validation(javascript not allowed). If the content is valid, i need to close this pop up window, and update the content in the main page.
My idea is in my struts.xml, i mapped
<result name="add" type ="redirectAction"> mainpage.jsp</result>
is it possible to add
<result name="add" > popupCurrentPage.jsp</result>
to let the same action result leads to two diffent pages at the same time?
Browsers may receive a single response for any request.
Doing multiple things on the client side based on a response requires JavaScript somewhere.
Use it's possible to add the tag in main page.
<s:include value="popupCurrentPage.jsp"/>
but when you are redirecting you will miss the valuestack data, so use messageStore interceptor.
Related
How is navigation from a Facelet page
<p:commandLink action="xyz.xhtml">
or a backing bean
<p:commandLink action="#{bean.redirect}">
public class Bean{
public String redirect(){
.....
return "xyz.xhtml";
}
}
different from each other?
How is navigation from a xhtml page or a backing bean different from each other.
There's no difference. The both examples invoke a POST request and instructs JSF to render the view associated with the given outcome. The backing bean method has the only advantage that it allows you to perform some business logic beforehand or even control the outcome value programmatically.
However, if you don't have any business logic at all and solely want to have an idempotent link to another page, then using a command link is actually a bad practice. Using POST for page-to-page navigation is not user nor SEO friendly. The target page is not bookmarkable (the URL remains the one of the page where the POST form was been submitted to) nor searchbot-crawlable (it is using JavaScript to submit a hidden form).
You should instead use a normal link.
<h:link outcome="xyz.xhtml">
This generates a SEO-friendly <a> element with the full URL in its href and ends up in an user-friendly bookmarkable URL.
See also:
When should I use h:outputLink instead of h:commandLink?
How to navigate in JSF? How to make URL reflect current page (and not previous one)
Check out the documentation of p:commandLink here, which says the following for action attribute:
A method expression or a string outcome to process when command is
executed.
Now, as action="xyz.xhtml" returns String xyz.xhtml you're redirected accordingly and for action="#{bean.redirect}" which again returns xyz.xhtml you are again redirected according to the returned String.
I need a way to manage the creation of the commandlink ID in DataTable, the problem is that when i use it for deleting a record and interrupt the rendering of the page (reloading it by 'ctrl+f5' ) it assigns the same id to another link button, which results in deleting the row containing it.
The problem in question is sound, but the requested solution is not the right one and not easily to achieve in JSF — basically, you'd need to homebrew a custom command link renderer which is designed specifically for usage in data tables and is able to recognize the specific entity.
The right solution is send a redirect to the same view after POST.
public String delete() {
// ...
return FacesContext.getCurrentInstance().getViewRoot().getViewId() + "?faces-redirect=true"; // Feel free to hardcode the view ID, though.
}
(if you intend to display some faces message along it, use the flash scope)
A browser refresh would then result in only the redirect being refreshed rather than the POST action.
An alternative is to submit by ajax instead.
<h:commandLink ...>
<f:ajax execute="#form" render="#form" />
</h:commandLink>
A browser refresh would then only re-execute the last synchronous request, which would be the initial GET request which opened the page in question.
I'm very new to struts.This is what i'm trying to achieve.
JavaScript function written in JSP-1 fires an AJAX which uses action class to get data from the database and jsp-2 uses data from action class and forms certain elements using struts tags and outputs this HTML data to JavaScript function which add HTML data to the JSP-1.
Now, the reason i'm using JSP-2 is to make the HTML data using struts tags.
JSP-2 just acts like a function which forms HTML data for JSP-1 which will never be displayed.
I know normal flow in which action class returns a JSON object to ajax but i'm not able to figure out how to do the middle JSP-2 page.
This is my struts.xml for jsp to action and action to ajax flow.
<action name="ajaxAction" class="ActionClass">
<result name="success" type="json"/>
</action>
My question here is how to go from action class to JSP-2 and then response to AJAX ?.
If you are using jquery, you can do it like this
$(document).ready(function(){
var url ="MyAjaxAction.action";
$("#sectionWhereJSP2WillbeThere").load(url);
});
And in your struts action class, just use normal success (i.e. treat as normal action)
So use dispatcher result type not json:
<action name="ajaxAction" class="ActionClass">
<result name="success">JSP-2</result>
</action>
There's no need to think about JSP-1. It was only the delivery mechanism for your Ajax application. The question is what kind of response the Ajax request you are making needs. Does your Javascript Ajax client want a JSON response or an HTML fragment?
If it wants json, then I don't see the need for JSP-2 ( though you can create json with a jsp -- but there's little reason to do so when struts provides the json result type to do that for you ).
If it needs a HTML fragment, the best way to create that is with a JSP, presumable your jsp-2. If this is the case, you need to change the result type to "dispatcher", which is the default type actually, i.e. you don't need to specify it.
<action name="ajaxAction" class="ActionClass">
<result name="success">/path/to/jsp-2</result>
</action>
I have a case like this:
JSP -> ACTION 1 -> Redirect-> Action 2 -> JSP
While redirecting we loose all the parameters. But there is one parameter that I'd like to pass from one action to another. The parameter is always the same for all actions.
I know that it is possible to write the following code and it works :
<action name="myAction" class="myActionClass" method="doThis">
<result name="success" type="redirect">doThat.action?myParam=${myParam}</result>
</action>
The only thing is that I'd like to pass the parameter automatically from one action to another when I have a redirect without writing anything in my action tag, but I don't know how to code it.
Is it possible with struts 2 to do that?
Someone suggested to put it in global-results, I tried, but still haven't succeeded.
You can set the property in to the session as an attribute and the same attribute can be taken back from the session irrespective of the action class you are using.
session.setAttribute(name,value);
session.getAttribute(name)
<action name="actionA" class="com.company.Someaction">
<result name="success" type="redirect-action">
<param name="actionName">OtherActionparam>
<param name="paramA">${someParams}</param>
<param name="paramB">${someParams}</param>
<param name="aBoatLoadOfOtherParams">${aBoatLoadOfOtherParams}</param>
</result>
</action>
In the above action map, I am redirecting from SomeAction to OtherAction. I am having issues, because unfortunately I need to pass a large amount of data between the two actions. IE7 will only allow GET requests to be like 2k, so its blowing up when I'm just over that limit when the response calls a get request to the other action.
Is it possible for me to set this redirect, to end up with a POST being called to the other action?
As the docs states:
The only way to pass data [after a redirection] is through
the session or with web parameters (url?name=value) [i.e., query string for a GET request]
Perhaps a case for action chaining? I'm not sure, and it's not usually recommended, but it seems that you scenario is rather unusual, so it might pay to take a look.
In this case, we are not really making a redirection, i.e., we are not going back to the client, but keeping everything inside the server. Supposedly, then, the full interceptor stack is executed again - and the posted data should impact on the new action, one would hope...