2013-05-08 47 views
0

我在Struts2應用程序中有一個單選按鈕。我可以驗證文本框字段,但是我不能爲單選按鈕(必須選中一個)和複選框列表(至少必須選中一個)進行驗證。如何驗證Struts2中的單選按鈕

我跟着一些頁面講述了這個話題,但沒有一個解決方案有效。

最後,我有這樣的XML文件中:

<field name="sexo"> 
    <field-validator type="requiredstring"> 
     <param name="trim">true</param> 
     <message>*</message> 
    </field-validator> 
</field>  

任何幫助將不勝感激。

補充:

這是動作類:(不,如果不存在驗證相關的代碼,這重要嗎?)

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package agenda; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpSession; 
import org.apache.struts2.ServletActionContext; 

/** 
* 
* @author Jaime 
*/ 
public class ProfileAction { 

    private String username; 
    private String password; 
    private String nombres; 
    private String apellidopaterno; 
    private String apellidomaterno; 
    private String fechanacimiento; 
    private String direccion; 
    private String comuna; 
    private String telefonofijo; 
    private String telefonomovil; 
    private String email; 
    private String sexo; 
    private String[] subscripcion; 

    public ProfileAction() { 
    } 

    /** 
    * 
    * @return @throws Exception 
    */ 
    public String execute() throws Exception { 
     HttpServletRequest request = ServletActionContext.getRequest(); 
     HttpSession session = request.getSession(); 
     String usuario = (String) session.getAttribute("id"); 
     if (usuario != null && !usuario.equals("") && !usuario.equals("0")) { 
      return "success"; 
     } else { 
      return "error"; 
     } 
    } 

    /** 
    * Muestra el formulario de registro de cliente 
    * 
    * @return 
    * @throws Exception 
    */ 
    public String register() throws Exception { 
     return "success"; 
    } 

    /** 
    * @return the username 
    */ 
    public String getUsername() { 
     return username; 
    } 

    /** 
    * @param username the username to set 
    */ 
    public void setUsername(String username) { 
     this.username = username; 
    } 

    /** 
    * @return the password 
    */ 
    public String getPassword() { 
     return password; 
    } 

    /** 
    * @param password the password to set 
    */ 
    public void setPassword(String password) { 
     this.password = password; 
    } 

    /** 
    * @return the nombres 
    */ 
    public String getNombres() { 
     return nombres; 
    } 

    /** 
    * @param nombres the nombres to set 
    */ 
    public void setNombres(String nombres) { 
     this.nombres = nombres; 
    } 

    /** 
    * @return the apellidopaterno 
    */ 
    public String getApellidopaterno() { 
     return apellidopaterno; 
    } 

    /** 
    * @param apellidopaterno the apellidopaterno to set 
    */ 
    public void setApellidopaterno(String apellidopaterno) { 
     this.apellidopaterno = apellidopaterno; 
    } 

    /** 
    * @return the apellidomaterno 
    */ 
    public String getApellidomaterno() { 
     return apellidomaterno; 
    } 

    /** 
    * @param apellidomaterno the apellidomaterno to set 
    */ 
    public void setApellidomaterno(String apellidomaterno) { 
     this.apellidomaterno = apellidomaterno; 
    } 

    /** 
    * @return the fechanacimiento 
    */ 
    public String getFechanacimiento() { 
     return fechanacimiento; 
    } 

    /** 
    * @param fechanacimiento the fechanacimiento to set 
    */ 
    public void setFechanacimiento(String fechanacimiento) { 
     this.fechanacimiento = fechanacimiento; 
    } 

    /** 
    * @return the direccion 
    */ 
    public String getDireccion() { 
     return direccion; 
    } 

    /** 
    * @param direccion the direccion to set 
    */ 
    public void setDireccion(String direccion) { 
     this.direccion = direccion; 
    } 

    /** 
    * @return the comuna 
    */ 
    public String getComuna() { 
     return comuna; 
    } 

    /** 
    * @param comuna the comuna to set 
    */ 
    public void setComuna(String comuna) { 
     this.comuna = comuna; 
    } 

    /** 
    * @return the telefonofijo 
    */ 
    public String getTelefonofijo() { 
     return telefonofijo; 
    } 

    /** 
    * @param telefonofijo the telefonofijo to set 
    */ 
    public void setTelefonofijo(String telefonofijo) { 
     this.telefonofijo = telefonofijo; 
    } 

    /** 
    * @return the telefonomovil 
    */ 
    public String getTelefonomovil() { 
     return telefonomovil; 
    } 

    /** 
    * @param telefonomovil the telefonomovil to set 
    */ 
    public void setTelefonomovil(String telefonomovil) { 
     this.telefonomovil = telefonomovil; 
    } 

    /** 
    * @return the email 
    */ 
    public String getEmail() { 
     return email; 
    } 

    /** 
    * @param email the email to set 
    */ 
    public void setEmail(String email) { 
     this.email = email; 
    } 

    /** 
    * @return the sexo 
    */ 
    public String getSexo() { 
     return sexo; 
    } 

    /** 
    * @param sexo the sexo to set 
    */ 
    public void setSexo(String sexo) { 
     this.sexo = sexo; 
    } 

    /** 
    * @return the subscripcion 
    */ 
    public String[] getSubscripcion() { 
     return subscripcion; 
    } 

    /** 
    * @param subscripcion the subscripcion to set 
    */ 
    public void setSubscripcion(String[] subscripcion) { 
     this.subscripcion = subscripcion; 
    } 
} 

這是JSP文件:

<%@ taglib prefix="s" uri="/struts-tags" %> 
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %> 

<script src="js/jquery/jquery.maskedinput.js" type="text/javascript"></script> 
<script src="js/jquery/jquery.selectbox-0.2.js" type="text/javascript"></script> 

<div id="register"> 
    <ul><li><a href="#frm_registro"><span><s:text name="profile.register" /></span></a></li></ul> 
    <div id="frm_registro"> 
     <s:div id="avisoError"><s:text name="profile.requiredtext" /></s:div> 
     <s:form id="registro" 
       action="proceso_registro" theme="css_xhtml" validate="true"> 
      <s:textfield name="username" key="profile.rut" labelposition="left" maxlength="50" /> 
      <s:password name="password" key="profile.password" labelposition="left" maxlength="50" /> 
      <s:textfield name="nombres" key="profile.nombres" labelposition="left" maxlength="80" size="40" /> 
      <s:textfield name="apellidopaterno" key="profile.apellidopaterno" labelposition="left" maxlength="80" size="40" /> 
      <s:textfield name="apellidomaterno" key="profile.apellidomaterno" labelposition="left" maxlength="80" size="40" /> 
      <s:textfield name="fechanacimiento" key="profile.fechanacimiento" labelposition="left" /> 
      <s:textfield name="direccion" key="profile.direccion" labelposition="left" maxlength="80" size="40" /> 
      <s:select name="comuna" key="profile.comuna" labelposition="left" list="#{'1':'Santiago', '2':'Huechuraba'}" headerValue="-= Seleccione Comuna =- " headerKey="" /> 
      <s:textfield name="telefonofijo" key="profile.telefonofijo" labelposition="left" maxlength="30" size="30" /> 
      <s:textfield name="telefonomovil" key="profile.telefonomovil" labelposition="left" maxlength="30" size="30" /> 
      <s:textfield name="email" key="profile.email" labelposition="left" maxlength="30" size="30" /> 
      <s:radio name="sexo" key="profile.sexo" labelposition="left" list="#{'M':'Masculino', 'F':'Femenino'}" /> 
      <s:checkboxlist name="subscripcion" key="profile.subscripcion" labelposition="left" list="#{'T':'Teléfono', 'E':'E-mail', 'M':'Mensaje de Texto'}" value="T" /> 

      <s:submit id="boton_registro" align="left" cssClass="send" value="Registrarse" /> 
     </s:form> 
    </div> 
</div> 

<script> 
    $("#register").tabs(); 
    $("#boton_registro").button(); 
    $("#registro_fechanacimiento").datepicker({ 
     dateFormat: "dd/mm/yy", 
     showOn: "both", 
     changeYear: true, 
     changeMonth: true, 
     buttonImage: "images/datepicker_icon.png", 
     buttonImageOnly: true, 
     buttonText: "<s:text name="AgendaPlus.choosedate" />"}); 
    $("#registro_fechanacimiento").mask("99/99/9999");            
    $.mask.definitions['~']='[0-9kK]'; 
    $("#registro_username").mask("99.999.999-~"); 
    $("#registro_comuna").selectbox({ 
     effect: "fade" 
    });  
    $("#registro_telefonofijo").mask("(99) 9999 9999");            
    $("#registro_telefonomovil").mask("(09) 9999 9999");            
</script> 

這是配置XML文件的一部分:

<action name="registro" class="agenda.ProfileAction" method="register"> 
     <result name="success" type="tiles">/register.tiles</result> 
     <result name="input" type="tiles">/register.tiles</result> 
    </action>   

    <action name="proceso_registro" class="agenda.ProfileAction" method="register"> 
     <interceptor-ref name="profiling"> 
      <param name="profilingKey">profilingKey</param> 
     </interceptor-ref>    
     <interceptor-ref name="jsonValidationWorkflowStack"/>    
     <result name="success" type="tiles">/register.success.tiles</result> 
     <result name="input" type="tiles">/register.tiles</result> 
    </action>   

的 「registro」 行動的入口點(的形式),因爲這個URL顯示:http://domain.cl:8080/AgendaPlus/registro

最後,這是整個驗證文件(ProfileAction-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="username"> 
     <field-validator type="requiredstring"> 
      <param name="trim">true</param> 
      <message>Requerido</message> 
     </field-validator> 
     <field-validator type="rutValidator"> 
      <param name="trim">true</param> 
      <message key="errors.short_invalid" /> 
     </field-validator> 
    </field> 
    <field name="password"> 
     <field-validator type="requiredstring"> 
      <param name="trim">true</param> 
      <message>*</message> 
     </field-validator> 
    </field>  
    <field name="nombres"> 
     <field-validator type="requiredstring"> 
      <param name="trim">true</param> 
      <message>*</message> 
     </field-validator> 
    </field> 
    <field name="apellidopaterno"> 
     <field-validator type="requiredstring"> 
      <param name="trim">true</param> 
      <message>*</message> 
     </field-validator> 
    </field> 
    <field name="fechanacimiento"> 
     <field-validator type="requiredstring"> 
      <param name="trim">true</param> 
      <message>*</message> 
     </field-validator> 
    </field>  
    <field name="comuna"> 
     <field-validator type="int"> 
      <param name="min">1</param> 
      <message>*</message> 
     </field-validator>   
     <field-validator type="requiredstring"> 
      <param name="trim">true</param> 
      <message>*</message> 
     </field-validator> 
    </field>  
    <field name="telefonofijo"> 
     <field-validator type="requiredstring"> 
      <param name="trim">true</param> 
      <message>*</message> 
     </field-validator> 
    </field>  
    <field name="telefonomovil"> 
     <field-validator type="requiredstring"> 
      <param name="trim">true</param> 
      <message>*</message> 
     </field-validator> 
    </field>  
    <field name="email"> 
     <field-validator type="requiredstring"> 
      <param name="trim">true</param> 
      <message>*</message> 
     </field-validator> 
     <field-validator type="email"> 
      <param name="trim">true</param> 
      <message key="errors.short_invalid" /> 
     </field-validator>   
    </field>  
    <field name="sexo"> 
     <field-validator type="required"> 
      <param name="trim">true</param> 
      <message>*</message> 
     </field-validator> 
    </field>  
    <field name="subscripcion"> 
     <field-validator type="fieldexpression"> 
      <param name="expression"><![CDATA[#subscripcion.size() > 0]]></param> 
      <message>al menos uno</message> 
     </field-validator> 
    </field>  
</validators> 

如您所見,validation.xml文件與Struts2教程中的大多數xml不同,即使我也在使用Struts2。但是這個XML文件至少適用於所有領域,但對於我的自定義驗證器,單選按鈕和複選框列表。從其他教程中使用validation.xml時,不起任何作用。

最好的問候,

感謝 海梅

+0

的http://stackoverflow.com/questions/6242590/validation-to-a-radio-list-in-struts-2 – sunleo 2013-05-08 02:49:20

+0

可能重複的是......這是相同的,但是我已經閱讀那裏的解決方案,我跟着例子,但它沒有工作。它不驗證任何東西,而我已經實現的方式,驗證除單選按鈕和複選框之外的所有內容。在發佈的消息鏈接中指向的教程鏈接對validator.xml文件使用了非常不同的XML語法。 – jstuardo 2013-05-08 17:06:02

+0

@jstuardo:可以發佈你的動作和JSP頁面。 – 2013-05-08 19:24:14

回答

0

您需要使用required驗證,而不是requiredstring。驗證程序檢查指定的字段是否不爲空。

<field name="sexo"> 
    <field-validator type="required"> 
     <param name="trim">true</param> 
     <message>*</message> 
    </field-validator> 
</field> 

而對於「至少一個」條件,您可以使用fieldexpression驗證程序。

更新

由於您的subscripcion是你需要使用length變量未size方法,和W在它前面/ O #陣列。

<field name="subscripcion"> 
    <field-validator type="fieldexpression"> 
     <param name="expression"><![CDATA[subscripcion.length > 0]]></param> 
     <message>al menos uno</message> 
    </field-validator> 
</field> 
+0

你好..對於無線電,它不工作....我有這個定義:。對於checkboxlist,我有這樣的表達式:<![CDATA [#subscripcion.size()> 0]]>其中#subscripcion被定義爲,它不起作用。 – jstuardo 2013-05-08 14:00:50

+0

@jstuardo:更新了我的答案。而對於你的情況'必需的'也應該爲單選按鈕工作。 – 2013-05-08 21:11:28