本文主要介绍J2EE开发过程中,后台的response通过alert形式显示。(如:后台判断用户在db中不存在,前端alert提示“用户不存在”)
原理是前端画面onload时,通过js来判断errorMessage是否为空,不为空显示alert。
jsp 代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/tags/struts-logic" prefix="l" %>
<h:form action="bsm01DaLianInit.do">
<SCRIPT language="javascript">
function test(){
<l:empty name="bsm01DaLianForm" property="errorMessage">
alert("没有错误");
</l:empty>
<l:notEmpty name="bsm01DaLianForm" property="errorMessage">
alert("有错误");
</l:notEmpty>
}
window.onload = test;
</script>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
页面内容
</td>
</tr>
</table>
</h:form>
public class Bsm01DaLianForm implements Serializable {
/** serialVersionUID */
private static final long serialVersionUID = 1L;
/** 错误消息 */
private String errorMessage;
/**
* @return errorMessage を戻す。
*/
public String geterrorMessage() {
return errorMessage;
}
/**
* @param errorMessage
* errorMessage を設定する。
*/
public void seterrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
}