使用 ModelDriven 时如何执行 XML 验证? [英] How to perform XML Validation when using ModelDriven?

查看:27
本文介绍了使用 ModelDriven 时如何执行 XML 验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 Struts2 项目,其中使用了基于 XML 的验证.模型类RegistrationForm如下所示

I've created a Struts2 project in which I used XML based validation. Model class RegistrationForm is shown below

package com.projects;

import com.opensymphony.xwork2.ActionSupport;

public class RegistrationForm implements Serializable{

   private static final long serialVersionUID = 1L;
   private String fname;
   private String lname;
   private int numbr;

  public int getNumbr() {
   return numbr;
  }
  public void setNumbr(int numbr) {
     this.numbr = numbr;
  }
  public String getFname() {
    return fname;
  }
  public void setFname(String fname) {
    this.fname = fname;
  }
  public String getLname() {
    return lname;
  }
  public void setLname(String lname) {
    this.lname = lname;
  }
}

RegistrationFormAction.Java

package com.projects;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class RegistrationFormAction extends ActionSupport implements ModelDriven<RegistrationForm> {
 private RegistrationForm registrationForm;
 public RegistrationForm getRegistrationForm() {
  return registrationForm;
 }
 public void setRegistrationForm(RegistrationForm registrationForm) {
   this.registrationForm = registrationForm;
 }

 public RegistrationForm getModel(){
  registrationForm=new RegistrationForm();
  return registrationForm;
 }
 public String execute(){
    return "success";
 }
}

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>

 <constant name="struts.devMode" value="true" />
 <package name="dd" extends="struts-default">

  <interceptors>
       <interceptor-stack name="defaultStack">
         <interceptor-ref name="exception" />
         <interceptor-ref name="alias" />
         <interceptor-ref name="servletConfig" />
         <interceptor-ref name="prepare" />
         <interceptor-ref name="i18n" />
         <interceptor-ref name="chain" />
         <interceptor-ref name="debugging" />
         <interceptor-ref name="profiling" />
         <interceptor-ref name="scopedModelDriven" />
         <interceptor-ref name="modelDriven" />
         <interceptor-ref name="params"/>
         <interceptor-ref name="validation"/>
         <interceptor-ref name="fileUpload" />
         <interceptor-ref name="checkbox" />
         <interceptor-ref name="staticParams" />
         <interceptor-ref name="conversionError" />
         <interceptor-ref name="workflow"/>
      </interceptor-stack>
    </interceptors>

      <action name="submitForm" class="com.projects.RegistrationFormAction">
         <interceptor-ref name="defaultStack" />
        <result name="success">/WelcomePage.jsp</result>
        <result name="input">/RegistrationForm.jsp</result>
  </action>
</package>
</struts>

RegistrationFormAction-validation.xml

<!DOCTYPE validators PUBLIC 
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> 

<validators>
<field name="registrationform">
      <field-validator type="visitor">
      <param name="appendPrefix">false</param>
     <message/>
    </field-validator>
 </field>

</validators>

RegistrationForm-validation.xml

 <!DOCTYPE validators PUBLIC 
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> 

<validators>
 <field name="fname">
  <field-validator type="requiredstring">
  <message>First Name can't be blank</message>
</field-validator>
</field>

<field name="lname">
  <field-validator type="requiredstring">
   <message>Last Name can't be blank</message>
  </field-validator>
 </field>
<field name="numbr">
  <field-validator type="int">
    <param name="min">1</param>
    <param name="max">10</param>
  <message>Number between 1 to 10</message>
 </field-validator>
 </field>
</validators>

但验证不起作用.

推荐答案

这里有很多事情要做!我会按照出现在问题中的顺序发布它们:

There are a lot of things goin' on here! I'll post them in order of appearance in the question:

  1. 永远不要让 POJO 扩展 ActionSupport:

  1. Never make a POJO extends ActionSupport:

public class RegistrationForm extends ActionSupport {

必须成为

public class RegistrationForm implements Serializable {

  • 返回 SUCCESS 比返回 "success" 更好,以防止输入错误(但这是次要的);

  • Better returning SUCCESS than "success" to prevent typos (but ok this is secondary);

    intercetpor 栈定制有四个问题:

    The intercetpor stack customization has four problems:

    • 您正在覆盖现有的 basicStack,这可能会违反 POLA,特别是如果其他人将在这个项目上工作;最好使用自定义名称,例如.myStack;

    • you are overriding the existing basicStack, this risks to violate the POLA, especially if other people will work on this project; it's better to use a custom name instead, eg. myStack;

    你只使用了三个拦截器,这很可疑;虽然可以删除许多默认拦截器,但应始终保留许多其他拦截器,尤其是涉及验证时,例如.ConversionError Interceptor, or Workflow Interceptor, etc. 了解整个过程是如何工作的.通常,只有当您确切地知道拦截器的作用并且绝对确定您不需要(也不会)需要它时,您才应该删除它.

    you are using only three interceptors, and this is suspicious; while many of the default interceptors can be dropped, many others instead should be always kept, especially with validation involved, eg. ConversionError Interceptor, or Workflow Interceptor, etc. Read how the whole thing works. As a rule, you should remove an Interceptor only when you know exactly what it does and you are absolutely sure you don't (and won't) need it.

    当使用ModelDriven时(通常不推荐,因为基本没用,而且不是高手的问题根源),你需要把ModelDrivenInterceptor before Parameters Interceptor,否则当参数拦截器运行时,模型还不会被推送,并且会在动作上搜索setter,例如.setFname(),而不是模型上的(导致模型中的空属性,并在警告中

    When using ModelDriven (that is usually not recommended, because basically useless and source of problems when not experts with it), you need to put the ModelDriven Interceptor before the Parameters Interceptor, otherwise when the parameters interceptor runs, the model won't be pushed yet, and the setters will be searched on the action, eg. setFname(), instead that on the model (resulting in null properties in the model, and in the warning

    在class RegistrationFormAction"上设置fname"时捕获到意外异常:将表达式fname"设置为值 ['Sumit', ]

    因为操作中缺少 setter).

    because of the missing setters in the action).

    您在 XML 验证文件的 DOCTYPE 中混合了 1.0.2 和 1.0.3,将所有内容都设为 1.0.3(并注意它们从 OpenSymphony 迁移到了 Apache);然后改变:

    You are mixing 1.0.2 and 1.0.3 in DOCTYPE of XML validation files, make everything 1.0.3 (and notice that they migrated from OpenSymphony to Apache); then change:

    <!DOCTYPE validators PUBLIC   
        "-//OpenSymphony Group//XWork Validator 1.0.2//EN"   
        "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
    

    <!DOCTYPE validators PUBLIC 
        "-//Apache Struts//XWork Validator 1.0.3//EN"
        "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">  
    

  • 确保 RegistrationFormAction-validation.xml 文件位于操作文件夹中,而 RegistrationForm-validation.xml 位于 RegistrationForm.java 文件夹.

  • Ensure that the RegistrationFormAction-validation.xml file is in the action folder, while the RegistrationForm-validation.xml is in the RegistrationForm.java folder.

    考虑避免使用 ModelDriven,因为正如 Stephen Young 所说,

    Consider avoiding ModelDriven, because as Stephen Young says,

    您必须驯服复杂性才能成为更好的程序员

    You Must Tame Complexity to Become a Better Programmer

  • 正如 AleksandrM 的评论所指出的,

  • As pointed out by AleksandrM's comment, there is also a typo in

    <field name="registrationform"> 
    

    应该是

    <field name="registrationForm">
    

  • 这篇关于使用 ModelDriven 时如何执行 XML 验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆