浏览 2964 次
锁定老帖子 主题:struts2的验证入门
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-09-01
struts2的验证和struts1的验证相对来说简单很多,注意的是在Action类的同一级目录下加一个 Action名-validation.xml文件就即可。下面是例题:
1.user.java package com.lxitedu.validate; public class User { private String name; private String password; private int age; private String email; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } 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; } @Override public String toString() { return "User [age=" + age + ", email=" + email + ", name=" + name + ", password=" + password + ", sex=" + "]"; } } 2.Action.java
package com.lxitedu.validate; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class Action extends ActionSupport implements ModelDriven<User> { private static final long serialVersionUID = 1L; private User user = new User(); @Override public String execute() throws Exception { return SUCCESS; } @Override public User getModel() { return user; } public String run() throws Exception { return INPUT; } } 3.在与Action.java的同一级目录下定义一个Action-validation.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <validators> <field name="name"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>必须输入名字</message> </field-validator> <field-validator type="regex"> <param name="expression"><![CDATA[(\w{4,25})]]></param> <message>您输入的用户名只能是字母和数组,且长度必须在4到25之间</message> </field-validator> </field> <field name="password"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>必须输入密码</message> </field-validator> <field-validator type="regex"> <param name="expression"><![CDATA[(\w{4,25})]]></param> <message>您输入的密码只能是字母和数组,且长度必须在4到25之间</message> </field-validator> </field> <field name="age"> <field-validator type="int"> <param name="min">1</param> <param name="max">150</param> <message>年龄必须在1到150之间</message> </field-validator> </field> <field name="email"> <field-validator type="email"> <message>输入正确的email地址</message> </field-validator> <field-validator type="requiredstring"> <param name="trim">true</param> <message>必须输入邮箱</message> </field-validator> </field> </validators> 4.struts.xml
<action name="validate" class="com.lxitedu.validate.Action"> <result name="input">/validate.jsp</result> <result name="success">/validate.jsp</result> </action> 5.validate.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> <title>Insert title here</title> </head> <body> <s:form action="abc/validate.action" > <s:textfield name="name" label="姓名" /> <s:textfield name="password" label="密码"/> <s:textfield name="age" label="年龄"/> <s:textfield name="email" label="邮箱"/> <s:submit/> </s:form> </body> </html>
OK.这就是struts2的一个最最简单的验证过程。。。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-09-01
感觉现在这样用的已经比较少了,要加的东西太多代码量比较大
网上有很多开源的用于js验证的例子,简便快捷 |
|
返回顶楼 | |
发表时间:2009-09-01
xingzhaozhong 写道 感觉现在这样用的已经比较少了,要加的东西太多代码量比较大
网上有很多开源的用于js验证的例子,简便快捷 这只是struts2的一个知识点,具体在项目中用那个,那就自己怎么想咯! |
|
返回顶楼 | |
发表时间:2009-09-01
单纯JS 验证不太安全,好容易就突破他,提交非法信息的!
|
|
返回顶楼 | |
发表时间:2009-09-02
kalin001 写道 单纯JS 验证不太安全,好容易就突破他,提交非法信息的! 也不是这样说吧,写的好,肯定不会。像一般的项目大部分都是js验证,速度快! |
|
返回顶楼 | |