分享
 
 
 

一个webwork下功能强大的FormTag实现

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

上篇html标记看法中说到的tag标记实现,现在有了雏形,个人比较喜欢webwork,因此就写了个webwork兼容的tag实现。

只需要在表单中修改form标记为<wwx:form ... ,所有的表单域属性都会自动帮定,无须改动原有html标记,也就是说不需要引入<ww:textfield>等标记,当然webwork的表单域标记中的自动错误输出我没实现,因为我不太喜欢这样的方式输出错误信息,如果想输出可以使用<ww:text>标记,此外这个form标记还可以自动输出webwork中客户端验证的javascript脚本。

当然如果你喜欢在webwork中用jstl的话还得写个JSTLFilter。

package net.libo.web.taglib;

import java.io.Writer;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.JspWriter;

import javax.servlet.jsp.tagext.BodyContent;

import org.apache.velocity.Template;

import org.apache.velocity.app.VelocityEngine;

import org.apache.velocity.context.Context;

import com.opensymphony.webwork.ServletActionContext;

import com.opensymphony.webwork.config.Configuration;

import com.opensymphony.webwork.views.jsp.ParametereizedBodyTagSupport;

import com.opensymphony.webwork.views.jsp.TagUtils;

import com.opensymphony.webwork.views.util.JavaScriptValidationHolder;

import com.opensymphony.webwork.views.util.UrlHelper;

import com.opensymphony.webwork.views.velocity.AbstractTagDirective;

import com.opensymphony.webwork.views.velocity.VelocityManager;

import com.opensymphony.xwork.ActionContext;

import com.opensymphony.xwork.ObjectFactory;

import com.opensymphony.xwork.config.ConfigurationManager;

import com.opensymphony.xwork.config.entities.ActionConfig;

import com.opensymphony.xwork.util.OgnlValueStack;

import net.libo.web.util.HtmlEncoder;

import net.libo.web.util.HtmlUtils;

/**

* @author Boyce

*

* Form tag populates a included form with values and

* generates the client-side validation script.

*/

public class FormTag extends ParametereizedBodyTagSupport {

final public static String OPEN_TEMPLATE = "form.vm";

final public static String TEMPLATE = "form-close.vm";

Class actionClass;

String actionName;

JavaScriptValidationHolder javaScriptValidationHolder;

String nameAttr;

String actionAttr;

String targetAttr;

String enctypeAttr;

String methodAttr;

String namespaceAttr;

String validateAttr;

//~ Methods ////////////////////////////////////////////////////////////////

public void setAction(String action) {

this.actionAttr = action;

}

public void setName(String name) {

this.nameAttr = name;

}

public void setTarget(String target) {

this.targetAttr = target;

}

public Class getActionClass() {

return actionClass;

}

public String getActionName() {

return actionName;

}

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;

}

protected String getDefaultTemplate() {

return TEMPLATE;

}

public String getDefaultOpenTemplate() {

return OPEN_TEMPLATE;

}

public int doStartTag() {

try {

evaluateParams(getStack());

mergeTemplate(getDefaultOpenTemplate());

} catch (Exception e) {

return SKIP_PAGE;

}

return EVAL_BODY_AGAIN;

}

/**

* Performs smart form population.

*

* @return SKIP_BODY

*/

public int doAfterBody() {

BodyContent body = getBodyContent();

try {

JspWriter out = body.getEnclosingWriter();

String bodytext = body.getString();

bodytext = populateForm(bodytext, getStack());

out.print(bodytext);

} catch (Exception ex) {

ex.printStackTrace();

}

return SKIP_BODY;

}

/**

* End of tag.

*

* @return EVAL_PAGE

* @throws JspException

*/

public int doEndTag() throws JspException {

try {

try {

evaluateParams(getStack());

mergeTemplate(this.getDefaultTemplate());

return EVAL_PAGE;

} catch (Exception e) {

throw new JspException("Fatal exception caught in " + this.getClass().getName() + " tag class, doEndTag: " + e.getMessage(), e);

}

} finally {

// clean up after ourselves to allow this tag to be reused

this.reset();

}

}

protected void mergeTemplate(String templateName) throws Exception {

// initialize the VelocityEngine

// this may happen more than once, but it's not a big deal

VelocityManager velocityManager = VelocityManager.getInstance();

velocityManager.init(pageContext.getServletContext());

VelocityEngine velocityEngine = velocityManager.getVelocityEngine();

Template t = velocityEngine.getTemplate(templateName);

Context context = velocityManager.createContext(getStack(),

(HttpServletRequest) pageContext.getRequest(),

(HttpServletResponse) pageContext.getResponse());

Writer outputWriter = (Writer) ActionContext.getContext().get(AbstractTagDirective.VELOCITY_WRITER);

if (outputWriter == null) {

outputWriter = pageContext.getOut();

}

context.put("tag", this);

context.put("parameters", getParameters());

t.merge(context, outputWriter);

}

public void evaluateParams(OgnlValueStack 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();

}

final String action = (String) findString(actionAttr);

String namespace;

if (namespaceAttr == null) {

namespace = TagUtils.buildNamespace(getStack(), (HttpServletRequest) pageContext.getRequest());

} else {

namespace = findString(namespaceAttr);

}

if (namespace == null) {

namespace = "";

}

final ActionConfig actionConfig = ConfigurationManager.getConfiguration().getRuntimeConfiguration().getActionConfig(namespace, action);

if (actionConfig != null) {

try {

actionClass = ObjectFactory.getObjectFactory().getClassInstance(actionConfig.getClassName());

} catch (ClassNotFoundException e) {

// this is ok

}

actionName = action;

String result = UrlHelper.buildUrl(namespace + "/" + action + "." + Configuration.get("webwork.action.extension"), request, response, null);

addParameter("action", result);

addParameter("namespace", namespace);

// if the name isn't specified, use the action name

if (nameAttr == null) {

addParameter("name", action);

}

// if the id isn't specified, use the action name

if (id == null) {

addParameter("id", action);

}

} else if (action != null) {

String result = UrlHelper.buildUrl(action, request, response, null);

addParameter("action", result);

// namespace: cut out anything between the start and the last /

int slash = result.lastIndexOf('/');

if (slash != -1) {

addParameter("namespace", result.substring(0, slash));

} else {

addParameter("namespace", "");

}

// name/id: cut out anything between / and . should be the id and name

if (id == null) {

slash = result.lastIndexOf('/');

int dot = result.indexOf('.', slash);

if (dot != -1) {

id = result.substring(slash + 1, dot);

} else {

id = result.substring(slash + 1);

}

addParameter("id", id);

}

}

// only create the javaScriptValidationHolder if the actionName,and class is known

// and the javaScriptValidationHolder hasn't been created already

// i.e. don'r re-create it on the second call to evaluateExtraParams

if (actionName != null && actionClass != null && javaScriptValidationHolder == null) {

javaScriptValidationHolder = new JavaScriptValidationHolder(actionName, actionClass, getStack());

}

}

if (nameAttr != null) {

addParameter("name", findString(nameAttr));

}

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");

}

}

// ---------------------------------------------------------------- populate

private String populateForm(String html, OgnlValueStack valueStack) {

int i = 0, s = 0;

StringBuffer result = new StringBuffer(html.length());

String currentSelectName = null;

while (true) {

// find starting tag

i = html.indexOf('<', s);

if (i == -1) {

result.append(html.substring(s));

break; // input tag not found

}

result.append(html.substring(s, i)); // tag found, all before tag is stored

s = i;

// find closing tag

i = html.indexOf('>', i);

if (i == -1) {

result.append(html.substring(s));

break; // closing tag not found

}

i++;

// match tags

String tag = html.substring(s, i);

//System.out.println(tag);

String tagName = HtmlUtils.getTagName(tag);

if (tagName.equalsIgnoreCase("input") == true) {

String tagType = HtmlUtils.getAttribute(tag, "type");

if (tagType != null) {

String name = HtmlUtils.getAttribute(tag, "name");

HashMap params = new HashMap();

params.put("name", name);

javaScriptValidationHolder.registerValidateField((String) name, params);

Object vobj = valueStack.findValue(name);

if (vobj != null) {

String value = vobj.toString();

tagType = tagType.toLowerCase();

if (tagType.equals("text")) {

tag = HtmlUtils.addAttribute(tag, "value", value);

} if (tagType.equals("hidden")) {

tag = HtmlUtils.addAttribute(tag, "value", value);

} if (tagType.equals("image")) {

tag = HtmlUtils.addAttribute(tag, "value", value);

} if (tagType.equals("password")) {

tag = HtmlUtils.addAttribute(tag, "value", value);

} if (tagType.equals("checkbox")) {

String tagValue = HtmlUtils.getAttribute(tag, "value");

if (tagValue == null) {

tagValue = "true";

}

if (tagValue.equals(value)) {

tag = HtmlUtils.addAttribute(tag, "checked");

}

} if (tagType.equals("radio")) {

String tagValue = HtmlUtils.getAttribute(tag, "value");

if (tagValue != null) {

if (tagValue.equals(value)) {

tag = HtmlUtils.addAttribute(tag, "checked");

}

}

}

}

}

} else if (tagName.equalsIgnoreCase("textarea") == true) {

String name = HtmlUtils.getAttribute(tag, "name");

Object value = valueStack.findValue(name);

if (value != null) {

if (value != null) {

tag += HtmlEncoder.encode(value.toString());

}

}

} else if (tagName.equalsIgnoreCase("select") == true) {

currentSelectName = HtmlUtils.getAttribute(tag, "name");

} else if (tagName.equalsIgnoreCase("/select") == true) {

currentSelectName = null;

} else if (tagName.equalsIgnoreCase("option") == true) {

if (currentSelectName != null) {

String tagValue = HtmlUtils.getAttribute(tag, "value");

if (tagValue != null) {

Object vals = valueStack.findValue(currentSelectName);

if (vals != null) {

if (vals.getClass().isArray() == false) {

String value = vals.toString();

if (value.equals(tagValue)) {

tag = HtmlUtils.addAttribute(tag, "selected");

}

} else {

Object[] arr = (Object[])vals;

for (int j=0;j<arr.length;j++) {

arr[j] = arr[j].toString();

}

String vs[] = (String[])arr;

for (int k = 0; k < vs.length; k++) {

String vsk = vs[k];

if (vsk != null) {

if (vsk.equals(tagValue)) {

tag = HtmlUtils.addAttribute(tag, "selected");

}

}

}

}

}

}

}

}

result.append(tag);

s = i;

}

return result.toString();

}

protected void reset() {

this.getParameters().clear();

javaScriptValidationHolder = null;

if (getActionName() != null && getActionClass() != null) {

javaScriptValidationHolder = new JavaScriptValidationHolder(getActionName(), getActionClass(), getStack());

}

}

}

还有两个util类就不放到这了,tld说明如下:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">

<taglib>

<tlibversion>1.1</tlibversion>

<jspversion>1.2</jspversion>

<shortname>wwx</shortname>

<uri>wwx</uri>

<tag>

<name>form</name>

<tagclass>net.libo.web.taglib.FormTag</tagclass>

<bodycontent>JSP</bodycontent>

<info>An HTML Component UI widget</info>

<attribute>

<name>name</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>action</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>target</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>namespace</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>method</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>enctype</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>validate</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

</tag>

</taglib>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有