简体   繁体   English

Struts2中的转换和验证问题

[英]Conversion and validation issue in Struts2

I have been working on struts for a long time. 我从事支柱工作已经很长时间了。

I am simulating programmatic validations and conversion errors in an application using Struts 2. 我正在使用Struts 2在应用程序中模拟程序验证和转换错误。

In my application I have a model class called Product which is as shown below: 在我的应用程序中,有一个名为Product的模型类,如下所示:

public class Product {

private String productId;
private String productName;
private int price;

public Product() {}

public Product(String productId, String productName, int price) {
    this.productId = productId;
    this.productName = productName;
    this.price = price;
}

public String getProductId() {
    return productId;
}

public void setProductId(String productId) {
    System.out.println("Product.setProductId() : '"+productId+"'");
    this.productId = productId;
    System.out.println("This.pid : "+this.productId);
}

public String getProductName() {
    return productName;
}

public void setProductName(String productName) {
    System.out.println("Product.setProductName() : '"+productName+"'");
    this.productName = productName;
}

public int getPrice() {
    return price;
}

public void setPrice(int price) {
    System.out.println("Product.setPrice() : '"+price+"'");
    this.price = price;
}

}

I have jsp called ProductForm.jsp where I ask user to enter product info like below: 我有一个名为ProductForm.jsp jsp,请用户在其中输入如下产品信息:

<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
    <title>Add Product Form</title>
    <style type="text/css">@import url(css/main.css);</style>
</head>
<body>
    <div id="global">
        <h3>Add a product</h3>
        <s:form method="post" action="Product_Save.action">
            <s:textfield label="Id" key="productId"/>
            <s:textfield label="Name" key="productName"/>
            <s:textfield label="Price" key="price"/>
            <s:submit value="Add Product"/>
        </s:form>
        <s:debug/>
    </div>
</body>
</html>

If user clicks on Add Product button by giving correct info data will be saved to database according to normal flow which is configured in struts.xml which is as below: 如果用户通过提供正确的信息单击“ Add Product按钮,则数据将按照struts.xml中配置的常规流程保存到数据库中,如下所示:

<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="product_module" extends="struts-default">

    <action name="Product_Input">
        <result>/jsp/ProductForm.jsp</result>
    </action>

    <action name="Product_Save" class="com.way2learnonline.ui.ProductAction">
        <result name="success">/jsp/ProductDetails.jsp</result>
        <result name="input">/jsp/ProductForm.jsp</result>
        <param name="test">MyTest</param>
    </action>

</package>

</struts>

My Action class is ProductAction which is as shown below: 我的Action类是ProductAction ,如下所示:

public class ProductAction extends ActionSupport implements ModelDriven<Product>{

private static final long serialVersionUID = 1L;

private Product product;

private String test;

public ProductAction() {}

public String execute(){
    DatabaseManager databaseManager=new DatabaseManager();
    databaseManager.write(product);
    return Action.SUCCESS;
}

@Override
public void validate() {

    System.out.println("p : "+product.getProductId());
    if(product.getProductId().trim().equals("")){
        addFieldError("productId", "Product Id should be present");
    }
    if(product.getPrice()<=0){
        addFieldError("price", getText("price.negative"));
    }
}

@Override
public Product getModel() {
    product=new Product();
    return product;
}

public void setTest(String test) {
    this.test = test;
}

public String getTest() {
    return test;
}
}

If we give invalid data like price less than or equal to zero or productId is empty then validation will be triggered. 如果我们提供无效数据(例如价格小于或等于零或productId为空),则会触发验证。

If we give alphabets in price field then conversion error will be triggered. 如果我们在价格字段中输入字母,则会触发转换错误。

But If I give alphabets in price field then my product object all variables are becoming nulls while it goes to validate() method, which is resulting null pointer exception in validate() method when I try to access productId from product object of ProductAction class. 但是,如果我给字母在价格领域那么我的product对象,而它关系到所有变量都变成空validate()方法,这是导致空指针异常validate()方法时我试图访问productIdproduct的对象ProductAction类。

Here why is my product object variables of ProductAction class are becoming null if I give alphabets in price field in ProductForm.jsp . 如果我在ProductForm.jsp price字段中输入字母,为什么我的ProductAction类的product对象变量变为null。

Put the model initialization in the declaration, not in the getter: 将模型初始化放在声明中,而不是在getter中:

private Product product = new Product();

@Override
public Product getModel() {
    return product;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM