有關防止注入

在controller層中添加防止注入的代碼html

@InitBinder
	public void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(String.class, new StringEditor());
	}

其中StringEditor的實體類以下java

import java.beans.PropertyEditorSupport;
import org.springframework.web.util.HtmlUtils;

public class StringEditor extends PropertyEditorSupport {

	public StringEditor() {
		super();
	}

	@Override
	public void setAsText(String text) {
		if (text == null) {
			setValue(null);
		} else {
			String value = HtmlUtils.htmlEscape(text);
//			if (escapeHTML) {
//			}
//			if (escapeJavaScript) {
//				value = StringEscapeUtils.escapeJavaScript(value);
//			}
//			if (escapeSQL) {
//				value = StringEscapeUtils.escapeSql(value);
//			}
			setValue(value);
		}

	}

	@Override
	public String getAsText() {
		Object value = getValue();
		return value!= null ? value.toString() : "";
	}
}

有關WebDataBinder的內容見該連接:http://blog.csdn.net/hongxingxiaonan/article/details/50282001web

相關文章
相關標籤/搜索