Spring MVC表单验证既不是BindingResult也不是bean名称的明确目标对象' BanqueForm'可用作请求属性

时间:2015-10-23 09:38:17

标签: spring spring-mvc

使用spring MVC验证表单时遇到问题 我有这个例外 java.lang.IllegalStateException:既不是BindingResult也不是bean名称的明确目标对象' BanqueForm'可用作请求属性

这是我的控制器

package org.gestion.banque.controllers;

import java.util.Map;

import javax.validation.Valid;

import org.gestion.banque.entities.Compte;
import org.gestion.banque.metier.IBanqueMetier;


@Controller
public class BanqueController {

@Autowired
private IBanqueMetier metier;


@RequestMapping(value="/index",method=RequestMethod.GET)
public String index(Model model){
    model.addAttribute("BanqueForm", new BanqueForm());

    return"banque";
}

@RequestMapping(value="/chargerCompte", method=RequestMethod.GET)
public String charger(Model m) {
    m.addAttribute("BanqueForm",  new BanqueForm());
    return "banque";
}


@RequestMapping(value="/chargerCompte",method=RequestMethod.POST)
public String charger(@Valid BanqueForm bf,
        BindingResult result,Model model){

    if(result.hasErrors())
    {
        return "banque";
    }
    try {
        Compte c=metier.ConsulterCompte(bf.getCode());
        bf.setTypeCompte(c.getClass().getSimpleName());
        bf.setCompte(c);
    } catch (Exception e) {

        bf.setException(e.getMessage());
    }

    model.addAttribute("BanqueForm", bf);
    return "banque";
}

}

这是我的观点

<body>
    <div>
    <f:form modelAttribute="BanqueForm" method="post" action="chargerCompte" >
    <table>
    <tr>
        <td>Code :</td>
        <td><f:input path="code"/></td>
        <td><f:errors path="code"></f:errors> </td>
    </tr>
    <tr>
        <td>
        <input  type="submit" value="OK" />
        </td>
    </tr>
    </table>    
    </f:form>
    </div>
    <c:if test="${ not empty BanqueForm.exception}">
    <div>${BanqueForm.exception} </div>

    </c:if>

    <c:if test="${not empty BanqueForm.compte}">

        <div>
            <table>
            <tr>
                <td>Solde :</td>
                <td>${BanqueForm.compte.solde}</td>
            </tr>
            <tr>
                <td>Solde :</td>
                <td>${BanqueForm.compte.dateCreation}</td>
            </tr>
            <tr>
                <td>Type de Compte :</td>
                <td>${BanqueForm.typeCompte}</td>
            </tr>
            <c:if test="${BanqueForm.typeCompte=='CompteCourant'}">
                <tr>
                    <td>Decouvert :</td>
                    <td>${BanqueForm.compte.decouvert}</td>
                </tr>
            </c:if>
            <c:if test="${BanqueForm.typeCompte=='CompteEpargne'}">
                <tr>
                    <td>taux :</td>
                    <td>${BanqueForm.compte.taux}</td>
                </tr>
            </c:if>
            </table>
        </div>
    </c:if> 
</body>
</html>

1 个答案:

答案 0 :(得分:0)

获取@ModelAttribute("BanqueForm")对象时添加BanqueForm注释。像这样:

public String charger(@Valid @ModelAttribute("BanqueForm") BanqueForm bf,
        BindingResult result, Model model) { ... }