【问题标题】:Need Help in struts2 Validation在 struts2 验证中需要帮助
【发布时间】:2009-06-20 12:38:59
【问题描述】:

我的 struts.xml 文件中有以下内容

<action name="ProductVerification" class="com.frontend.ProductVerification">
    <result name="success">/jsp/product_verification.jsp</result>
    <result name="input">/jsp/product_verification.jsp</result>
    <result name="error">/jsp/product_verification.jsp</result>
</action>

我的 html 代码中有以下内容

<s:form name="frmVerification" id="frmVerification" onsubmit="Javascript: return checkFrmVerification(this);"  >

<s:select name="countryId" id="cmbcountryid"  headerKey="0"  headerValue="%{getText('label.Please_Select')}" list="%{countryListCombo}" listKey="countryId" listValue="country" value="countryId" cssClass="style2" onchange="Javascript: GetCities();" required="true" />

<s:submit name="submit" src="images/submit_btn.jpg" type="image" value="submit" />

</form>

我有如下执行方法。

public String execute() throws Exception {

    Session session = this.getHibernateSession();

    Transaction tx = session.beginTransaction();

    //Following will set combo box containing country list
    this.setCountryListCombo();

    tx.commit();

    return SUCCESS;
}

我正在重写验证方法,如下所示。

@Override
public void validate() {
 HttpServletRequest request = this.getServletRequest();

     if(request.getParameter(countryId) == 0){
          addFieldError("countryId", "Please select country");
     }

}

现在,当我执行我的操作时,它会显示带有 countryId 组合框的表单,其中包含国家/地区。

现在,当我在不选择组合框的情况下提交表单时,它应该会显示错误。

但不是显示错误“请选择国家”它给了我以下错误。

Struts 问题报告

Struts 检测到一个未处理的异常:

消息: 标签“select”、字段“list”、名称“countryId”:请求的列表键“%{countryListCombo}”无法解析为集合/数组/地图/枚举/迭代器类型。示例:人或人。{name}

文件: org/apache/jasper/servlet/JspServletWrapper.java

行号: 522

谁能告诉我为什么会这样?

在 validate() 方法给出 result="input" 之后,它似乎没有调用 execute() 方法,而是尝试直接显示“/jsp/product_verification.jsp”页面。

请帮我解决问题。

谢谢。

【问题讨论】:

    标签: validation struts2


    【解决方案1】:

    你的假设是正确的,当添加一个字段错误时,它会默认返回“INPUT”结果,导致输入结果被渲染。我建议研究实现 preparable,这将允许您始终在页面呈现之前填充组合框。

    【讨论】:

      【解决方案2】:

      您需要在方法中添加您的组合框代码,如下所示:

      public String execute() throws Exception {
      
      //add this code also as per your comobobx code
      countryList = new ArrayList<Country>();
              countryList.add(new Country(1, "India"));
              countryList.add(new Country(1, "Shri Lanka"));
              countryList.add(new Country(1, "USA"));
              countryList.add(new Country(1, "Pakistan"));
              countryList.add(new Country(1, "NewsLnad"));
      
      //add this code also as per your comobobx code
      
          Session session = this.getHibernateSession();
      
          Transaction tx = session.beginTransaction();
      
          //Following will set combo box containing country list
          this.setCountryListCombo();
      
          tx.commit();
      
          return SUCCESS;
      }
      

      并在你的jsp页面中添加以下标签

      &lt;s:actionerror /&gt;

      【讨论】:

        猜你喜欢
        • 2019-12-16
        • 1970-01-01
        • 1970-01-01
        • 2012-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-10
        • 2013-02-24
        相关资源
        最近更新 更多