بحاجة إلى مساعدة في التحقق من صحة Struts2
-
06-07-2019 - |
سؤال
لقد اتبعت في ملف 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 Combo المليء بالبلدان.
الآن عندما أقوم بإرسال نموذج دون تحديد مربع التحرير والسرد ، يجب أن يظهر لي خطأ.
ولكن بدلاً من إظهار الخطأ "الرجاء تحديد البلد" ، فإنه يعطيني الخطأ التالي.
تقرير مشكلة الدعامات
اكتشف الدعامات استثناء غير معقول:
رسائل: علامة "حدد" ، الحقل "قائمة" ، اسم "CountryId": لا يمكن حل مفتاح القائمة المطلوبة "٪ {countrylistcombo}" كنوع مجموعة/خريطة/تعداد/تعداد. مثال: الناس أو الناس. {name}
ملف: org/apache/jasper/servlet/jspservletwrapper.java
رقم السطر: 522
هل يمكن لأي شخص أن يخبرني لماذا هو الحال؟
يبدو أنه بعد التحقق من صحة () تعطي النتيجة = "إدخال" ، لا يتم استدعاء طريقة تنفيذ () وبدلاً من ذلك يحاول إظهار "/jsp/product_verification.jsp" مباشرة.
الرجاء مساعدتي في حل المشكلة.
شكرًا.
المحلول
يكون افتراضك صحيحًا ، عند إضافة خطأ في الحقل ، سيتم إرجاع نتيجة "الإدخال" افتراضيًا ، مما يؤدي إلى تقديم نتيجة الإدخال. أود أن أقترح النظر في تنفيذ التحضير ، مما يتيح لك دائمًا ملء Combobox قبل عرض الصفحة.
نصائح أخرى
تحتاج إلى إضافة رمز مربع التحرير والسرد الخاص بك في الطريقة كما هو الحال أدناه:
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 الخاصة بك
<s:actionerror />