📄 beanwritertag.java
字号:
/* ====================================================================
* $Id$
* ====================================================================
* 文件名 BeanWriterTag.java
* 机能名 写入Bean属性的标签定义。
* 履历 2005-1-26 dlxu 创建新文件
* Copyright 2004 东南大学 All Rights Reserved
* ====================================================================
*/
package cn.edu.seu.album.common;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
/**
* <p> [概 要] 写入Bean属性的标签定义。</p>
* <p> [详 细] 写入Bean属性的标签定义。</p>
* <p> [备 考] 无。</p>
*
* @author dlxu
* @version 1.0 2005-1-26
* @since 1.0
*/
public class BeanWriterTag extends TagSupport {
/**
* name名。
*/
private String name = null;
/**
* property名。
*/
private String property = null;
/**
* submit名。
*/
private String submit = null;
/**
* nullは 改まる
*/
private String convert = null;
/**
* spanを生成する。
*/
private String id = null;
/**
* "\r\n"To <br>
*/
private String wrap = null;
/**
* ブランクのフラグ
*/
private String noSpace = null;
/**
* scopeの定義。
*/
private String scope = null;
/**
* 设定convert。
*
* @param convert 设定convert。
*/
public void setConvert(String convert) {
this.convert = convert;
}
/**
* 设定id。
*
* @param id 设定id。
*/
public void setId(String id) {
this.id = id;
}
/**
* 设定name。
*
* @param name 设定name。
*/
public void setName(String name) {
this.name = name;
}
/**
* 设定noSpace。
*
* @param noSpace 设定noSpace。
*/
public void setNoSpace(String noSpace) {
this.noSpace = noSpace;
}
/**
* 设定property。
*
* @param property 设定property。
*/
public void setProperty(String property) {
this.property = property;
}
/**
* 设定scope。
*
* @param scope 设定scope。
*/
public void setScope(String scope) {
this.scope = scope;
}
/**
* 设定submit。
*
* @param submit 设定submit。
*/
public void setSubmit(String submit) {
this.submit = submit;
}
/**
* 设定wrap。
*
* @param wrap 设定wrap。
*/
public void setWrap(String wrap) {
this.wrap = wrap;
}
/**
* メッセージタグを生成する。
*
* @return int 処理結果
*
* @exception JspException if a JSP exception has occurred
*/
public int doEndTag() throws JspException {
try {
// log.debug("-------------------------------------------");
// オブジェクトの定義。
Object obj = null;
// log.debug("The scope is:" + scope);
// オブジェクトを見つかる。
if (null == scope) {
// オブジェクトをページscopeにあるかどうか。
obj = pageContext.getAttribute(name, PageContext.PAGE_SCOPE);
if (null == obj) {
/*
* log .debug("The bean " + name + " is not in the page
* scope!");
*/
// オブジェクトをリクエストscopeにあるかどうか。
obj = pageContext.getAttribute(name,
PageContext.REQUEST_SCOPE);
}
if (null == obj) {
/*
* log.debug("The bean " + name + " is not in the request
* scope!");
*/
// オブジェクトをセッションscopeにあるかどうか。
obj = pageContext.getAttribute(name,
PageContext.SESSION_SCOPE);
}
if (null == obj) {
/*
* log.debug("The bean " + name + " is not in the session
* scope!");
*/
throw new JspException("Can not find the bean:" + name);
}
} else {
if (scope.equalsIgnoreCase("page")) {
// オブジェクトをページscopeに見つかる。
obj = pageContext
.getAttribute(name, PageContext.PAGE_SCOPE);
} else if (scope.equalsIgnoreCase("request")) {
// オブジェクトをリクエストscopeに見つかる。
obj = pageContext.getAttribute(name,
PageContext.REQUEST_SCOPE);
} else if (scope.equalsIgnoreCase("session")) {
// オブジェクトをセッションscopeに見つかる。
obj = pageContext.getAttribute(name,
PageContext.SESSION_SCOPE);
} else {
throw new JspException("The scope type is wrong");
}
}
Class objClass = obj.getClass();
String methodName = null;
// Parameterの種類。
Class[] paraTypes = new Class[0];
// Parameterの作成。
Object[] paras = new Object[0];
// 子属性かどうかあるをチェックする。
if (-1 == property.indexOf(".")) {
// 属性の取得メソッド名を取得する。
methodName = property.substring(0, 1).toUpperCase()
+ property.substring(1);
methodName = "get" + methodName;
}
methodName = property;
// log.debug("The Bean type is :" + objClass.getName());
// log.debug("The property is:" + property);
// 最後の属性を取得する。
while (-1 != methodName.indexOf(".")) {
// 属性の取得メソッド名を取得する。
String nowMethodName = property.substring(0, methodName
.indexOf("."));
nowMethodName = methodName.substring(0, 1).toUpperCase()
+ methodName.substring(1, methodName.indexOf("."));
nowMethodName = "get" + nowMethodName;
// 内層の属性のオブジェクトを取得する。
obj = objClass.getMethod(nowMethodName, paraTypes).invoke(obj,
paras);
// 内層の属性のオブジェクトのタイプを取得する。
objClass = obj.getClass();
// 内層の属性の名前を取得する。
methodName = methodName.substring(methodName.indexOf(".") + 1);
}
methodName = methodName.substring(0, 1).toUpperCase()
+ methodName.substring(1);
methodName = "get" + methodName;
// log.debug("The methodName is:" + methodName);
// 目的のオブジェクトを取得する。
Object valueObject = objClass.getMethod(methodName, paraTypes)
.invoke(obj, paras);
String value = null;
if (null != valueObject) {
value = valueObject.toString();
}
if ("true".equalsIgnoreCase(noSpace)) {
value = filter(value);
} else if (!"false".equalsIgnoreCase(convert)) {
// 出力Stringを格式する。
value = toHTMLOutStr(value);
}
if (null == value) {
value = "";
}
if ("true".equalsIgnoreCase(wrap)) {
value = BeanWriterTag.replaceStr(value, "\r\n", "<br>");
}
// OutPutterを取得する。
JspWriter out = pageContext.getOut();
if (null != id) {
String temp = "<span id=\"" + id + "\">" + value + "</span>";
value = temp;
}
// 情報を出力する。
out.print(value);
if ("true".equalsIgnoreCase(submit)) {
StringBuffer sb = new StringBuffer();
sb.append("<input type=\"hidden\" name=\"");
if (-1 == property.indexOf(".")) {
sb.append(property);
} else {
sb
.append(property.substring(property
.lastIndexOf(".") + 1));
}
sb.append("\" value=\"");
sb.append(value);
sb.append("\"/>");
out.print(sb.toString());
}
// log.debug("The value is:" + value);
} catch (JspException e) {
throw e;
} catch (Exception e1) {
throw new JspException(e1);
}
// 次のページを戻る。
return super.doEndTag();
}
/**
* HTML文字に変換する。
*
* @param sIn 指定文字列
* @return 変換後の文字列
*/
private static String toHTMLOutStr(String sIn) {
if ((null == sIn) || ("".equals(sIn.trim()))) {
return " ";
}
return filter(sIn);
}
/**
* 文字列の入れ替わり処理を行う
*
* @param src 処理対象文字列
* @param sFnd 入れ替わり元の文字
* @param sRep 入れ替わり先の文字
* @return String 入れ替わり処理後の文字列
*/
private static String replaceStr(String src, String sFnd, String sRep) {
if (null == src || null == sFnd || null == sRep) {
return src;
}
StringBuffer sTemp = new StringBuffer();
int endIndex = 0;
int beginIndex = 0;
do {
endIndex = src.indexOf(sFnd, beginIndex);
if (0 <= endIndex) { //mean found it.
sTemp.append(src.substring(beginIndex, endIndex));
sTemp.append(sRep);
beginIndex = endIndex + sFnd.length();
} else if (0 <= beginIndex) {
sTemp.append(src.substring(beginIndex));
break;
}
} while (0 <= endIndex);
return sTemp.toString();
}
/**
* HTML文字に変換する。
*
* @param sIn 指定文字列
* @return 変換後の文字列
*/
private static String filter(String sIn) {
if (null == sIn) {
return "";
}
final char[] content = new char[sIn.length()];
sIn.getChars(0, sIn.length(), content, 0);
StringBuffer result = new StringBuffer(content.length + 50);
for (int i = 0; i < content.length; i++) {
switch (content[i]) {
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '&':
result.append("&");
break;
case '"':
result.append(""");
break;
case ' ':
result.append(" ");
break;
default:
result.append(content[i]);
}
}
return result.toString();
}
/**
* Release all allocated resources.
*/
public void release() {
super.release();
this.name = null;
this.property = null;
this.scope = null;
this.submit = null;
this.id = null;
this.wrap = null;
this.noSpace = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -