📄 formtag.java
字号:
*/
public String getTarget() {
return (this.target);
}
/**
* Set the window target.
*
* @param target The new window target
*/
public void setTarget(String target) {
this.target = target;
}
/**
* Return the Java class of our bean.
*/
public String getType() {
return (this.type);
}
/**
* Set the Java class of our bean.
*
* @param type The new Java class
*/
public void setType(String type) {
this.type = type;
}
public String getValidate() {
return this.validate;
}
public void setValidate(String validate) {
this.validate = validate;
}
// --------------------------------------------------------- Public Methods
/**
* Render the beginning of this form.
*
* @exception JspException if a JSP exception has occurred
*/
public int doStartTag() throws JspException {
// Look up the form bean name, scope, and type if necessary
//this.lookup();
// Create an appropriate "form" element based on our parameters
StringBuffer results = new StringBuffer();
results.append(this.renderFormStartElement());
results.append(this.renderToken());
ResponseUtils.write(pageContext, results.toString());
// Store this tag itself as a page attribute
pageContext.setAttribute(
Constants.FORM_KEY,
this,
PageContext.REQUEST_SCOPE);
//this.initFormBean();
return (EVAL_BODY_INCLUDE);
}
/**
* Locate or create the bean associated with our form.
* @throws JspException
* @since Struts 1.1
*/
protected void initFormBean() throws JspException {
int scope = PageContext.SESSION_SCOPE;
if ("request".equalsIgnoreCase(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);
}
/**
* 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(name);
results.append("\"");
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("\"");
}
if (enctype != null) {
results.append(" enctype=\"");
results.append(enctype);
results.append("\"");
}
if (onreset != null) {
results.append(" onreset=\"");
results.append(onreset);
results.append("\"");
}
if (onsubmit != null) {
results.append(" onsubmit=\"");
results.append(onsubmit);
results.append("\"");
}
if (style != null) {
results.append(" style=\"");
results.append(style);
results.append("\"");
}
if (styleId != null) {
results.append(" id=\"");
results.append(styleId);
results.append("\"");
}
if (target != null) {
results.append(" target=\"");
results.append(target);
results.append("\"");
}
if ( validate!=null && validate.equals( VALIDATE_ENABLE) ) {
results.append(" onSubmit=\"");
results.append(" return validate(this);");
results.append("\"");
}
results.append(">");
return results.toString();
}
/**
* 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);
if (token != null) {
results.append("<input type=\"hidden\" name=\"");
results.append(Constants.TOKEN_KEY);
results.append("\" value=\"");
results.append(token);
if (this.isXhtml()) {
results.append("\" />");
} else {
results.append("\">");
}
}
}
return results.toString();
}
/**
* Render the end of this form.
*
* @exception JspException if a JSP exception has occurred
*/
public int doEndTag() throws JspException {
// Remove the page scope attributes we created
pageContext.removeAttribute(
Constants.BEAN_KEY,
PageContext.REQUEST_SCOPE);
pageContext.removeAttribute(
Constants.FORM_KEY,
PageContext.REQUEST_SCOPE);
// Render a tag representing the end of our current form
StringBuffer results = new StringBuffer("</form>");
// Render JavaScript to set the input focus if required
if (this.focus != null) {
results.append(this.renderFocusJavascript());
}
// Print this value to our output writer
JspWriter writer = pageContext.getOut();
try {
writer.print(results.toString());
} catch (IOException e) {
throw new JspException(
messages.getMessage("common.io", e.toString()));
}
// Continue processing this page
return (EVAL_PAGE);
}
/**
* 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() {
super.release();
action = null;
moduleConfig = null;
enctype = null;
focus = null;
focusIndex = null;
mapping = null;
method = null;
name = null;
onreset = null;
onsubmit = null;
scope = null;
servlet = null;
style = null;
styleClass = null;
styleId = null;
target = null;
type = null;
}
// ------------------------------------------------------ Protected Methods
/**
* Look up values for the <code>name</code>, <code>scope</code>, and
* <code>type</code> properties if necessary.
*
* @exception JspException if a required value cannot be looked up
*/
protected void lookup() throws JspException {
// Look up the module configuration information we need
moduleConfig = RequestUtils.getModuleConfig(pageContext);
if (moduleConfig == null) {
JspException e =
new JspException(messages.getMessage("formTag.collections"));
pageContext.setAttribute(
Globals.EXCEPTION_KEY,
e,
PageContext.REQUEST_SCOPE);
throw e;
}
servlet =
(ActionServlet) pageContext.getServletContext().getAttribute(
Globals.ACTION_SERVLET_KEY);
// Look up the action mapping we will be submitting to
String mappingName = RequestUtils.getActionMappingName(action);
mapping = (ActionMapping) moduleConfig.findActionConfig(mappingName);
if (mapping == null) {
JspException e =
new JspException(
messages.getMessage("formTag.mapping", mappingName));
pageContext.setAttribute(
Globals.EXCEPTION_KEY,
e,
PageContext.REQUEST_SCOPE);
throw e;
}
// Were the required values already specified?
if (name != null) {
if (type == null) {
JspException e =
new JspException(messages.getMessage("formTag.nameType"));
pageContext.setAttribute(
Globals.EXCEPTION_KEY,
e,
PageContext.REQUEST_SCOPE);
throw e;
}
beanName = name;
beanScope = (scope == null ? "session" : scope);
beanType = type;
return;
}
// Look up the form bean definition
FormBeanConfig formBeanConfig =
moduleConfig.findFormBeanConfig(mapping.getName());
if (formBeanConfig == null) {
JspException e =
new JspException(
messages.getMessage("formTag.formBean", mapping.getName()));
pageContext.setAttribute(
Globals.EXCEPTION_KEY,
e,
PageContext.REQUEST_SCOPE);
throw e;
}
// Calculate the required values
beanName = mapping.getAttribute();
beanScope = mapping.getScope();
beanType = formBeanConfig.getType();
}
/**
* Returns true if this tag should render as xhtml.
*/
private boolean isXhtml() {
return RequestUtils.isXhtml(this.pageContext);
}
/**
* Returns the focusIndex.
* @return String
*/
public String getFocusIndex() {
return focusIndex;
}
/**
* Sets the focusIndex.
* @param focusIndex The focusIndex to set
*/
public void setFocusIndex(String focusIndex) {
this.focusIndex = focusIndex;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -