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」とvalidateメソッドには別の問題もあります。現在、コードは次のようになっています。
@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);
グラスフィッシュが戻ってくる
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;
}
}
今何が悪いのか分かりますか?