/*
* Copyright (c) 2002-2003 by OpenSymphony
* All rights reserved.
*/
package com.opensymphony.webwork.views.jsp.ui;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.webwork.views.util.JavaScriptValidationHolder;
import com.opensymphony.xwork.util.OgnlValueStack;
/**
* FormTag
*
* @author Jason Carreira
* Created Apr 1, 2003 8:19:47 PM
*
* Revised by Henry Hu Email: hu_pengfei@yahoo.com.cn
*/
public class FormTag extends AbstractClosingUITag {
//~ Static fields/initializers /////////////////////////////////////////////
final public static String OPEN_TEMPLATE = "form";
final public static String TEMPLATE = "form-close";
//~ Instance fields ////////////////////////////////////////////////////////
Class actionClass;
List fieldParameters;
List fieldValidators;
String actionName;
JavaScriptValidationHolder javaScriptValidationHolder;
String actionAttr;
String targetAttr;
String enctypeAttr;
String methodAttr;
String namespaceAttr;
String validateAttr;
String onsubmit;
//~ Methods ////////////////////////////////////////////////////////////////
public void setAction(String action) {
this.actionAttr = action;
}
public void setTarget(String target) {
this.targetAttr = target;
}
public Class getActionClass() {
return actionClass;
}
public String getActionName() {
return actionName;
}
public String getDefaultOpenTemplate() {
return OPEN_TEMPLATE;
}
public void setEnctype(String enctype) {
this.enctypeAttr = enctype;
}
public void setMethod(String method) {
this.methodAttr = method;
}
public void setNamespace(String namespace) {
this.namespaceAttr = namespace;
}
public void setValidate(String validate) {
this.validateAttr = validate;
}
public void setOnsubmit(String onsubmit) {
this.onsubmit = onsubmit;
}
public void evaluateExtraParams(OgnlValueStack stack) {
super.evaluateExtraParams(stack);
if (actionAttr != null) {
/**
* If called from a JSP, pageContext will not be null. otherwise, we'll get request and response from the
* ServletActionContext.
*
* todo - determine if there's any reason we can't just always use ServletActionContext
* -> because we want to be able to use the tags if we went directly to the page
*/
HttpServletResponse response;
HttpServletRequest request;
if (pageContext != null) {
response = (HttpServletResponse) pageContext.getResponse();
request = (HttpServletRequest) pageContext.getRequest();
} else {
request = ServletActionContext.getRequest();
response = ServletActionContext.getResponse();
}
/*
* Attention: in current version, the client JavaScript vildation funcation was canceled
*
* Revised for Portlet actionURL as form action, and add wwAction as hidden field.
* Refer to template.simple form.vm
*
* <form
* action="$parameters.action"
* #if ($parameters.id) id="$!webwork.htmlEncode($parameters.id)" #end
* #if ($parameters.name) name="$!webwork.htmlEncode($parameters.name)" #end
* #if ($parameters.target) target="$!webwork.htmlEncode($parameters.target)" #end
* #if ($parameters.method) method="$!webwork.htmlEncode($parameters.method)" #end
* #if ($parameters.enctype) enctype="$!webwork.htmlEncode($parameters.enctype)" #end
* #if ($parameters.cssClass) class="$!webwork.htmlEncode($parameters.cssClass)" #end
* #if ($parameters.cssStyle) style="$!webwork.htmlEncode($parameters.cssStyle)" #end
* >
* <input type="hidden" name="wwAction" value="$parameters.wwAction"/>
*/
String actionURL = com.opensymphony.webwork.portlet.context.PortletContext.getContext().getActionURL();
String wwAction = findString(actionAttr);
addParameter("action", actionURL);
addParameter("wwAction", wwAction);
// addParameter("action", findString(actionAttr));
}
if (onsubmit != null) {
addParameter("onsubmit", findString(onsubmit));
}
if (targetAttr != null) {
addParameter("target", findString(targetAttr));
}
if (enctypeAttr != null) {
addParameter("enctype", findString(enctypeAttr));
}
if (methodAttr != null) {
addParameter("method", findString(methodAttr));
}
if (validateAttr != null) {
addParameter("validate", findValue(validateAttr, Boolean.class));
}
if (javaScriptValidationHolder != null && javaScriptValidationHolder.hasValidators()) {
addParameter("javascriptValidation", javaScriptValidationHolder.toJavaScript());
} else {
addParameter("javascriptValidation", "// cannot find any applicable validators");
}
}
protected String getDefaultTemplate() {
return TEMPLATE;
}
protected boolean evaluateNameValue() {
return false;
}
/**
* Resets the attributes of this tag so that the tag may be reused. As a general rule, only
* properties that are not specified as an attribute or properties that are derived need to be
* reset. Examples of this would include the parameters Map in ParameterizedTag and the
* namespace in the ActionTag (which can be a derived value). <p /> This should be the last
* thing called as part of the doEndTag
*/
protected void reset() {
super.reset();
javaScriptValidationHolder = null;
if (getActionName() != null && getActionClass() != null) {
javaScriptValidationHolder = new JavaScriptValidationHolder(getActionName(), getActionClass(), getStack());
}
}
/**
* Provide access to the JavaScriptValidationHolder so that the AbstractUITag
* can trigger the registration of all validators.
*
* @return
*/
JavaScriptValidationHolder getJavaScriptValidationHolder() {
return javaScriptValidationHolder;
}
}