Class Name: TfAction Package: controller Action Path: /tf_handlerThen Next.
ActionForm Bean Name: TfHandler Input Resource: /textfield.jsp Scope: Session
errors.add("name", new ActionMessage("error.name.required"));
by
errors.add("name", new ActionMessage("errors.required", "name"));
This allows us to use the already-existing messages in
ApplicationResource.properties.
and avoid modifying this file.
<action-mappings>
<action
input="/textfield.jsp"
name="TfHandler"
path="/tf_handler"
scope="session"
type="controller.TfAction">
<forward name="success" path="/textfield.jsp"/>
</action>
<action
input="/ajax-out.jsp"
name="TfAjaxHandler"
path="/tf_ajax_handler"
scope="session"
type="controller.TfAction">
<forward name="success" path="/ajax-out.jsp"/>
</action>
<action path="/textfield" forward="/textfield.jsp"/>
<action path="/textfield-ajax" forward="/textfield-ajax.jsp"/>
<action path="/index" forward="/index.jsp"/>
</action-mappings>
One observation to make is that both /tf_handler and
/tf_ajax_handler use the same controller.TfAction class
which would make generating the Ajax action trick using the NetBeans wizards.
<html:form action="/tf_ajax_handler" onsubmit="tf();return false;"
styleId="tf" >
The action is not actually called. This is because when the form
is submitted the JavaScript function
tf() is called. The final return false statement
ensures that the action is not called.
The reason we need action="/tf_ajax_handler" is so that
Struts requires it so that it
can match the form activatation with the TfAjaxHandler ActionForm Bean
using information in the action mapping
in the struts-config.xml file.
function tf() {
new Ajax.Request(
'${pageContext.request.contextPath}/tf_ajax_handler.do',
{
method: 'get',
parameters: $('tf').serialize(true),
onSuccess: function(xhrq) {
var response = xhrq.responseText || "no response text";
$('resp').innerHTML = response
},
onFailure: function(){ alert('Something went wrong...') }
}
);
}
Note that we must call the /tf_ajax_handler action
in a very strict way, expression
the full path to it, along with the ".do" extension.
The form's id, which is tf,
is expressed by the Struts styleId attribute (a misnomer!).
Using this id value, the parameter are all passed via the option:
parameters: $('tf').serialize(true),
Upon server-side complete,
the server response is placed in the div below the form:
<div id="resp"></div>
<action
input="/ajax-out.jsp"
name="TfAjaxHandler"
path="/tf_ajax_handler"
scope="session"
type="controller.TfAction">
<forward name="success" path="/ajax-out.jsp"/>
</action>
This ensures that in all cases, whether success is achieved
(and forward is used) or not (validation fails and input is used),
that the Ajax server-side call will call the script ajax-out.jsp
<%@taglib prefix="html" uri="http://jakarta.apache.org/struts/tags-html" %>
<%@taglib prefix="logic" uri="http://jakarta.apache.org/struts/tags-logic" %>
<logic:messagesNotPresent>
name: ${param.name}
</logic:messagesNotPresent>
<html:errors />
permits the desired behavior to happen and be presented in the
div below the form. If success is reached, the html:errors
tag is empty and the logic:messagesNotPresent will be active, allowing
us to display the body.
If validation fails, and there are errors,
the logic:messagesNotPresent will be inactive and
html:errors will display the errors.