最原始的ajax实现前台验证
<script type="text/javascript">
function sub(){
var mName=document.getElementById("CDictionaryName");
if(mName.value==""){
document.getElementById("mName").innerHTML="对不起!车型不能为空!";
return false;
}else{
return true;
}
}
</script>
后台Action代码: // 根据ID查看 public ActionForward selectModelsByName(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { String cIictionaryName = request.getParameter("modelsName"); try { cIictionaryName = java.net.URLDecoder.decode(cIictionaryName,"UTF-8"); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //这句话一定要,因为如果不写的 //话,编码 就 是%E5%A6%88%System.out System.out.println(cIictionaryName+"aaaaaaaa"); //response.setContentType(arg0) try { response.setContentType("text/html;charset=utf-8"); PrintWriter out=response.getWriter(); Dictionary dictionary=new Dictionary(); dictionary.setCDictionaryFlag("1"); dictionary.setCDictionaryName(cIictionaryName); List reList=dictionaryService.findByExample(dictionary); if(reList.size()>0){ out.print("车型已存在"); }else { out.print("该车型可以使用!"); } out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }