Showing posts with label struts. Show all posts
Showing posts with label struts. Show all posts
0

Using Cancel button in struts

Cancel button is a type of submit button which bypasses validation during submission of the form. So, cancel button can be used for submitting a form without validation and to reset form.

  1. Using Cancel button for bypassing validation

    Reset button is also available in struts but reset button resets to the form fields to default if the form has not been submitted or has not be passed through validator. Once the validation has occured and validation failed, the form fields are populated with the values we have send for validation. Now by pressing reset button will retain the typed and not default values. In this case, struts cancel button is used. Struts cancel button can be rendered through
    <html:cancel/>.

    It is rendered in html as
    <input type="reset" value="Reset"><input type="submit"
    name="org.apache.struts.taglib.html.CANCEL" value="Cancel"
    onclick="bCancel=true;">

    The onclick="bCancel=true;" in generated reset button plays a great role for bypassing clientside validation.

    If you are using dispactch action, create a method called 'cancelled' and get the form field values inside that method.

    Cancel button bypassed both serverside and clientside validation.
  2. Using Cancel button as reset button
    If you're using Struts 1.2.9 or above, you must specify cancellable="true" in the Action mapping as;


    <action
    attribute="registrationForm"
    input="..."
    ...
    ...
    <set-property property="cancellable" value="true"/>
    ...
    ...
    </action>
    now its time to reset form fields through action like;

    if(isCancelled(request)){
    registrationForm.set("firstname",
    "");
    }
    [I'm using DynaValidatorForm here]

    If you are using DispatchAction then you have to create a method named cancelled and reset forms there.



|
0

Select initial value in struts

Suppose, you want a default value to be selected during the page load while using DynaValidatorForm or DynaValidatorActionForm, use initial attribute of field-property to define the value that is to be selected by default.

eg; you have a SEX porperty whose value can be either male or female and you use radio button. If you wish male to be selected by default, then put inial=male.

Here is my form-bean definition

<form-bean name="consumerRegistrationForm"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="firstname" type="java.lang.String" />
...
...
<form-property name="sex" type="java.lang.String"
initial="male" />
...
...
<form-property
name="userType" type="java.lang.String" initial="homeUser"/>
</form-bean>

Then, while rendering the form, male is initially selected for SEX property.

|
0

Preventing Form Resubmission (due refreshing)

To prevent form resubmission due to page refresh we can use HTTP redirect

To use HTTP redirect feature, the forward is set as follows:



However there is one catch in this method, the actual jsp name is shown.

Therefore we can change the forward action as following:

<forward redirect="true" path="/gotoSuccess.do" name="success">

where gotoSuccess.do is another action mapping using forward action as follows:
<action path="/gotoSuccess" validate="false" parameter="/success.jsp" type="org.apache.actions.ForwardAction">

|
0

Struts Forward redirect=true

excerpted from : Struts : The complete Reference

The forward tag looks up a forward from the Struts configuration file and redirects to it. Based on the way that the forward is defined in the configuration file, this tag will either forward to the URL specified by the forward or perform a redirect to it. If the forward has its redirect attribute set to “true”, as shown in the following example, this tag will perform a redirect:

A redirect sends a response to the user’s browser instructing it to load another page. A forward, on the other hand, simply “forwards” control (on the server side) to another URL without having the browser make another request.

Note that only URLs residing on the same server can be forwarded to, whereas any URL can be redirected to.

|

Copyright © 2009 So That I Can Remember All rights reserved. Theme by Laptop Geek. | Bloggerized by FalconHive.