需要 struts2 验证帮助

发布于 2024-07-25 05:54:09 字数 2128 浏览 5 评论 0原文

我的 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}' 无法解析为集合/数组/映射/枚举/迭代器类型。 示例:people 或 people.{name}

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

行号: 522

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

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

请帮我解决问题。

谢谢。

I Have following in my struts.xml file

<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>

I have following in my html code

<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>

I have execute method as below.

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;
}

I am overriding validate method as below.

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

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

}

Now when I execute my action it will show me form with countryId combo box filled with countries.

Now when I submit form without selecting combo box it should show me error.

But instead of showing error "Please select country" It gives me following error.

Struts Problem Report

Struts has detected an unhandled exception:

Messages: tag 'select', field 'list', name 'countryId': The requested list key '%{countryListCombo}' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name}

File: org/apache/jasper/servlet/JspServletWrapper.java

Line number: 522

Can any one tell me why is it so?

It seems that after validate() method giving result="input", it is not calling execute() method and instead tries to show "/jsp/product_verification.jsp" page directly.

Please help me solving the problem.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为人所爱 2024-08-01 05:54:09

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

Your assumption is correct, when a field error is added, it will by default return the "INPUT" result, causing the input result to be rendered. I would suggest looking into implementing preparable, which would allow you to always populate the combobox prior to page rendering.

云巢 2024-08-01 05:54:09

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

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 页面中添加以下标记

You need to add your combo box code in method just like below:

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;
}

and also add the following tag in your jsp page

<s:actionerror />

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文