通过Struts1自带的validate验证表单数据功能实现Ajax的效果(登录案例)

这个案例是入门以后的第一个案例,主要讲解了Struts1的Validate数据处理的用法.

先上效果图:

clip_image001

接下来说一下实现步骤.

MVC-V 第一步:先写视图

clip_image003

写一个register.jsp页面,这个页面主要负责控制注册信息,也就是用户能够直接看到的界面.写一个表单,里面包括姓名,密码,生日,邮箱,爱好,城市等信息.

上完整代码:

<%@ page language=”java” pageEncoding=”UTF-8″%>

<%@ page isELIgnored=”false” %>

<%@ page import=”org.apache.struts.action.*” %>

<%@ page import=”java.util.*” %>

<%@ page import=”com.lxgyx.struts1.domain.*” %>

<%@ taglib uri=”http://struts.apache.org/tags-html” prefix=”html” %>

<%@ taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c” %>

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>

<html>

<body>

<%–

ActionErrors errors = (ActionErrors)request.getAttribute(“org.apache.struts.action.ERROR”);

if(errors!=null){

Iterator<ActionMessage> it = errors.get(“username”);

while(it.hasNext()){

ActionMessage am = it.next();

String info = am.getKey();

out.write(“<font color=’red’>”+info+”</font>”);

}

}

–%>

<form action=”${pageContext.request.contextPath}/register.do” method=”post”>

<table border=”1″ align=”center”>

<caption>用户注册(struts1版本)</caption>

<tr>

<th>用户名</th>

<td><input type=”text” name=”username” value=”${registerForm.username}”/></td>

<td>

<html:errors property=”username”/>

</td>

</tr>

<tr>

<th>密码</th>

<td><input type=”password” name=”password1″/></td>

<td>

<html:errors property=”password1″/>

</td>

</tr>

<tr>

<th>确认密码</th>

<td><input type=”password” name=”password2″/></td>

<td>

<html:errors property=”password2″/>

</td>

</tr>

<tr>

<th>性别</th>

<td>

<input checked type=”radio” name=”gender” value=”male”/>男

<input type=”radio” name=”gender” value=”female”/>女

</td>

</tr>

<tr>

<th>生日</th>

<td>

<input type=”text” name=”birthday”/>

</td>

<td>

<html:errors property=”birthday”/>

</td>

</tr>

<tr>

<th>城市</th>

<td>

<select name=”city”>

<c:forEach var=”city” items=”<%=City.values()%>”>

<option value=”${city.value}”>${city.desc}</option>

</c:forEach>

</select>

</td>

<td>

<html:errors property=”city”/>

</td>

</tr>

<tr>

<th>月收入</th>

<td>

<input type=”text” name=”income”/>

</td>

<td>

<html:errors property=”income”/>

</td>

</tr>

<tr>

<th>爱好</th>

<td>

<c:forEach var=”like” items=”<%=Like.values()%>”>

<input type=”checkbox” name=”like” value=”${like.value}”/>${like.desc}

</c:forEach>

</td>

<td>

<html:errors property=”like”/>

</td>

</tr>

<tr>

<th>邮箱</th>

<td>

<input type=”text” name=”email”/>

</td>

<td>

<html:errors property=”email”/>

</td>

</tr>

<tr>

<td colspan=”2″ align=”center”>

<input type=”submit” value=”提交”/>

</td>

</tr>

</table>

</form>

</body>

</html>

MVC-M 第二步:再写模型

由于这个表单信息较多,所以用了三个模型来控制.

分别是:

1. City用来存储城市信息

源码截图:

clip_image004

这个是一个枚举类,枚举类用enum表示而不是用class

写枚举类的基本步骤

先写枚举值->再写枚举键值对->再设置键值对的get方法

完整源码:

package com.lxgyx.struts1.domain;

public enum City {

bj(“bj”,”北京”),

sh(“sh”,”上海”),

sz(“sz”,”深圳”),

gz(“gz”,”广州”),

tj(“tj”,”天津”);

private String value;

private String desc;

City(String value, String desc) {

this.value = value;

this.desc = desc;

}

public String getValue() {

return value;

}

public String getDesc() {

return desc;

}

}

2. Like 用来存储兴趣爱好

源码截图:

clip_image005

完整源码:

package com.lxgyx.struts1.domain;

public enum Like {

//自定义枚举类

read(“read”,”阅读”),sing(“sing”,”唱歌”),

dance(“dance”,”跳舞”),net(“net”,”跳舞”),play(“play”,”游戏”);

private String value;//传到后台的值

private String desc;//传到前台显示的值

Like(String value, String desc) {

this.value = value;

this.desc = desc;

}

public String getValue() {

return value;

}

public String getDesc() {

return desc;

}

}

3. Student 用来存储学生基本信息

源码截图:

clip_image006

完整源码:

package com.lxgyx.struts1.domain;

public class Student {

public Student() {

}

private int id;

private String username;

private String password;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

}

MVC-C 第三步:最后写控制器

控制器有两个,一个是RegisterForm.java,这个控制器主要作用是像对象模型一样的东西,同时也具有验证数据的功能.

源码较多,直接上完整源码:

package com.lxgyx.struts1.form;

import com.lxgyx.struts1.domain.City;

import com.lxgyx.struts1.domain.Like;

import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;

import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionMessage;

import org.apache.taglibs.standard.lang.jstl.ELEvaluator;

import javax.servlet.http.HttpServletRequest;

import java.util.Locale;

//参数收集器

//参数格式验证器

public class RegisterForm extends ActionForm {

private String username;

private String password1;

private String password2;

private String birthday;

private String city;

private String income;

private String[] like;

private String email;

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword1() {

return password1;

}

public void setPassword1(String password1) {

this.password1 = password1;

}

public String getPassword2() {

return password2;

}

public void setPassword2(String password2) {

this.password2 = password2;

}

public String getBirthday() {

return birthday;

}

public void setBirthday(String birthday) {

this.birthday = birthday;

}

public String getCity() {

return city;

}

public void setCity(String city) {

this.city = city;

}

public String getIncome() {

return income;

}

public void setIncome(String income) {

this.income = income;

}

public String[] getLike() {

return like;

}

public void setLike(String[] like) {

this.like = like;

}

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

@Override

public ActionErrors validate(ActionMapping mapping,

HttpServletRequest request) {

//创建一个错误消息的集合

ActionErrors errors=new ActionErrors();

//用户名[必填+英文数字]

if (this.username!=null&&this.username.trim().length()>0){

if (this.username.matches(“[a-zA-Z0-9]+]”)){

;

}else {

ActionMessage am=new ActionMessage(“username.english”,true);

errors.add(“username”,am);

}

}else{

//创建一个错误消息

ActionMessage am=new ActionMessage(“username.required”,true);

//将错误信息加入集合

errors.add(“username”,am);

}

//密码1[必填+6位数字]

if (this.password1!=null&&this.password1.trim().length()>0){

if (this.password1.matches(“[0-9]{6}”)){

;

}else{

ActionMessage am=new ActionMessage(“password1.number”,true);

errors.add(“password1”,am);

}

}else{

ActionMessage am=new ActionMessage(“password1.required”,true);

errors.add(“password1”,am);

}

//密码2[必填+6位数字 与password1相同]

if (this.password2!=null&&this.password2.trim().length()>0){

if (this.password2.matches(“[0-9]{6}”)){

if (this.password2.equals(password1)){

}else {

ActionMessage am=new ActionMessage(“二次密码不一致”,false);

errors.add(“password2”,am);

}

}else{

ActionMessage am=new ActionMessage(“password2.number”,true);

errors.add(“password2”,am);

}

}else {

ActionMessage am=new ActionMessage(“password2.required”,true);

errors.add(“password2”,am);

}

//生日[必填+符合yyyy-MM-dd格式]

if (this.birthday!=null&&this.birthday.trim().length()>0){

try {

DateLocaleConverter dlc=new DateLocaleConverter(Locale.CHINA);

dlc.convert(this.birthday);

}catch (Exception e){

ActionMessage am=new ActionMessage(“生日格式非法”,false);

errors.add(“birthday”,am);

}

}else {

ActionMessage am=new ActionMessage(“生日必填”,false);

errors.add(“birthday”,am);

}

//收入[必填+每位必须是数字]

if (this.income!=null&&this.income.trim().length()>0){

if (this.income.matches(“[0-9]{4}”)){

;

}else {

ActionMessage am=new ActionMessage(“收入必须是数字”,false);

errors.add(“income”,am);

}

}else {

ActionMessage am=new ActionMessage(“收入必填”,false);

errors.add(“income”,am);

}

//邮箱[必填=符合邮件格式]

if (this.email!=null&&this.email.trim().length()>0){

if (this.email.matches(“\\w+@\\w+(\\.+\\w+)+”)){

;

}else {

ActionMessage am=new ActionMessage(“邮箱格式非法”,false);

errors.add(“email”,am);

}

}else {

ActionMessage am=new ActionMessage(“邮箱必填”,false);

errors.add(“email”,am);

}

//爱好[必填=取值必须是枚举值之一]

if (this.like!=null&&this.email.trim().length()>0){

for (String aLike:like){

//判断该值是否是枚举值之一

try {

Like.valueOf(aLike);

}catch (Exception e){

ActionMessage am=new ActionMessage(“爱好取值非法”,false);

errors.add(“like”,am);

}

}

}else {

ActionMessage am=new ActionMessage(“爱好必选”,false);

errors.add(“like”,am);

}

//城市[必填+取值必须是枚举值之一]

if (this.city!=null&&this.city.trim().length()>0){

try {

City.valueOf(this.city);

}catch (Exception e){

ActionMessage am=new ActionMessage(“城市取值非法”,false);

errors.add(“city”,am);

}

}else {

ActionMessage am=new ActionMessage(“城市必填”,false);

errors.add(“city”,am);

}

//返回集合

return errors;

}

}

另一个是RegisterAction.java,这个是一个纯粹的控制器,控制数据的分发.

完整源码:

package com.lxgyx.struts1.action;

import com.lxgyx.struts1.form.RegisterForm;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

//分控制器

public class RegisterAction extends Action {

//重写execute()方法

@Override

public ActionForward execute(ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

throws Exception {

request.setCharacterEncoding(“UTF-8”);

//复制ActionForm强转

RegisterForm rf= (RegisterForm) form;

String username=rf.getUsername();

String password1=rf.getPassword1();

return mapping.findForward(“toSuccessJsp”);

}

}

张 大鹏

北京理想国惠玩科技有限公司CEO

发表评论

电子邮件地址不会被公开。 必填项已用*标注