需要 struts2 验证帮助
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的假设是正确的,当添加字段错误时,它会默认返回“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.
您需要在方法中添加组合框代码,如下所示:
并在 jsp 页面中添加以下标记
You need to add your combo box code in method just like below:
and also add the following tag in your jsp page
<s:actionerror />