在Spring v3.1中替换SimpleFormController

时间:2012-12-02 00:11:15

标签: java spring spring-mvc

我正在尝试从springsource.org编写一些基本示例,并将示例中的SimpleFormController替换为某些@Controller和@RequestMapping注释。但是我在应用程序运行时得到的只是错误:java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'priceIncrease' available as request attribute

这是一些代码,在.jsp我有这个:

<form:form method="post" modelAttribute="priceIncrease">
 <table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5">
 <tr>
   <td align="right" width="20%">Increase (%):</td>
     <td width="20%">
      <form:input path="percentage"/>
     </td>
     <td width="60%">
      <form:errors path="percentage" cssClass="error"/>
     </td>
 </tr>
</table>
<br>
<input type="submit" align="center" value="Execute">
</form:form>

和控制器:

@Controller
@RequestMapping("/priceincrease.htm")
public class PriceIncreaseFormController {

@Autowired
PriceIncreaseValidator priceIncreaseValidator; 

protected final Log logger = LogFactory.getLog(getClass());

@RequestMapping(method= RequestMethod.POST)
public String onSubmit(@ModelAttribute("priceIncrease")PriceIncrease priceIncrease,   BindingResult result) throws ServletException {

    int increase = priceIncrease.getPercentage();
    priceIncreaseValidator.validate(increase, result);

    if (result.hasErrors()) {
        return "priceIncrease";
    }

    logger.info("Increasing prices by " + increase + "%.");

    return "redirect:home.htm";
  }

@RequestMapping(method=RequestMethod.GET)
public String initializeForm(ModelMap model) {
    Map<Integer, String> priority = new LinkedHashMap<Integer, String>();

    priority.put(1, "low");
    priority.put(2, "medium");
    priority.put(3, "high");

    model.addAttribute("priorityList", priority);

    return "priceincrease";

  }
}

据我所知,我在servlet.xml中不需要任何其他配置,对吗?所以你知道男人们的问题在哪里吗?

@ kmb385 感谢您的帮助,现在表单正确初始化,但是'onSubmit'和验证方法还存在另一个问题。现在代码看起来像这样:

@Autowired
PriceIncreaseValidator priceIncreaseValidator; 

protected final Log logger = LogFactory.getLog(getClass());

@Autowired
private ProductManager productManager;

@RequestMapping(method= RequestMethod.POST)
public String onSubmit(@ModelAttribute("priceIncrease")PriceIncrease priceIncrease, BindingResult result) throws ServletException {

    int increase = priceIncrease.getPercentage();
    logger.info("Increasing prices by " + increase + "%.");

    logger.info("result : " + result.toString());
    priceIncreaseValidator.validate(increase, result);

    productManager.increasePrice(increase);

    if (result.hasErrors()) {
        return "priceIncrease";
    }



    return "redirect:hello.htm";
}

并在

priceIncreaseValidator.validate(increase, result);

glassfish返回

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

PriceIncreaseValidator:

public class PriceIncreaseValidator implements Validator {

private int DEFAULT_MIN_PERCENTAGE = 0;
private int DEFAULT_MAX_PERCENTAGE = 50;
private int minPercentage = DEFAULT_MIN_PERCENTAGE;
private int maxPercentage = DEFAULT_MAX_PERCENTAGE;

@Override
public boolean supports(Class clazz) {
    return PriceIncrease.class.equals(clazz);

}

@Override
public void validate(Object arg0, Errors arg1) {
    PriceIncrease pi = (PriceIncrease) arg0;

    if (pi == null) {
        arg1.rejectValue("percentage", "error.not-specified", null, "cos wymagane");
    }
    else {
        if (pi.getPercentage() > maxPercentage) {
            arg1.rejectValue("percentage", "error.too-high", new Object[] {new Integer(maxPercentage)}, "za duzo");
        }
        if (pi.getPercentage() <= minPercentage) {
            arg1.rejectValue("percentage", "error.too-low", new Object[] {new Integer(minPercentage)}, "za malo");
        }
    }

}

public void setMinPercentage(int i) {
    this.minPercentage = i;
}

public int getMinPercentage() {
    return minPercentage;
}

public void setMaxPercentage(int i) {
    this.maxPercentage = i;
}

public int getMaxPercentage() {
    return maxPercentage;
}

}

你知道现在的错误吗?

1 个答案:

答案 0 :(得分:0)

初始化表单时,您需要向模型添加名称为PriceIncrease的{​​{1}}类型的属性。

priceIncrease