struts2 validation result input cannot be an action - validation

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.

Related

Validation with one action and multiple JSP pages in Struts 2

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;
}

Struts 1 form validation

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>

Magento - createBlock() in controller, can I access this in layout.xml?

I have an index controller with a view action that does the following:
echo $this->getLayout()
->createBlock('core/template','builder')
->setTemplate('pages/builder/view.phtml')
->setHeaderText($extra->getHeaderText())
->setFooterText($extra->getFooterText())
->setProducts($collection)
->toHtml();
Based on this, I am trying to perform layout updates in builder.xml (which is being set in the module's config.xml). However, I am not sure if you can target blocks instantiated in this manner. Based on the above block creation, am I able to perform the following:
<layout version="0.1.0">
<builder_index_view>
<!-- shouldn't this reference the createBlock() name argument? -->
<reference name="builder">
<block type="page/html_header" name="builder.header" as="builder.header" template="pages/builder/header.phtml"/>
</reference>
</builder_index_view>
</layout>
If the block is instantiated prior to the call to loadlayout(), yes.

how to display review form as well as reviews in a tab in product page [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
how to add review tab on product view page
How to display review form as well as reviews in a tab in product page .
I was doing the following in catalog.xml
<!--action method="addTab" translate="title" module="catalog"><alias>review</alias><title>Review</title><block>review/product_view_list</block><template>review/product/view/list.phtml</template></action--> <!--For getting the review datails-->
<action method="addTab" translate="title" module="catalog"><alias>review</alias><title>Review</title><block>review/form</block><template>review/form.phtml</template></action> <!--For displaying review form-->
This is how I handled this situation in one of my projects:
Add tab with reviews
<block type="catalog/product_view_tabs" name="product.info.tabs" as="info_tabs" template="catalog/product/view/tabs.phtml" >
<action method="addTab" translate="title" module="catalog"><alias>tab_review_list</alias><title>Product Reviews</title><block>review/product_view_list</block><template>catalog/product/view/tabs/reviews.phtml</template></action>
</block>
Now, the review form is handled by the different type of block which normally is a sub-block of review page. There is no way to make nested block with addTab action but you can use <reference> handler after creating review block in tabs like this:
<reference name="tab_review_list">
<block type="review/form" name="tab_review_form" as="review_form" template="catalog/product/view/tabs/review_form.phtml" />
</reference>
name in <reference> handler must be equal to what is in <alias> in addTab action
And in catalog/product/view/tabs/reviews.phtml you just use
echo $this->getChildHtml('review_form');
You can use <reference> handler to add more block to review list and review form.
Of course, you have to create files for review list and review form in the paths entered in template argument, so in this case you would need to create catalog/product/view/tabs/reviews.phtml and catalog/product/view/tabs/review_form.phtml. You can change review form template to the default one review/form.phtml if you do not need change the code there or you will be using it only in that tab but review list might need more changes in html structure so it is good idea to create separate file for it and use parts of the default code as needed.

Magento - custom module that captures user e-mail as soon as it is entered

In a custom extension, I'm thinking I might like to capture the user's e-mail address as they enter it in the various places that it can be entered (Checkout, Create Account, Log In). I might not, but if I did...
I think the best way to do this would be to inject a piece of JavaScript, but I'm not sure how to do that. I guess I need to specify a block in a layout file? So maybe,
app/design/frontend/base/default/layout/mymodule.xml
<default>
<reference name="before_body_ends">
<block type="mymodule/myblock" name="not sure" as="not sure" />
</reference>
</default>
and in the block class, the _toHtml() method could return some JavaScript that added an event handler [onchange] to the email address field, and the implementation of the event handler made an AJAX call to a controller passing the e-mail address entered.
I think the above layout class would result in JavaScript being injected in every page. I would only want it on the ones that contain an email address entry field. What layout file should I create to do that (location, name, and contents).
I went through the demo site and viewed the source of the pages where an e-mail address is entered. The id of the email address field is different in each case, so the JavaScript would have to account for that.
Many Thanks for any help.
If all you want to do is to include a .js file on every page you should add it to page.xml:
<layout>
<default>
<reference name="root">
<action method="addJs"><script>relative/path/from/js/folder</script></action>
</reference>
</default>
</layout>
Wait for the dom to load and setup your javascript observers.

Resources