首页 > 代码库 > struts2学习(11)struts2验证框架1.验证简介、内置验证
struts2学习(11)struts2验证框架1.验证简介、内置验证
一、Struts2验证简介:
二、struts2内置验证:
下面例子,需求是:为用户注册进行验证;
com.cy.model.User.java:
package com.cy.model; public class User { private String userName; private String name; private int age; private String email; private String homePage; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getHomePage() { return homePage; } public void setHomePage(String homePage) { this.homePage = homePage; } @Override public String toString() { return "User [userName=" + userName + ", name=" + name + ", age=" + age + ", email=" + email + ", homePage=" + homePage + "]"; } }
com.cy.action.RegisterAction.java:
package com.cy.action; import com.cy.model.User; import com.opensymphony.xwork2.ActionSupport; public class RegisterAction extends ActionSupport { private static final long serialVersionUID = 1L; private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String execute() throws Exception { System.out.println("传入的用户" + user); return SUCCESS; } }
RegisterAction-validation.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> <validators> <field name="user.userName"> <field-validator type="requiredstring"> <message>请输入用户名</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">6</param> <param name="maxLength">10</param> <message>用户名必须在${minLength}和${maxLength}之间</message> </field-validator> </field> <field name="user.name"> <field-validator type="requiredstring"> <message>请输入姓名</message> </field-validator> </field> <field name="user.age"> <field-validator type="int"> <param name="min">18</param> <message>年龄必须满18周岁</message> </field-validator> </field> <field name="user.email"> <field-validator type="requiredstring"> <message>请输入邮件</message> </field-validator> <field-validator type="email"> <message>邮件格式不对</message> </field-validator> </field> <field name="user.homePage"> <field-validator type="requiredstring"> <message>请输入主页</message> </field-validator> <field-validator type="url"> <message>主页格式不对</message> </field-validator> </field> <!-- <![CDATA[xxxx]]>表示忽略里面的一些特殊的转义字符 --> <validator type="expression"> <param name="expression"><![CDATA[!user.name.equals(user.userName)]]></param> <message>用户名和真实姓名不能相同</message> </validator> </validators>
struts.xml配置:
<?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> <package name="manage" extends="struts-default"> <action name="registerAction" class="com.cy.action.RegisterAction"> <!-- 如果验证失败的话,返回到register.jsp注册页面 --> <result name="input">register.jsp</result> <result name="success">success.jsp</result> </action> </package> </struts>
register.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body>
<!-- 这个作用就是表达式验证失败的错误信息,显示在这里。 --> <s:actionerror/> <s:form action="/registerAction" method="post"> <s:textfield name="user.userName" label="用户名"></s:textfield> <s:textfield name="user.name" label="真实姓名"></s:textfield> <s:textfield name="user.age" label="年龄"></s:textfield> <s:textfield name="user.email" label="邮件"></s:textfield> <s:textfield name="user.homePage" label="主页"></s:textfield> <s:submit value="注册"></s:submit> </s:form> </body> </html>
success.jsp:
<body> 注册成功! </body>
测试:
注册填写失败,提示信息:
注册成功,console打印:
struts2学习(11)struts2验证框架1.验证简介、内置验证
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。