我是Spring MVC的新手,试图编写基于Web的应用程序。

 org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: setAttribute:


名称不可序列化的属性
InventoryMgmtSpring.web.PriceIncreaseFormController.FORM.priceIncrease
根本原因

 java.lang.IllegalArgumentException: setAttribute: Non-serializable attribute with name InventoryMgmtSpring.web.PriceIncreaseFormController.FORM.priceIncrease


app-servelet.xml中的代码片段

  <bean name="/priceincrease.htm"   class="InventoryMgmtSpring.web.PriceIncreaseFormController">
    <property name="sessionForm" value="true"/>
    <property name="commandName" value="priceIncrease"/>
    <property name="commandClass" value="InventoryMgmtSpring.service.PriceIncrease"/>
    <property name="validator">
        <bean class="InventoryMgmtSpring.service.PriceIncreaseValidator"/>
    </property>
    <property name="formView" value="priceincrease.htm"/>
    <property name="successView" value="hello.htm"/>
    <property name="productManager" ref="productManager"/>
</bean>


来自PriceIncreaseFormController的代码-----

 public class PriceIncreaseFormController extends SimpleFormController {

/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());
private ProductManager productManager;

public ModelAndView onSubmit(Object command) throws ServletException {
    int increase = ((PriceIncrease) command).getPercentage();
    logger.info("Increasing prices by " + increase + "%.");
    productManager.increasePrice(increase);
    logger.info("returning from PriceIncreaseForm view to " + getSuccessView());
    return new ModelAndView(new RedirectView(getSuccessView()));
}

protected Object formBackingObject(HttpServletRequest request) throws ServletException {
    PriceIncrease priceIncrease = new PriceIncrease();
    priceIncrease.setPercentage(20);
    return priceIncrease;
}
public void setProductManager(ProductManager productManager) {
    this.productManager = productManager;
}
public ProductManager getProductManager() {
    return productManager;
    }
    }


PriceIncrease中的代码段
= ------------------------------

 public class PriceIncrease {

/** Logger for this class and subclasses */

protected  final Log logger = LogFactory.getLog(getClass());
private int percentage ;
public void setPercentage(int i)
{
    this.percentage=i;
    logger.info("Percentage set to" + i);
}
public int getPercentage()
{
    return percentage;
  }
 }

最佳答案

首先,我将避免在PriceIncrease中使用Logger,而将PriceIncrease视为POJO。很难确定问题的根本原因到底是什么,但是我认为问题可能出在您尝试序列化记录器时。尝试删除记录器或在log变量之前添加关键字transient。

希望对您有所帮助。

10-08 18:25