<%-- contactdispatch.jsp --%>
<%!
private static String[] formFields = {"name","email","textarea"};
private void doBeforeRender(HstRequest request, HstResponse response) throws HstComponentException {
HttpSession session = request.getSession(true);
FormMap formMap = (FormMap) session.getAttribute("contactdispatch:formMap");
if (formMap == null) {
formMap = new FormMap();
session.setAttribute("contactdispatch:formMap", formMap);
}
request.setAttribute("form", formMap);
}
private void doAction(HstRequest request, HstResponse response) throws HstComponentException {
HttpSession session = request.getSession(true);
FormMap formMap = new FormMap(request, formFields);
session.setAttribute("contactdispatch:formMap", formMap);
// Do a really simple validation:
if (formMap.getField("email") != null && formMap.getField("email").contains("@")) {
// success
// do your business logic
// possible do a redirect to a thankyou page: do not use directly response.sendRedirect;
HstSiteMapItem item = request.getRequestContext().getResolvedSiteMapItem().getHstSiteMapItem().getChild("thankyou");
if (item != null) {
sendRedirect(request, response, item.getId());
} else {
log.warn("Cannot redirect because siteMapItem not found. ");
}
} else {
// validation failed. Persist form map, and add possible error messages to the formMap
formMap.addMessage("email", "Email address must contain '@'");
}
}
private void sendRedirect(HstRequest request, HstResponse response, String redirectToSiteMapItemId) {
HstLinkCreator linkCreator = request.getRequestContext().getHstLinkCreator();
HstSiteMap siteMap = request.getRequestContext().getResolvedSiteMapItem().getHstSiteMapItem().getHstSiteMap();
HstLink link = linkCreator.create(siteMap.getSiteMapItemById(redirectToSiteMapItemId));
StringBuffer url = new StringBuffer();
for (String elem : link.getPathElements()) {
String enc = response.encodeURL(elem);
url.append("/").append(enc);
}
String urlString = ((HstResponse) response).createNavigationalURL(url.toString()).toString();
try {
response.sendRedirect(urlString);
} catch (IOException e) {
throw new HstComponentException("Could not redirect. ",e);
}
}
%>
<%
HstRequest hstRequest = (HstRequest) request;
HstResponse hstResponse = (HstResponse) response;
String hstRequestLifecyclePhase = hstRequest.getLifecyclePhase();
String dispatchLifecyclePhase = (String) hstRequest.getAttribute(SimpleDispatcherHstComponent.LIFECYCLE_PHASE_ATTRIBUTE);
if (HstRequest.ACTION_PHASE.equals(hstRequestLifecyclePhase)) {
doAction(hstRequest, hstResponse);
} else if (SimpleDispatcherHstComponent.BEFORE_RENDER_PHASE.equals(dispatchLifecyclePhase)) {
doBeforeRender(hstRequest, hstResponse);
} else if (HstRequest.RENDER_PHASE.equals(hstRequestLifecyclePhase)) {
%>
<
div
>
<
form
method
=
"POST"
name
=
"myform"
action="<hst:actionURL/>">
<
input
type
=
"hidden"
name
=
"previous"
value
=
"${form.previous}"
/>
<
table
>
<
tr
>
<
td
>Name</
td
>
<
td
><
input
type
=
"text"
name
=
"name"
value
=
"${form.value['name']}"
/></
td
>
<
td
><
font
style
=
"color:red"
>${form.message['name']}</
font
></
td
>
</
tr
>
<
tr
>
<
td
>Email</
td
>
<
td
><
input
type
=
"text"
name
=
"email"
value
=
"${form.value['email']}"
/></
td
>
<
td
><
font
style
=
"color:red"
>${form.message['email']}</
font
></
td
>
</
tr
>
<
tr
>
<
td
>Text</
td
>
<
td
><
textarea
name
=
"textarea"
>${form.value['textarea']}</
textarea
></
td
>
<
td
><
font
style
=
"color:red"
>${form.message['textarea']}</
font
></
td
>
</
tr
>
<
tr
>
<
td
>
<
c:if
test
=
"${form.previous != null}"
>
<
input
type
=
"submit"
name
=
"prev"
value
=
"prev"
/>
</
c:if
>
</
td
>
<
td
><
input
type
=
"submit"
value
=
"send"
/></
td
>
</
tr
>
</
table
>
</
form
>
</
div
>
<% } %>