I have a form with a section where I submit a comment separate from updating the entire form.
To keep my JSP manageable I use the following Struts 2 action:
<action name="maininfo" class="MainInfo">
<result name="success" type="tiles">maininfo</result>
<result name="addFundingComment" type="tiles">addFundingComment</result>
<result name="input" type="tiles">maininfo</result>
</action>
The maininfo tile displays the main form JSP page. The addFundingComment tile displays the form to submit the comment.
My issue is if validation fails the "input" result has to go to either the maininfo tile or the addFundingComment tile but I need it to go to the tile corresponding to the form the validation failed for.
I can get around this by putting the addFundingComment JSP code in the maininfo JSP code and using a <s:if> to display the form I want but I think having two separate JSP files makes each one easier to manage and debug.
I want to use one Action to make it easier to keep all of the maininfo field changes when the user submits a comment.
Is there a way to have one Action with <results> which go to different JSP pages and have validation failures return to the corresponding JSP page?
You can use dynamic result configuration to return the corresponding view. The dynamic property is evaluated via OGNL, so you have to create a getter method to return the location for the input result.
<result name="input" type="tiles">${inputName}</result>
In the action class
public String getInputName() {
String inputName = "maininfo";
if (actionName.equals("addFundingComment")
inputName = "addFundingComment";
return inputName;
}
Related
I'm using struts 1 in legacy project. I want to validate form. I have read good article about that. But I don't understand what struts do if form's validate method returns non-empty ActionErrors. I have specified validate="true" and input="somepage.jsp" in action, but I get a blank page if my form's validation doesn't pass. As I understnad it has to stay at page specified in input? Am I right?
edit
<action path="/struts/BlaBlaAction"
type="com.example.BlaBlaAction"
name="BlaBlaForm"
validate="true"
scope="request"
input="/struts/blablainput.jsp">
<forward name="someaction" path="/struts/AnotherAction.do"/>
<forward name="error" path="/error.html" redirect="true"/>
</action>
Folder struts resides in the root of my app
You also need to display the validation errors using <html:errors/> tag, in somepage.jsp
If your form's validate method returns non-empty ActionErrors, Struts would redirect to the JSP you have defined as input. And then if you have <html:errors> in the input JSP, all the ActionErrors are iterated and displayed.
You may have forgot the "failure" forward in your struts XML configuration. Look at this example:
<action path="/LogonSubmit" type="app.jcj.LogonAction" name="logonForm"
scope="request" validate="true" input="/pages/Logon.jsp">
<forward name="success" path="/pages/Welcome.jsp"/>
<forward name="failure" path="/pages/Logon.jsp"/>
</action>
In the struts-config.xml file,action element has a input attribute which used when the actionForm's validate method return some errors.
<action
path="/somepath"
attribute="someForm"
input="/some.jsp"
name="someForm"
parameter="status"
scope="request"
type="cn.mycompany.struts.action.SomeAction"/>
But I want to change this input attribute when errors occur in the validate method,because I have more than one page submit to this action and I want it forward to the current submit page.
How can I do this,please help me.
I have an "ajax execute"-tag embedded in a commandLink-Tag in my JSF-File, which works pretty well.
My problem:
I want to execute this ajax-call directly by using a Get-Request.So far, I'm using only one html-file(index.html) without any parameters.
Is there any way to call an AJAX-tag directly by using url/get-parameters but keeping the ajax-tag in jsf?
Thanks guys
To answer the question in your question title, "How to redirect to Ajax-call in JSF?", just add ?faces-redirect=true to the navigation case outcome.
public String submit() {
// ...
return "index?faces-redirect=true";
}
To answer the question in your question body, "I want to execute this ajax-call directly by using a Get-Request", replace the POST command link by a normal GET link <h:link> or <h:outputLink> wherein you pass the parameters as <f:param>.
<h:link value="link" outcome="index">
<f:param name="foo" value="bar" />
</h:link>
They can be collected and processed in the target page by <f:viewParam> or #ManagedProperty.
I have a JSF page in which I have a div which acts as a popup window.
This popup is displayed when the user clicks on a certain button or link, until which it is hidden.
I would like to have another JSF page that provides the content for this div via an AJAX call.
I vaguely remember doing this using Struts Action and JSP fragment.
Is it possible to do this in JSF 2.0?
ADDITIONAL DETAILS:
My scenario is as follows:
I have a page that displays the details of employees as a summary table using the dataTable tag with a class EmployeeInfo as a backing bean that provides a Collection of EmployeeBeans. On this page I have a radio button as the first column in the dataTable. This page has a div that is hidden.
When the radio button is switch on and a certain button is clicked, over an AJAX call we need to hit the backing bean to get the details of the EmployeeBean that has been selected as above and populate the div based on this AJAX call.
The reason why I do not want to have a full submit on the first page and get the second page is, because I want to save the state of any changes that have been done on the first page.
Using a tag, you are able to show or hide content quite easily. This is all rough code typed out quickly but in your bean, imagine you had the following:
public class MyBean {
public boolean renderHidden = false;
public void toggleHidden(AjaxBehaviorEvent event) {
renderHidden = !renderHidden;
}
}
Then in your JSF page, you'd have a link to show hide your popup done as a ui:fragment:
<h:commandLink value="Click Me!">
<f:ajax event="click" listener="#{myBean.toggleHidden}" render="hiddenarea" />
</h:commandLink>
<ui:fragment id="hiddenarea" rendered="#{myBean.renderHidden}">
<div><!-- Content to show/hide here --></div>
</ui:fragment>
That ui:fragment could easily be in another JSF page that you include via ui:include if you need it to be. The important bit is that the f:ajax take is what makes the AJAX call (on a click event in this case) and updates the specified element (hiddenarea).
I have an action Registration.java which is used for users to create an account.
this action class has two methord: doList and execute.
doList gets data from the database and renders the initial jsp page with some s:select tags.
execute do the actual business logics.
in the struts.xml:
<action name="InitList" method="list" class="......Registration" >
<result name="success">/..../...../Registration.jsp</result>
<action name="Registration" class="......Registration">
**<result name="input" >InitList.action</result>**
<result name="next" type="redirect">InitListReg.action</result>
</action>
I also have a validation config file: RegistrationAction-Registration-validation.xml
when i created some validation error and the intial page was not displayed with the error: InitList.action is not available. It seems strut2s did not recognized the action InitList. When i change the result input like this:
<action name="Registration" class="......Registration">
**<result name="input" type="redirect">InitList.action</result>**
<result name="next" type="redirect">InitListReg.action</result>
</action>
the initial page was displayed successfully, but the validation error messages were lost and not displayed because of "redirect".
So i wonder if input can be an action or only support jsps. Or how can i fix my problem?
When you use the Struts2 Validations togehther with redirect you need the MessageStoreInterceptor.