简体   繁体   English

用spring和bean列表创建一个表单

[英]Creating a form with spring and list of bean

I know there are similar questions to mine but I can't make it work. 我知道我有类似的问题,但我无法解决。 But please bear with me. 但是请忍受我。

Here is my bean 这是我的豆子

public class FLTQuestionnaireBean
{
    private int myQuestionId;
    private int myQuestionNum;
    private String myQuestion;
    private String myQuestionAnswer;
    private String myQuestionType;
    private String myQuestionModule;
    private String myQuestionStrand;
    private String myQuestionDifficulty;

    // getters and setters here
}

And my wrapper class: 和我的包装类:

public class QuestionnaireBeanWrapper
{
    private List<FLTQuestionnaireBean> myQuestionnaireBeanList;

    public void add( FLTQuestionnaireBean questionnaireBean )
    {
        myQuestionnaireBeanList.add( questionnaireBean );
    }

        //getter and setter here
}

EDIT: change modelAttribute from ${ wrapper } to "wrapper" Spring form 编辑:将modelAttribute从$ {wrapper}更改为“ wrapper” Spring形式

<form:form id="reading-form" action="../index/FLT_evaluate_reading.do" method="POST" modelAttribute="wrapper">
     <c:forEach items="${ wrapper.questionnaireBeanList }" varStatus="i">
           <form:input path="questionnaireBeanList[${i.index}].questionId" type="text"/>
           <form:input path="questionnaireBeanList[${i.index}].questionNum" type="text"/>
           <form:input path="questionnaireBeanList[${i.index}].question" type="text"/>
           <form:input path="questionnaireBeanList[${i.index}].questionAnswer" type="text"/>
           <form:input path="questionnaireBeanList[${i.index}].questionType" type="text"/>
           <form:input path="questionnaireBeanList[${i.index}].questionModule" type="text"/>
           <form:input path="questionnaireBeanList[${i.index}].questionStrand" type="text"/>
           <form:input path="questionnaireBeanList[${i.index}].questionDifficulty" type="text"/>
      </c:forEach>
</form:form>

Controller 控制者

@RequestMapping( value = "/index/FLT", method = RequestMethod.GET )
public String indexFLT( @RequestParam( defaultValue = "" )
String message, @RequestParam( defaultValue = "" )
String messageType, ModelMap model )
{
    model.addAttribute( "message", message );
    model.addAttribute( "messageType", messageType );

    QuestionnaireBeanWrapper wrapper = new QuestionnaireBeanWrapper();
    wrapper.add( new FLTQuestionnaireBean() );
    wrapper.add( new FLTQuestionnaireBean() );

    model.addAttribute( "wrapper", wrapper );

    return "als-questionnaire/flt";
}

I'm getting an exception when I run the app. 运行应用程序时出现异常。 I'm getting the error starting at first <form:input.. heres my stacktrace: 我从第一个<form:input..开始出现错误<form:input..我的堆栈跟踪:

EDIT: Change of stactrace 编辑:改变触角

   java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'com' available as request attribute
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:141)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:132)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:116)
...

Can you tell me what am I doing wrong? 你能告诉我我在做什么错吗? I really need help. 我真的需要帮助 Thanks in advance. 提前致谢。

Check spell of get-er in wrapper class it should be something like this 在包装器类中检查get-er的拼写应该是这样的

public List<FLTQuestionnaireBean> getQuestionnaireBeanList{
return myQuestionnaireBeanList
};

Change this 改变这个

<form:form id="reading-form" action="../index/FLT_evaluate_reading.do" method="POST" modelAttribute="${ wrapper }">

to

<form:form id="reading-form" action="../index/FLT_evaluate_reading.do" method="POST" modelAttribute="wrapper">

The form tag's modelAttribute element needs the name of the model attribute, not the object itself (or rather its toString() result), which you get from ${wrapper} . form标记的modelAttribute元素需要model属性的名称,而不是对象本身(或者是其toString()结果),该名称是从${wrapper}

The hint was 提示是

Neither BindingResult nor plain target object for bean name 'com' BeanResult'com '的 BindingResult和普通目标对象都不

I am sure if you do 我确定你愿意

wrapper.toString()

and print the result in your controller, you will see something like 并在控制器中打印结果,您将看到类似

com.somepackage.QuestionnaireBeanWrapper

It tries to use that to resolve the model attribute, resolving each token before a . 它尝试使用它来解析model属性,在之前解析每个标记. , failing on the first it doesn't find in the Model attributes. ,如果第一次失败,则无法在Model属性中找到。 In this case, there is nothing called com in the Model attributes, so it complains and fails. 在这种情况下, Model属性中没有称为com的内容,因此它会抱怨并失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM