Struts 2中不显示基于注释的验证消息

时间:2014-12-26 08:05:33

标签: java hibernate jsp validation struts2

我在项目中使用基于Struts 2注释的验证。这是我的实体类在这里我只验证了一些字段

public class Vendor implements Serializable {

 private String vendorName;

 public String getVendorName() {
    return vendorName;
}

@RequiredStringValidator(message = "VendorName is Required", type = ValidatorType.FIELD)
public void setVendorName(String vendorName) {
    this.vendorName = vendorName;
}

}

这是我的动作类,它实现了模型驱动的接口

public class AddVendorAction extends ActionSupport implements ModelDriven<Vendor> {

private Vendor v;

public AddVendorAction() {
    this.v = new Vendor();
}

public String save() {
    System.out.println("hhhhhhhhhWE");
    vd.addVendor(getV());

    return SUCCESS;
}

public String populateItems() {
    leftsideVendorlist = vd.itemSet();
    return SUCCESS;
}

public String vendorList() {
    vendorlist = vd.list();
    return SUCCESS;
}

public Vendor getV() {
    return v;
}

public void setV(Vendor v) {
    this.v = v;
}

public List<Vendor> getVendorlist() {
    return vendorlist;
}

public void setVendorlist(List<Vendor> vendorlist) {
    this.vendorlist = vendorlist;
}

@Override
public Vendor getModel() {
    return v;
}
}

这是我的struts.xml文件,它扩展了hibernate-default包

   

 <struts>
   <package name="purchasemodule" namespace="/" extends="hibernate-default">    
     <action name="AddVendor" class="com.elegant.purchasemodule.purchasemasters.vendor.AddVendorAction" 
        method="save">            
       <interceptor-ref name="defaultStack" />
        <result name="input" type="chain">populateitems</result>
        <result name="success" type="redirect">vendorlist</result>
    </action>
    <action name="vendorlist"  class="com.elegant.purchasemodule.purchasemasters.vendor.AddVendorAction" 
            method="vendorList">
        <result name="success">/VendorList.jsp</result>
    </action>
    <action name="populateitems" class="com.elegant.purchasemodule.purchasemasters.vendor.AddVendorAction" 
            method="populateItems">
        <result name="success">/addvendor.jsp</result>  
        <result name="input" >/addvendor.jsp</result>
    </action>
  </package> 
</struts>

这是我的jsp页面

<form  action="AddVendor" method="post" class="form-horizontal" >
    <label class="col-md-3 control-label" for="textinput">Vendor Name</label>  
        <s:textfield name="vendorName" cssClass="form-control input-md"></s:textfield>
        </form>

它没有显示任何验证信息。我无法找出问题所在。任何人都可以帮助我。

2 个答案:

答案 0 :(得分:0)

您应该在model属性

上使用访问者字段验证程序
@Override
@VisitorFieldValidator(appendPrefix = false)
public Vendor getModel() {
    return v;
}

答案 1 :(得分:0)

根据@Roman C提供的建议。这个验证之类的更改正在进行中。在我的行动类中添加了@VisitorFieldValidator(appendPrefix = false)

public class AddVendorAction extends ActionSupport implements ModelDriven<Vendor>,Preparable {
 @Override
@VisitorFieldValidator(appendPrefix = false)
public Vendor getModel() {
    return v;
} 
@Override
public void prepare() throws Exception {
     leftsideVendorlist = vd.itemSet();
}
}

在Struts.xml文件中添加了defaultStack

<action name="AddVendor" 
            class="com.elegant.purchasemodule.purchasemasters.vendor.AddVendorAction" 
            method="save">            
        <interceptor-ref name="defaultStack"/>
        <result name="input">/addvendor.jsp</result>
        <result name="success" type="redirect">vendorlist</result>
    </action>

在Jsp页面中添加了<s:error标签及相应的字段名称

<s:form  action="AddVendor" method="post" cssClass="form-horizontal" theme="simple">

 <label class="col-md-3 control-label" for="textinput">Vendor Name</label>  
 <s:textfield id="vendorName" name="vendorName" cssClass="form-control input-md"/> 
 <s:fielderror fieldName="vendorName" cssClass="alert-danger"/>
 </s:form>