0

我是 Spring MVC 的新手,遵循“Spring in Action”一书,从头开始创建自己的项目。尝试访问“reward.jsp”时出现以下异常:

java.lang.IllegalStateException:Bean 名称“reward”的 BindingResult 和普通目标对象都不能用作请求属性

我的课程内容:

奖励.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf"%>
<html>
<head>
<title>Rewards</title>
</head>
<body>
    <h1>Rewards module</h1>

<h2>Add reward</h2>
<sf:form action="add" method="post" modelAttribute="reward">
    Reward name:<sf:input path="name" />
    Point value:<sf:input path="pointValue" />
    <input type="submit" value="Add">
</sf:form>


<br />
<h2>Reward list</h2>
<table>
    <c:forEach var="reward" items="${rewardList}">
        <tr>
            <td>${reward.name}</td>
            <td>${reward.pointValue}</td>
            <td><a href="delete/${reward.id}">delete</a></td>
        </tr>
    </c:forEach>
</table>

当我删除表单输入时它工作得很好。

奖励控制器.java

@Controller
public class RewardController {

@Autowired
private RewardService rewardService;

@RequestMapping("/reward")
public String listRewards(Map<String, Object> model) {
    model.put("rewardList", rewardService.listReward());
    return "reward";
}

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addReward(@ModelAttribute("reward") Reward reward,
        BindingResult result) {

    rewardService.addReward(reward);
    return "redirect:/reward";
}

@RequestMapping("/delete/{rewardId}")
public String deleteContact(@PathVariable("rewardId") long rewardId) {
    rewardService.removeReward(rewardId);
    return "redirect:/reward";
}
}
4

1 回答 1

0

更改您的listRewards方法,以便将表单支持对象添加到模型映射model.put("reward", new Reward())

@RequestMapping("/reward")
public String listRewards(Map<String, Object> model) {
    model.put("rewardList", rewardService.listReward());
    model.put("reward", new Reward())
    return "reward";
}
于 2013-03-07T10:15:33.830 回答