一、前端解决
1、使用javascript代码验证,为空不允许提交。就算验证成功也不是表单提交成功了,只是肯定前端传来的数据是正确的。
2、使用ajax方式,action处理完成返回一个成功消息就可以了。失败就返回失败消息。(每个提交时都要用ajax,太麻烦)
二、后台解决
3、使用struts2自带功能。判断完参数后,如果为空,return到一个错误页面error,正常的话return到一个提交成功页面success。
第一种:在struts2 action中弹出Js脚本提示信息
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// 让action实现ServletResponseAware接口,同时实现其setServletResponse() 方法 import org.apache.struts2.interceptor.ServletResponseAware; public class StudentAction extends ActionSupport implements ServletResponseAware{ private javax.servlet.http.HttpServletResponse response; public String addStudent() throws IOException{ response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码 PrintWriter out = response.getWriter(); studentDAO=new StudentDAO(); flag=studentDAO.addStudent(stu); if(flag){ out.print("<script>alert('添加成功!')</script>"); out.print("<script>window.location.href='http://ykdreaming.blog.163.com/blog/stuaction.action'</script>"); out.flush(); out.close(); return null; }else{ return "error"; } } } // 获得HttpServletResponse对象 public void setServletResponse(HttpServletResponse response) { this.response = response; }
第二种:把错误信息放入request里面,然后在页面上判断此信息是否存在,存在的话就弹出
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// action: request.setAttribute("tipMessage", "提交成功!");// (tipMessage是自定义的) // 在当前提交的页面里加上如下一段js: <script type="text/javascript"> var msg="${requestScope.tipMessage}"; if(msg!=""){ alert(msg); } </script>
第三种 action 自带错误提示功能(addActionMessage)
// action this.addActionMessage("提交成功!"); // 页面上: <s:if test="hasActionMessages()"> <s:iterator value="actionMessages"> <script language="JavaScript"> alert('<s:property/>'); </script> </s:iterator> </s:if>
改进:在需要显示消息的页面加入include来引用message.jsp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
message.jsp代码如下 <%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <body> //fielderror <s:if test="hasFieldErrors()"> <s:iterator value="fieldErrors"> <s:iterator value="value" status="statu"> //field的错误消息是List 因此还需要迭代一次 <s:set name="index" value="#statu.index"/> <s:set name="msg" value="#msg==null?'':#msg+ value.get(#request.index).toString()+'\ '"/>//关键代码 </s:iterator> </s:iterator> </s:if> //actionerror <s:if test="hasActionErrors()"> <s:iterator value="actionErrors"> <script language="JavaScript"> alert("<s:property escape="false"/>") </script> </s:iterator> </s:if> //actionmessage <s:if test="hasActionMessages()"> <s:iterator value="actionMessages"> <script language="JavaScript"> alert("<s:property escape="false"/>") </script> </s:iterator> </s:if> //打印转换好格式的fielderror信息 <s:if test="#msg.length()>0"> <script language="JavaScript"> alert("<s:property escape='false' value='#msg'/>") </script> </s:if> </body> </html>
4、XML配置校验(表单验证,页面上不用加html代码显示错误信息)
拦截器和Action放一个包下 起名规则:1、UserAction-validation.xml (Action类名-validation.xml)对这个action中所有方法进行验证 2、UserAction-user_add-validation.xml (Action类名-action名-validation.xml)对指定的方法进行验证 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> <validators> <field name="loginName"> <field-validator type="requiredstring"> <message>用户名不能为空</message> </field-validator> <field-validator type="stringlength"> <param name="maxLength">12</param> <param name="minLength">3</param> <message>用户名长度必须在${minLength}到${maxLength}之间</message> </field-validator> </field> <field name="loginPwd"> <field-validator type="requiredstring"> <message>密码不能为空</message> </field-validator> </field> </validators>
5、注解配置校验
来源: https://blog.csdn.net/zhang527836447/article/details/11796937