利用Struts 2完成手动校验

xiaoxiao2026-08-17  0

利用Struts 2完成手动校验有两种方式: [color=red] (1)重写validate方法 (2)重写validateXxx方法 [/color] 首先,介绍一下重写validate方法,实现方式是通过在Action中重写ActionSupport类的validate方法就能完成客户端提交过来的数据校验。 [color=red]下面创建一个RegistValidateAction.java文件,该文件主要是对userRegister_validate.jsp页面中的打“*”的字段进行处理。界面如下图:[/color] [img]/upload/attachment/109459/5907c7c8-5ce4-340e-bb20-70a147b2319c.jpg[/img] [color=red]RegistValidateAction.java代码如下:[/color] package com.gxa.edu.struts2.action.ch6;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ActionContext;public class RegistValidateAction extends ActionSupport { private String username; private String userpassword; private String confirm_password; private String address; private String vaild; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getConfirm_password() { return confirm_password; } public void setConfirm_password(String confirm_password) { this.confirm_password = confirm_password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpassword() { return userpassword; } public void setUserpassword(String userpassword) { this.userpassword = userpassword; } public String getVaild() { return vaild; } public void setVaild(String vaild) { this.vaild = vaild; } public void validate() { System.out.println("===注册用户数据校验方法==="); if (username == null || "".equals(username)) { addFieldError("username" , "用户名不能为空,请填写"); } if (userpassword == null || "".equals(userpassword)) { addFieldError("userpassword" , "密码不能为空,请填写"); } else if (confirm_password == null || "".equals(confirm_password)) { addFieldError("confirm_password" , "确认密码不能为空,请填写"); } else if (!userpassword.equals(confirm_password)) { addFieldError("userpassword" , "第一次密码和第二次确认密码不相同,请重新输入"); } if (address == null || "".equals(address)) { addFieldError("address" , "地址不能为空,请填写"); } ActionContext ctx = ActionContext.getContext(); String rand = (String) ctx.getSession().get("rand"); if (rand == null || !rand.equals(vaild)) { addFieldError("vaild" , "验证码输入不正确,请重新输入"); } }} 上面的validate()方法是覆盖ActionSupport类中方法,该方法主要作用就是当客服端提交过来的数据,首先必须执行validate()方法来进行数据校验,一旦根据数据校验失败,就把校验失败提示信息通过addFieldError方法添加到系统的fieldError。然后通过在JSP页面中加入<s:fielderror/>,就可以在页面中输出校验失败的提示信息。 [color=red]接着,创建struts-ch6.xml,在该文件中添加如下信息:[/color] <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <constant name="struts.custom.i18n.resources" value="globalMessages"/> <package name="ch6" extends="struts-default" namespace="/ch6"> <action name="RegistValidate" class="com.gxa.edu.struts2.action.ch6.RegistValidateAction"> <result name="input">/ch6/userRegister_validate.jsp</result> </action> </package></struts> [color=red]最后还需在struts.xml文件加入一段代码:[/color] <include file="com/gxa/edu/struts2/action/ch6/struts-ch6.xml"></include> [color=red]测试结果如下图[/color] [img]/upload/attachment/109461/58cb9cd9-53bf-386f-a67d-bccdad7322bc.jpg[/img] [img]/upload/attachment/109463/35f4d586-575d-3ef7-b217-515cc3f53e33.jpg[/img] [color=red]提问:在struts.xml文件中声明method属性来指定多个逻辑处理方法,那么如果只对某一个或是多个逻辑处理方法自定义相应的数据校验功能,应该如何实现?[/color] 为了实现校验指定逻辑处理方法的功能,Struts 2框架为Action提供了validateXxx()方法。其中“Xxx”代表逻辑处理方法名。下面以一个例子来介绍重写validateXxx的使用 [color=red]第一步:在原来的RegistValidateAction.java文件中添加如下两个方法[/color] 添加regist()方法 public String regist() { return "success";} 添加validateRegist()方法 public void validateRegist() { System.out.println("===注册用户数据校验方法validateRegist==="); if (username == null || "".equals(username)) { addFieldError("username" , "用户名不能为空,请填写"); } if (userpassword == null || "".equals(userpassword)) { addFieldError("userpassword" , "密码不能为空,请填写"); } else if (confirm_password == null || "".equals(confirm_password)) { addFieldError("confirm_password" , "确认密码不能为空,请填写"); } else if (!userpassword.equals(confirm_password)) { addFieldError("userpassword" , "第一次密码和第二次确认密码不相同,请重新输入"); } if (address == null || "".equals(address)) { addFieldError("address" , "地址不能为空,请填写"); } ActionContext ctx = ActionContext.getContext(); String rand = (String) ctx.getSession().get("rand"); if (rand == null || !rand.equals(vaild)) { addFieldError("vaild" , "验证码输入不正确,请重新输入"); }} [color=red]第二步:在struts-ch6.xml文件中加入如下代码[/color] <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <constant name="struts.custom.i18n.resources" value="globalMessages"/> <package name="ch6" extends="struts-default" namespace="/ch6"> <action name="RegistValidate" class="com.gxa.edu.struts2.action.ch6.RegistValidateAction"> <result name="input">/ch6/userRegister_validate.jsp</result> </action> <action name="RegistValidate2" class="com.gxa.edu.struts2.action.ch6.RegistValidateAction" method="regist"> <result name="input">/ch6/userRegister_validate.jsp</result> </action> </package></struts> [color=red]第三步:测试结果下图[/color] [img]/upload/attachment/109538/653691c1-aa57-3e8f-b03d-7c9d911c48f1.jpg[/img]
转载请注明原文地址: https://www.6miu.com/read-5051364.html

最新回复(0)