java.lang.IllegalArgumentException:setAttribute:具有名称的非序列化属性

时间:2016-01-03 21:55:56

标签: java spring-mvc serialization

我是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;
  }
 }

3 个答案:

答案 0 :(得分:0)

首先,我会避免在PriceIncrease中使用Logger并将PriceIncrease更像是POJO。很难确定问题的根本原因究竟是什么,但我相信问题可能出在您尝试序列化记录器时。尝试删除记录器或在日志变量之前添加关键字transient。

我希望它有所帮助。

答案 1 :(得分:0)

错误告诉您priceIncrease FORM属性的PriceIncreaseFormController属性属于未实现java.io.Serializable的类型。我在您发布的代码中看不到任何FORM属性,因此已发布的代码已被修改,或者继承树中的属性更高(SimpleFormController)。

答案 2 :(得分:0)

谢谢大家。我发现了错误......

1)类PriceIncrease实现Serializable。

2)价值必须改变为&#34;价格增加&#34;而不是&#34; priceincrease.htm&#34;

代码工作正常。再次感谢您的帮助