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>
Related
I am working on an application built using oracle ADF 10.1.2.17.87 and I see that the SQL loads each time the action class is invoked.
<action path="/search/SearchPage" ...>
<set-property property="modelReference" value="search_searchUIModel"/>
<forward name="success" path="/search/searchPage.do"/>
</action>
<DCIterator
id="SearchIterator"
Binds="SearchModuleDataControl.SearchView"
RSIName="null"
RangeSize="10"
>
</DCIterator>
<ViewObject
Name="SearchView"
SelectList="empno,ename from employee"
/>
RefreshCondition = #{adfFacesContext.postback == true} does not work. It throws nullpointer exception. What else can I do to avoid full table load during initial load? are there any attributes that I can set to avoid this full table scan.
use AdfFacesContext.getCurrentInstance().isPostback() in your managed bean constructor
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?
Is it possible to use Custom Variables in a layout file? I can use them in a template file like this:
Mage::getModel('core/variable')->loadByCode('variableCode')->getData('store_plain_value')
But not sure with the xml file.
I know I could use the above instead, but this would be useful to know anyway for future uses too.
UPDATE: Have been most unclear I'm afraid. I am specifically looking to access the admin panel "Custom Variables" section, not just pass my own variables to a block. I do apologise for the lack of clarity.
Mage_Core_Block_Abstract extends Varien_Object and inherits its __call() overloading. Whereas block actions in layout XML call block methods, the following are possible:
Pass a string (and it can be translated!):
<action method="setSomeVal" translate="arg" module="some/helper">
<arg>Some String</arg>
</action>
Pass an array:
<action method="setSomeVal">
<arg>
<key1>Some String</key1>
<key2>Some String</key2>
<key3>
<multikey1>Some String</multikey1>
</key3>
</arg>
</action>
Pass anything you want:
<action method="setSomeVal">
<arg helper="some/helper/method">
<param_for_the_helper_method>
<getting_crazy>Oh Boy.</getting_crazy>
</param_for_the_helper_method>
</action>
Retrieve the value in the block/template with $this->getSomeVal();.
Fun, huh?
Did you try the following :
<!-- in layout xml file -->
<action method="setData"><name>color_id</name><value>5</value></action>
Then, you can use in block file like below :
$colors = $this->getColorId();
# or
$colors = $this->getData('color_id');
Based on the updated question:
Create a helper class which wraps the core/variable functionality, e.g:
class Some_Module_Helper_Variable
{
public function getVariableData($code,$param)
{
return Mage::getModel('core/variable')->loadByCode($code)->getData($param);
}
}
Then, in layout XML for your block you can do this (I believe):
<action method="setSomeVal">
<arg helper="class_group/variable/getVariableData">
<arg1>variableCode</arg1>
<arg2>store_plain_value</arg2>
</arg>
</action>
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>
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