« | August 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | | | | | |
|
统计 |
blog名称:小雨 日志总数:262 评论数量:1273 留言数量:15 访问次数:4669377 建立时间:2005年1月8日 |
| 
|
W3CHINA Blog首页 管理页面 写新日志 退出
[知识积累]带验证的 struts |
小雨 发表于 2007/12/2 21:23:00 |
<?xml version="1.0" encoding="ISO-8859-1" ?><!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.-->
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans> <form-bean name="simpleForm" type="org.cjea.Struts.example.RegUserForm"/> </form-bean> </form-beans> <action-mappings> <action path="/RegUser" type="org.cjea.Struts.example.RegUserAction" name="simpleForm" scope="request" input="index.jsp" validate="true"> <forward name="success" path="/html-good.jsp"/> <forward name="fails" path="/html-error.jsp"/> </action> </action-mappings>
<message-resources parameter="org.apache.struts.webapp.el.exercise.ApplicationResources" null="false"/> <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml" /> </plug-in></struts-config>
2 form
package org.cjea.Struts.example;import javax.servlet.http.HttpServletRequest;import org.apache.struts.action.ActionMapping;
public final class RegUserForm extends org.apache.struts.validator.ValidatorForm{
private String logname; private String password; private String email;
public RegUserForm(){ logname = null; password = null; email = null; }
public String getLogName() { return this.logname; } public void setLogName(String logname) { this.logname = logname; } public void setPassWord(String password) { this.password = password; } public String getPassWord() { return this.password; } public void setEmail(String email) { this.email = email; } public String getEmail() { return this.email; }
public void reset(ActionMapping mapping, HttpServletRequest request) { logname = null; password = null; email = null; } }
3 validation.xml
<?xml version="1.0" encoding="ISO-8859-1" ?><!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.-->
<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN" "http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd">
<form-validation>
<!-- This is a minimal Validator form file with a couple of examples.-->
<global>
<!-- An example global constant <constant> <constant-name>postalCode</constant-name> <constant-value>^\d{5}\d*$</constant-value> </constant> end example-->
</global>
<formset>
<!-- An example form --> <form name="simpleForm"> <field property="logName" depends="required"> <arg key="simpleForm.logName"/> </field> <field property="passWord" depends="required,mask"> <arg key="simpleForm.passWord"/> <var> <var-name>mask</var-name> <var-value>^[0-9a-zA-Z]*$</var-value> </var> </field> </form> </formset></form-validation> 4 action
package org.cjea.Struts.example;
import javax.servlet.http.*; import org.apache.struts.action.*;
public final class RegUserAction extends Action { public RegUserAction() { super(); }
public ActionForward execute(ActionMapping mapping, ActionForm actionform, HttpServletRequest req, HttpServletResponse res) { RegUserForm form = (RegUserForm)actionform; String title = form.getLogName();String password = form.getPassWord();String email = form.getEmail(); if(title.equals("email"))return (mapping.findForward("success"));elsereturn (mapping.findForward("fails"));} }
|
阅读全文(3444) | 回复(0) | 编辑 | 精华 |
|