我一直在研究struts。
我正在使用 Struts 2 在应用程序中模拟编程验证和转换错误。
在我的应用程序中,我有一个名为 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;
}
}
我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>
如果用户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>
我的 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;
}
}
如果我们提供无效数据,例如价格小于或等于零或 productId 为空,则将触发验证。
如果我们在价格字段中输入字母,则会触发转换错误。
但是,如果我在价格字段中给出字母,那么我的product
对象在进入方法时所有变量都变为空值,当我尝试从类对象访问时validate()
,这会导致方法中出现空指针异常。validate()
productId
product
ProductAction
product
如果我在. ProductAction
_price
ProductForm.jsp