Ajax called the previous Action in struts2? - ajax

Let say I have jsp pages named list_question.jsp and ajax_result.jsp
in struts.xml
<action name="question/*/*" class="ProcessAction" >
<param name="selectedCatId">{1}</param>
<param name="questionId">{2}</param>
<result name="success">list_question.jsp</result>
</action>
<action name="submitReponse" class="AJXAction" >
<result name="success">ajax_result.jsp</result>
</action>
the scenario as follows:
First, the page list_question.jsp is displayed as the success result of ProcessAction. Everything worked perfectly.
Then, inside list_question.jsp, I perform an ajax call as follows:
$("#postResponse").click(function(){
$("#responses").html("loading...");
$.ajax({
type:"POST",
url: "submitReponse", // Action name
data: $('form').serialize(),
success: function(data){
$("#responses").html(data);
}
});
});
The problem is, it never called the AJXAction action class, rather it always invoked the previous action class (ProcessAction), even though different action names are specified.
I am missing something?

Related

Multiple input (result name) in Struts2

I am working on an application that is using Struts2 framework. In action class I got two validatemethods, one for each action. In struts.xml I have input to validate a method and returns a corresponding a view but for the other action that needs validation too, how this approach would work? The only thing I need to know is whether I can change the default input to something else so that method2 gets validated if not then how can I go to different view after actions are validated.
Action Class:
public void validateSearch() {
// validation
}
public void validateSubmit() {
// validation
}
// Action Methods
public String search() {
// business logic
}
public String submit() {
// business logic
}
struts.xml
<result name="input">search.jsp</result>
<result name="????">submit.jsp</result>
In case of two input I don't get my views the way I want them. For submit I get a view of search. is there any way to configure this.
You are probably using DMI (which is deprecated and highly discouraged), and have something like this:
<action name="foo" class="foo.bar.fooAction">
<result name="success">foo.jsp</result>
<result name="input">search.jsp</result>
</action>
You simply need to turn your two action methods into two real actions, like follows:
<action name="fooSearch" class="foo.bar.fooAction" method="search">
<result name="success">foo.jsp</result>
<result name="input">search.jsp</result>
</action>
<action name="fooSubmit" class="foo.bar.fooAction" method="submit">
<result name="success">foo.jsp</result>
<result name="input">submit.jsp</result>
</action>
then instead of:
<s:submit action="foo" method="search" />
<s:submit action="foo" method="submit" />
something like:
<s:submit action="fooSearch" />
<s:submit action="fooSubmit" />
ok, since you are still looking fro answers I will tell you that input name for result is conventional. You can change it any time if your action class implements ValidationWorkflowAware.
ValidationWorkflowAware classes can programmatically change result name when errors occurred This interface can be only applied to action which already implements ValidationAware interface!
public void validateSubmit() {
// validation
if (hasErrors())
setInputResultName("inputSubmit");
}
The result config
<result name="inputSubmit">submit.jsp</result>

Magento - Is it possible to specify multiple values for ifconfig?

I want to specify multiple values for ifconfig in layout xml.
<action method="setTemplate" ifconfig="mymodule/general/is_enabled">
<template>mymodule/test.phtml</template>
</action>
Is is possible to add below two conditions for one action?
ifconfig="mymodule/general/is_enabled"
ifconfig="mymodule/frontend/can_show"
Any suggestions would be most welcome.
You could use a helper method in your action parameter. Something like this
<action method="setTemplate">
<template helper="mymodule/myhelper/canShowIf"/>
</action>
will call setTemplate with the results of a call to
Mage::helper('mymodule/myhelper')->canShowIf();
And the following in your modules default helper:
public function canShowIf()
{
if($displayOnly = Mage::getStoreConfig('mymodule/general/is_enabled') == true)
// Do Something
}else if($displayOnly = Mage::getStoreConfig('mymodule/frontend/can_show') == true) {
// Do Something Else
}
return $displayOnly;
}
Implement your custom logic in canShowIf.
Define a function in Helper (Data.php)
<reference name="root">
<action method="setTemplate">
<template helper="modulename/getNewLayoutupdate"/>
</action>
</reference>
in helper function you can load template by conditions.
consider below scenario:
<catalog_category_default>
<reference name="product_list">
<action method="setTemplate" >
<template>mymodule/mytemplate.phtml</template>
</action>
</reference>
</catalog_category_default>
ifconfig : If return value is false, then it takes layout defined in base folder.
helper function : If return value is false, then does not take layout defined in base folder, and not template gets added. that's why empty block is shown.
ifconfig="mymodule/general/is_enabled"
ifconfig="mymodule/frontend/can_show"
why not create an additional config node
ifconfig="mymodule/frontend/is_enabled_can_show" and depending on this value proceed.

how to redirect struts2 action to a url after validation?

I have a action with a url creation like this
if (this.sequence.equals("") ) {
action= "input";
} else {url = "/files/" + testHTML.getName();
action= "redirect";
}
return action;
in my struts.xml my action is declarated has
<interceptor-ref name="completeStack"/>
<interceptor-ref name="execAndWait">
<param name="delaySleepInterval">500</param>
</interceptor-ref>
<result name="wait">testwait.jsp</result>
<result name="input">test.jsp</result>
<result name="redirect" type="redirect">${url}</result>
</action>
When I launch the application, the url is created but there are no redirection to this new page but to the test.jsp page and I have the validation error message that appear. Anybody have some idea?
Your original code example looks correct. Here are a few things to check:
Does your action have a getUrl() method? The ${url} part of the result relies on that method to get the url property.
Have you verified that the correct result is returned in each case? e.g., 'redirect' in the else block and 'input' otherwise.
if you are using validate method and if there is validation errors struts return result name="input". In your case put redirect in
<result name="input" type="redirect">${url}</result>

Struts2 JPA Validation error how to forward to action and not lose ActionErrors messages?

I'm using Hibernate validator 4.1 to validate my Entity.
I have a dashboard that I can access from the action viewDashboard. In my action class, I set the values of two List like this.
public String execute() throws Exception {
listDocteur = service.listDocteur();
listUser = service.listUser();
return SUCCESS;
}
In the DashBoard, I have a submit button that can add a User.
<action name="saveUser" class="com.test.action.CreateUserAction" method="execute">
<interceptor-ref name="mybasicStackWithValidation" >
</interceptor-ref>
<result name="input">/WEB-INF/jsp/viewDashboard.jsp</result>
<result name="success" type="redirectAction">viewDashboard</result>
</action>
If I submit an invalid value, I'll see the error messages, but I'll lose my 2 Lists. If I have a type="redirectAction", I lose the error messages.
In Struts 1, I would forward to the action viewDashboard.do without a redirect and that will works. How can i achieve this in Struts2?
Check this may be it will help you, since redirectAction means a new request at from beginning
Link
Action Chaining may be what you are looking for.
Chaining “allows an Action to forward requests to a target Action, while propagating the state of the source Action.”
I Found a solution. I have to use <input type="redirectAction">youraction ..
and need to put this in your SAVEAction
<interceptor-ref name="store">
<param name="operationMode">STORE</param>
</interceptor-ref>
and finally, in your displayAction (that will display the error messages)
<interceptor-ref name="store">
<param name="operationMode">AUTOMATIC</param>
</interceptor-ref>
or RESTORE

Add a link to Magento's My Account Page Conditionally

I would like to create a link on the My Account page that only get displays under certain conditions.
Right now I have the link display all the time by adding the following entry to my layout XML file:
<customer_account>
<reference name="customer_account_navigation">
<action method="addLink" translate="label" module="nie"><name>nie</name><path>nie</path><label>NIE Admin</label></action>
</reference>
</customer_account>
I am assuming there is a way to code this so that it only displays under certain circumstances.
The cart & checkout links already do something similar so their method can be copied.
Create a block. It won't be displaying directly so can be descended from the boring Mage_Core_Block_Abstract.
Give it a method where the conditional logic will go.
public function addNieLink()
{
if (($parentBlock = $this->getParentBlock()) && (CONDITION-GOES-HERE)) {
$parentBlock->addLink($this->_('NIE Admin'), 'nie', $this->_('NIE Admin'), true, array(), 50, null, 'class="top-link-cart"');
// see Mage_Page_Block_Template_Links::addLink()
}
}
protected function _prepareLayout()
{
// Add the special link automatically
$this->addNieLink();
return parent::_prepareLayout();
}
Put your check in place of CONDITION-GOES-HERE.
Add your block to the links block.
<customer_account>
<reference name="customer_account_navigation">
<block type="yourmodule/link" name="yourmodule.link" />
</reference>
</customer_account>
(Correct the block type here to your newly created link block)
The important bit is it calls getParentBlock() to find out where the link is to go.

Resources