You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by dg...@apache.org on 2003/04/23 02:01:19 UTC
cvs commit: jakarta-struts/src/share/org/apache/struts/taglib/html FormTag.java
dgraham 2003/04/22 17:01:18
Modified: src/share/org/apache/struts/taglib/html FormTag.java
Log:
Refactored code into smaller reusable methods.
Revision Changes Path
1.46 +160 -124 jakarta-struts/src/share/org/apache/struts/taglib/html/FormTag.java
Index: FormTag.java
===================================================================
RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/FormTag.java,v
retrieving revision 1.45
retrieving revision 1.46
diff -u -r1.45 -r1.46
--- FormTag.java 10 Mar 2003 01:57:30 -0000 1.45
+++ FormTag.java 23 Apr 2003 00:01:18 -0000 1.46
@@ -506,18 +506,84 @@
lookup();
// Create an appropriate "form" element based on our parameters
- HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
+ StringBuffer results = new StringBuffer();
+
+ results.append(this.renderFormStartElement());
+
+ results.append(this.renderToken());
+
+ // Print this field to our output writer
+ ResponseUtils.write(pageContext, results.toString());
+
+ // Store this tag itself as a page attribute
+ pageContext.setAttribute(Constants.FORM_KEY, this, PageContext.REQUEST_SCOPE);
+
+ // Locate or create the bean associated with our form
+ int scope = PageContext.SESSION_SCOPE;
+ if ("request".equals(beanScope)) {
+ scope = PageContext.REQUEST_SCOPE;
+ }
+ Object bean = pageContext.getAttribute(beanName, scope);
+ if (bean == null) {
+ if (type != null) {
+ // Backwards compatibility - use explicitly specified values
+ try {
+ bean = RequestUtils.applicationInstance(beanType);
+ if (bean != null) {
+ ((ActionForm) bean).setServlet(servlet);
+ }
+ } catch (Exception e) {
+ throw new JspException(
+ messages.getMessage("formTag.create", type, e.toString()));
+ }
+ } else {
+ // New and improved - use the values from the action mapping
+ bean =
+ RequestUtils.createActionForm(
+ (HttpServletRequest) pageContext.getRequest(),
+ mapping,
+ moduleConfig,
+ servlet);
+ }
+ if (bean instanceof ActionForm) {
+ ((ActionForm) bean).reset(mapping, (HttpServletRequest) pageContext.getRequest());
+ }
+ if (bean == null) {
+ throw new JspException(messages.getMessage("formTag.create", beanType));
+ }
+ pageContext.setAttribute(beanName, bean, scope);
+ }
+ pageContext.setAttribute(Constants.BEAN_KEY, bean, PageContext.REQUEST_SCOPE);
+
+ // Continue processing this page
+ return (EVAL_BODY_INCLUDE);
+
+ }
+
+ /**
+ * Generates the opening <code><form></code> element with appropriate
+ * attributes.
+ * @since Struts 1.1
+ */
+ protected String renderFormStartElement() {
+ HttpServletResponse response =
+ (HttpServletResponse) this.pageContext.getResponse();
+
StringBuffer results = new StringBuffer("<form");
results.append(" name=\"");
results.append(beanName);
results.append("\"");
- results.append(" method=\"");
- results.append(method == null ? "post" : method);
- results.append("\" action=\"");
- results.append(response.encodeURL(RequestUtils.getActionMappingURL(action, pageContext)));
- results.append("\"");
- if (styleClass != null) {
- results.append(" class=\"");
+ results.append(" method=\"");
+ results.append(method == null ? "post" : method);
+ results.append("\" action=\"");
+ results.append(
+ response.encodeURL(
+ RequestUtils.getActionMappingURL(this.action, this.pageContext)));
+
+ results.append("\"");
+
+ if (styleClass != null) {
+ results.append(" class=\"");
results.append(styleClass);
results.append("\"");
}
@@ -552,11 +618,22 @@
results.append("\"");
}
results.append(">");
+ return results.toString();
+ }
- // Add a transaction token (if present in our session)
+ /**
+ * Generates a hidden input field with token information, if any.
+ * @return A hidden input field containing the token.
+ * @since Struts 1.1
+ */
+ protected String renderToken() {
+ StringBuffer results = new StringBuffer();
HttpSession session = pageContext.getSession();
+
if (session != null) {
- String token = (String) session.getAttribute(Globals.TRANSACTION_TOKEN_KEY);
+ String token =
+ (String) session.getAttribute(Globals.TRANSACTION_TOKEN_KEY);
+
if (token != null) {
results.append("<input type=\"hidden\" name=\"");
results.append(Constants.TOKEN_KEY);
@@ -570,52 +647,7 @@
}
}
- // Print this field to our output writer
- ResponseUtils.write(pageContext, results.toString());
-
- // Store this tag itself as a page attribute
- pageContext.setAttribute(Constants.FORM_KEY, this, PageContext.REQUEST_SCOPE);
-
- // Locate or create the bean associated with our form
- int scope = PageContext.SESSION_SCOPE;
- if ("request".equals(beanScope)) {
- scope = PageContext.REQUEST_SCOPE;
- }
- Object bean = pageContext.getAttribute(beanName, scope);
- if (bean == null) {
- if (type != null) {
- // Backwards compatibility - use explicitly specified values
- try {
- bean = RequestUtils.applicationInstance(beanType);
- if (bean != null) {
- ((ActionForm) bean).setServlet(servlet);
- }
- } catch (Exception e) {
- throw new JspException(
- messages.getMessage("formTag.create", type, e.toString()));
- }
- } else {
- // New and improved - use the values from the action mapping
- bean =
- RequestUtils.createActionForm(
- (HttpServletRequest) pageContext.getRequest(),
- mapping,
- moduleConfig,
- servlet);
- }
- if (bean instanceof ActionForm) {
- ((ActionForm) bean).reset(mapping, (HttpServletRequest) pageContext.getRequest());
- }
- if (bean == null) {
- throw new JspException(messages.getMessage("formTag.create", beanType));
- }
- pageContext.setAttribute(beanName, bean, scope);
- }
- pageContext.setAttribute(Constants.BEAN_KEY, bean, PageContext.REQUEST_SCOPE);
-
- // Continue processing this page
- return (EVAL_BODY_INCLUDE);
-
+ return results.toString();
}
/**
@@ -634,56 +666,7 @@
// Render JavaScript to set the input focus if required
if (this.focus != null) {
-
- results.append(lineEnd);
- results.append(this.getJsStartElement());
-
- // xhtml script content shouldn't use the browser hiding trick
- if (!this.isXhtml()) {
- results.append(" <!--");
- results.append(lineEnd);
- }
-
- // Construct the control name that will receive focus.
- // This does not include any index.
- StringBuffer focusControl = new StringBuffer("document.forms[\"");
- focusControl.append(beanName);
- focusControl.append("\"].elements[\"");
- focusControl.append(this.focus);
- focusControl.append("\"]");
-
- results.append(" var focusControl = ");
- results.append(focusControl.toString());
- results.append(";");
- results.append(lineEnd);
- results.append(lineEnd);
-
- results.append(" if (focusControl.type != \"hidden\") {");
- results.append(lineEnd);
-
- // Construct the index if needed and insert into focus statement
- String index = "";
- if (this.focusIndex != null) {
- StringBuffer sb = new StringBuffer("[");
- sb.append(this.focusIndex);
- sb.append("]");
- index = sb.toString();
- }
- results.append(" focusControl");
- results.append(index);
- results.append(".focus();");
- results.append(lineEnd);
-
- results.append(" }");
- results.append(lineEnd);
-
- if (!this.isXhtml()) {
- results.append(" // -->");
- results.append(lineEnd);
- }
-
- results.append("</script>");
- results.append(lineEnd);
+ results.append(this.renderFocusJavascript());
}
// Print this value to our output writer
@@ -700,6 +683,71 @@
}
/**
+ * Generates javascript to set the initial focus to the form element given in the
+ * tag's "focus" attribute.
+ * @since Struts 1.1
+ */
+ protected String renderFocusJavascript() {
+ StringBuffer results = new StringBuffer();
+
+ results.append(lineEnd);
+ results.append("<script type=\"text/javascript\"");
+ if (!this.isXhtml()) {
+ results.append(" language=\"JavaScript\"");
+ }
+ results.append(">");
+ results.append(lineEnd);
+
+ // xhtml script content shouldn't use the browser hiding trick
+ if (!this.isXhtml()) {
+ results.append(" <!--");
+ results.append(lineEnd);
+ }
+
+ // Construct the control name that will receive focus.
+ // This does not include any index.
+ StringBuffer focusControl = new StringBuffer("document.forms[\"");
+ focusControl.append(beanName);
+ focusControl.append("\"].elements[\"");
+ focusControl.append(this.focus);
+ focusControl.append("\"]");
+
+ results.append(" var focusControl = ");
+ results.append(focusControl.toString());
+ results.append(";");
+ results.append(lineEnd);
+ results.append(lineEnd);
+
+ results.append(" if (focusControl.type != \"hidden\") {");
+ results.append(lineEnd);
+
+ // Construct the index if needed and insert into focus statement
+ String index = "";
+ if (this.focusIndex != null) {
+ StringBuffer sb = new StringBuffer("[");
+ sb.append(this.focusIndex);
+ sb.append("]");
+ index = sb.toString();
+ }
+ results.append(" focusControl");
+ results.append(index);
+ results.append(".focus();");
+ results.append(lineEnd);
+
+ results.append(" }");
+ results.append(lineEnd);
+
+ if (!this.isXhtml()) {
+ results.append(" // -->");
+ results.append(lineEnd);
+ }
+
+ results.append("</script>");
+ results.append(lineEnd);
+ return results.toString();
+ }
+
+ /**
* Release any acquired resources.
*/
public void release() {
@@ -786,22 +834,10 @@
}
/**
- * Returns the starting javascript element formatted for xhtml if needed.
- */
- private String getJsStartElement() {
- StringBuffer sb = new StringBuffer("<script type=\"text/javascript\"");
- if (!this.isXhtml()) {
- sb.append(" language=\"JavaScript\"");
- }
- sb.append(">");
- sb.append(lineEnd);
- return sb.toString();
- }
-
- /**
* Returns true if this tag should render as xhtml.
+ * @since Struts 1.1
*/
- private boolean isXhtml() {
+ protected boolean isXhtml() {
String xhtml =
(String) this.pageContext.getAttribute(Globals.XHTML_KEY, PageContext.PAGE_SCOPE);
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-dev-help@jakarta.apache.org