2014-01-20 115 views
1

大家好我是我的形式試圖使用驗證和我做了樣品中所有的東西,一派多次找到解決辦法,但遺憾的是我沒能解決我的問題。Struts 2的XML驗證工作不`噸

我的問題是:當我點擊空提交按鈕提交其註冊空數據庫和驗證不起作用!!!!!!!!!!!!

我使用struts2的tomcat7 JDK7 spring4 Hibernate3的和SiteMesh的2.4.2

任何機構可以幫助我,請

struts.xml

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 
    "http://struts.apache.org/dtds/struts-2.3.dtd"> 

<struts> 

    <constant name="struts.custom.i18n.resources" value="ApplicationResources"/> 
    <constant name="struts.ui.theme" value="xhtml"/> 
    <constant name="struts.devMode" value="true"/> 
    <!--<constant name="struts.objectFactory" value="spring" />--> 


    <package name="json" namespace="/" extends="json-default"> 
     <action name="state2CityAction" class="state2CityAction"> 
      <result name="SUCCESS" type="json" /> 
     </action> 
    </package> 


    <package name="default" namespace="/" extends="struts-default"> 

     <interceptors> 
      <interceptor-stack name="myStack"> 
       <interceptor-ref name="defaultStack"> 
        <param name="validation.excludeMethods">prepareNewInterView,prepare,list,previous,next,load,delete,prepareNewPersonnel,loadPayments,loadTeacherSalary, 
         searchInterViewed,findActives,findFrozen,findCanceled,findCanceled,findEducated,searchStudentBaseGroupLesson,prepareSearchStudentPayments,loadPersonnel4FishRegister, 
         prepareGroups4Score,prepareSearchStudentStatus,loadPersonnelFullPayment,listGroupLesson,listGroupLesson4Assign,prepareNewTeacher,listTeacher,prepareSearchTeacherSalary,find, 
         prepareSearchPersonnelSalaryRedirect,prepareSearchCostsPaymentRedirect,prepareSearchTeacher2AssignAccountNumber,createOtherRedirect, 
         prepareSearchPersonnel2AssignAccountNumber,listHesabCari,updateInterViewed,loadTeacherPaymentDetails4Edit,loadTeacherFullPayment, 
         loadTeacherPaymentDetails4Delete,prepareSearchPersonnelSalaryRedirect</param> 
       </interceptor-ref> 
      </interceptor-stack> 
     </interceptors> 


     <default-interceptor-ref name="myStack"/> 
     <default-action-ref name="index" /> 

     <global-results> 
      <result name="error">/error.jsp</result> 
     </global-results> 

    <action name="listBank" class="bankInfoAction" method="list"> 
      <result name="success">/listBank.jsp</result> 
     </action> 

     <action name="createBank" class="bankInfoAction" method="create"> 
      <result name="success">/listBank.jsp</result> 
      <result name="input">/createBank.jsp</result> 

     </action> 


     <action name="updateBank" class="bankInfoAction" method="update"> 
      <result name="input">/updateBank.jsp</result> 
      <result name="success">/listBank.jsp</result> 
     </action> 

     <action name="deleteBankInfo" class="bankInfoAction" method="delete"> 
     <result name="input">/deleteBank.jsp</result> 
     <result name="success">/listBank.jsp</result> 
     </action> 

     <action name="load2EditBank" class="bankInfoAction" method="load"> 
      <result name="success">/editBank.jsp</result> 
     </action> 

     <action name="load4DeleteBank" class="bankInfoAction" method="load"> 
      <result name="success">/deleteBank.jsp</result> 
     </action> 

BankInfoAction-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="bankInfo.bankName"> 
     <field-validator type="required"> 
      <message key="errors.required"/> 
     </field-validator> 
    </field> 

</validators> 

這是我的實體

BankInfo.java

@Entity 
public class BankInfo { 

    @Id 
    @TableGenerator(name = "ct_key", table = "keys_tbl", allocationSize = 70, initialValue = 11) 
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "ct_key") 
    long id; 
    String bankName; 

    public BankInfo() { 
    } 

    public BankInfo(String bankName) { 
     this.bankName = bankName; 


    public long getId() { 
     return id; 
    } 

    public void setId(long id) { 
     this.id = id; 
    } 

    public String getBankName() { 
     return bankName; 
    } 

    public void setBankName(String bankName) { 
     this.bankName = bankName; 
    } 

    public String toString(){ 
     return bankName ; 
    } 

} 

我的行動

BankInfoAction.java

import com.opensymphony.xwork2.ActionSupport; 
import com.opensymphony.xwork2.ModelDriven; 
import org.apache.struts2.interceptor.ParameterAware; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Scope; 
import org.springframework.stereotype.Component; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.Map; 

@Component 
public class BankInfoAction extends ActionSupport implements ParameterAware,ModelDriven { 

    @Autowired 
    BankInfoService bankInfoService; 
    BankInfo bankInfo = new BankInfo(); 
    List<BankInfo> banks = new ArrayList<BankInfo>(); 
    Map<String, String[]> request; 

    public String create(){ 
     bankInfoService.save(bankInfo); 
     list(); 
     return "success"; 
    } 


    public String update(){ 
     bankInfoService.update(bankInfo); 
     list(); 
     return "success"; 
    } 


    public String delete(){ 
     bankInfoService.delete(bankInfo); 
     list(); 
     return "success"; 
    } 


    public String list(){ 
     banks = bankInfoService.list(); 
     return "success"; 

    } 


    public String load(){ 
     bankInfo = bankInfoService.load(bankInfo.getId()); 
     return "success"; 
    } 


    public String cancel(){ 
     return "success"; 
    } 


    public BankInfoService getBankInfoService() { 
     return bankInfoService; 
    } 

    public void setBankInfoService(BankInfoService bankInfoService) { 
     this.bankInfoService = bankInfoService; 
    } 

    public BankInfo getBankInfo() { 
     return bankInfo; 
    } 

    public void setBankInfo(BankInfo bankInfo) { 
     this.bankInfo = bankInfo; 
    } 

    public List<BankInfo> getBanks() { 
     return banks; 
    } 

    public void setBanks(List<BankInfo> banks) { 
     this.banks = banks; 
    } 

    public Map<String, String[]> getRequest() { 
     return request; 
    } 

    public void setRequest(Map<String, String[]> request) { 
     this.request = request; 
    } 


    @Override 
    public void setParameters(Map<String, String[]> maps) { 
     request = maps; 
    } 

    @Override 
    public Object getModel() { 
     return bankInfo; 

    } 
} 

createBank.jsp

<%@ taglib prefix="s" uri="/struts-tags" %> 
<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<html> 
<head> 
    <title>Create Bank </title> 
</head> 
<body> 

    <s:form action="createBank" method="Post" validate="true"> 
      <table style="border: 1px solid #C1D0C8;" align="center" bgcolor="#f4f7f5" cellpadding="20" cellspacing="0" height="320" 
        width="550"> 
      <tr> 
      <td align="center" valign="top"> 
      <table border="0" width="95%"> 
       <div class="divForms"> 
       <div class="item"> 
        <div class="label"> 
         <span><s:text name="Bank.Name"/></span> 
         <span style="color:red;"> * </span> 
        </div> 
        <div class="control"> 
         <s:textfield name="bankName" cssClass="normal_input" theme="simple" /> 
        </div> 
       </div> 


        <div class="But"> 
         <s:submit type="submit" cssClass="gradient-button" name="newsletter_submit" id="newsletter_submit" key="Save" theme="simple"/> 
         <s:submit type="submit" cssClass="Cancel" name="newsletter_submit" id="newsletter_submit" key="Cancel" theme="simple"/> 
        </div> 

      </div> 

      </table> 
       </tr> 
      </table> 

      </s:form> 
</body> 
</html> 

這是我的lib目錄下:http://i.stack.imgur.com/V2KkW.jpg

而另一件事是BankInfoAction-validation.xml中和BankInfoAction.java是在同一個目錄

+2

這就是排除法的瘋狂數量。考慮以不同的方式組織它。 –

+0

Lol @excludes,但在你的第一個問題中付出很多努力+1。 –

回答

0

您應該使用

<field name="bankName"> 
    <field-validator type="requiredstring"> 
     <message key="errors.required"/> 
    </field-validator> 
</field> 

,因爲這個領域被填充。 modelDriven攔截器將模型置於值棧頂部。所以,你不應該擔心設置值。

+0

感謝您的回覆,我沒有太,但它仍然不'噸工作 – user3215984

+0

好吧,我想通了,你需要另一種驗證 –

+0

你能解釋更多嗎? – user3215984

0

3天之後我終於可以解決我的問題,我的老師的幫助: 我做了所有的配置進行驗證正如你看到的,但: 第一個問題是:validatoin沒有工作後提交>>我的Struts版本是Struts2的-core-2.3.14.3.jar我將其更改爲struts2-core-2.3.15.x.jar並且它工作正常:)

第二個問題是:提交驗證消息後沒有顯示>>> after 1有一天,我找到了解決辦法>>>>>問題是主題在我的文本框=「簡單」我改成了主題=「css_xhtml」