昨晚建【1】后,现在开始试struts2.1的验证- -#
struts.xml要加个input result
<?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> <include file="struts-default.xml"></include> <package name="StrutsRETestA" extends="struts-default"> <action name="ActionOnly" class="com.action.ActionOnly"> <result>index.jsp</result> <result name="input">actionOnly.jsp</result> </action> </package> </struts>
ActionOnly-validation.xml //使用验证框架
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.14.dtd"> <validators> <field name="name"> <field-validator type="requiredstring"> <message>can not be emtpy!</message> </field-validator> </field> </validators>
com.action.ActionOnly.javaactionAjax.jsp //使用ajax验证package com.action; import com.opensymphony.xwork2.ActionSupport; /** * Jun 30, 2008 * @author ipinko@msn.com * */ public class ActionOnly extends ActionSupport { /** * 名字 */ private String name; /* (non-Javadoc) * @see com.opensymphony.xwork2.ActionSupport#execute() */ @Override public String execute() throws Exception { // TODO Auto-generated method stub return SUCCESS; } /* (non-Javadoc) * @see com.opensymphony.xwork2.ActionSupport#validate() */ @Override //使用代码验证 public void validate() { // TODO Auto-generated method stub if(name.equals("badman")){ addFieldError("name", "you are badman - -!"); } } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } } actionOnly.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>actionOnly.jsp</title> </head> <body> <br> <s:form action="ActionOnly"> <s:textfield name="name" label="name" /> <s:submit /> </s:form> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java"%> <%@ taglib prefix="s" uri="/struts-tags"%> <%@ taglib prefix="sx" uri="/struts-dojo-tags"%><!-- 注意,这里多了个标签。 --> <html> <head> <title>actionAjax.jsp</title> <sx:head /><!-- 注意 --> </head> <body> <s:form action="ActionOnly" theme="xhtml"><!-- 注意,没有Ajax的theme了。 --> <s:textfield name="name" label="Name" /> <sx:submit validate="true" /><!-- 注意 --> </s:form> </body> </html> 最后还要加上包struts2-dojo-plugin-2.1.2.jar
![]()
这个AJAX验证还不能在失去焦点时就返回验证信息,本人对AJAX比较一窍不通哈- -#
不过之前struts2的验证也有问题,
而IE里用失去焦点验证会提交不了- -#
在FF里可以用DIV来action进行失去焦点验证,不过也很麻烦,用form的我在网上看过都是验证完提交不了的- -#
因为struts2好象不GC DWR,所以没有试,最近都好累哈- -!