0

我有一个游戏和一个开发者。该游戏有一组开发人员。我想创建一个可以将游戏添加到数据库的表单。在表格中,我输入名称、价格并检查开发商。所以我为每个开发人员创建了一个复选框。然而,当我检查那些页面似乎只是刷新。当我调试时,我的控制器似乎永远不会访问 doSubmitAction 函数。当我省略复选框时,一切都按预期工作。spring 无法创建集合吗?我不完全了解 Spring 幕后发生的事情。这是我使用 spring 创建的第一个项目。

这是我的表格:

 <form:form method="POST" commandName="game" >
                    <table>
                        <tr>
                            <td>
                                Name
                            </td>
                            <td>
                                <form:input path="gameNaam" size="20" />
                            </td>
                        </tr>

                        <tr>
                            <td>Choose Developers</td>
                            <td>
                                <form:checkboxes id="selectdeveloper" items="${developers}" path="developers" itemLabel="naam" />

                            </td>
                        </tr>
                        <tr>
                            <td>
                               Price
                            </td>
                            <td>
                                <form:input path="prijs" size="10" />
                            </td>
                        </tr>
                        <tr>
                            <td>

                                <input type="submit" value="Add" />

                            </td>
                            <td></td>
                        </tr>

                    </table>
                </form:form>

和表单控制器:

public class GameFormController extends SimpleFormController {

    private GameOrganizer gameOrganizer;

    public GameFormController() {
       setCommandClass(Game.class);
       setCommandName("game");
       setFormView("AddGame");
       setSuccessView("forward:/Gamedatabase.htm");
   }

    public void setGameOrganizer(GameOrganizer gameOrganizer){
       this.gameOrganizer=gameOrganizer;
    }

    @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
       Game game = null;
       long id = ServletRequestUtils.getLongParameter(request, "id");
       if(id<=0){
           game = new Game();
       }else{
           game = gameOrganizer.getGame(id);
       }
       return game;
    }



    @Override
    protected void doSubmitAction(Object command) throws Exception {

        Game game = (Game) command;
        if(game.getId()<=0){
            gameOrganizer.addGame(game);
        }else{
           gameOrganizer.update(game);

       }

   }

   @Override
   protected Map referenceData(HttpServletRequest request) throws Exception {
       Set<Developer> developers = gameOrganizer.getAllDevelopers();
       HashMap<String, Object> map = new HashMap<String, Object>();
      map.put("developers", developers);
      return map;
   }


}
4

1 回答 1

1

好吧,显然我必须为 Developer 创建一个 propertyEditor。这个网站上有一个很好的解释:

http://static.springsource.org/spring/docs/2.0.x/reference/validation.html

编辑额外信息:

因此,显然,当您选中一个复选框时,它会将值作为字符串提供给您。当然,Collection 必须使用开发人员对象来制作。

所以我创建了一个developerEditor:

package domainmodel;

import java.beans.PropertyEditorSupport;

public class DeveloperEditor extends PropertyEditorSupport {

    private GameOrganizer gameOrganizer;


    public void setGameOrganizer(GameOrganizer gameOrganizer) {
        this.gameOrganizer = gameOrganizer;
    }

    @Override
    public void setAsText(String id) {
        long id2 = Long.parseLong(id);
        Developer type = gameOrganizer.getDeveloper(id2);
        setValue(type);
    }
}

并使用复选框,我将对象的 id 作为 itemvalue

<form:checkboxes id="selectdeveloper" items="${allDevelopers}" itemValue="id" path="developers" itemLabel="name" />

然后在表单控制器中,我覆盖了 initBinder 方法。因此,当我 Spring 必须填写开发人员对象时,它将首先使用我的编辑器将其从字符串转换为开发人员对象。

 private DeveloperEditor developerEditor;

 public void setDeveloperEditor(DeveloperEditor developerEditor){
    this.developerEditor = developerEditor;
    developerEditor.setGameOrganizer(gameOrganizer);
}

 @Override
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
    binder.registerCustomEditor(Developer.class, developerEditor);
}

就是这样的人。如果有人有任何问题,我很乐意回答。

于 2012-05-07T08:34:22.163 回答