4

次のようにマッピングされたリクエストがあります

@RequestMapping(value = "/path", method = RequestMethod.POST)
public ModelAndView createNewItem(@ModelAttribute PostRequest request)

PostRequestにはegのようないくつかのプロパティがありますが、クライアントはの代わりにのuserName (getUserName()/setUserName())ようなパラメータを送信します。これらすべての醜いメソッドを配置せずにこれを行うためのアノテーションまたはカスタムマッピングインターセプターはありますか?user_name=foouserName=foosetUser_name()

これは非常に頻繁に発生するため(すべてがアンダースコアを使用しているAPIを実装する必要があります)、実装にある程度の努力を払うことは許容されます。

4

1 に答える 1

0

Spring のフォーム タグ ライブラリを使用しないのはなぜですか? http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/view.html#view-jsp-formtaglib

taglib (コントローラーと組み合わせて) は、ModelAttribute を自動的にマップします。フォームの GET リクエストを実行するとき、PostRequest の新しい (おそらく空の) オブジェクトを作成し、それをモデルに貼り付けます。POST 後、フォーム スプリングは ModelAttribute にフォーム値を提供します。

回路図の例:

コントローラ:

@RequestMapping(value="/path", method = RequestMethod.GET)
public String initForm(ModelMap model) {

        PostRequest pr = new PostRequest();
        model.addAttribute("command", pr);

        return "[viewname]";
    }

@RequestMapping(value="/path", method = RequestMethod.POST)
public ModelAndView postForm(
        @ModelAttribute("command") PostRequest postRequest) {

        // postRequest should now contain the form values
        logger.debug("username: " + postRequest.getUsername());

        return "[viewname]";
     }

jsp:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<form:form method="post" enctype="utf8">
    Username: <form:input path="username" />
   <br/>
   <%-- ... --%>
   <input type="submit" value="Submit"/>
</form:form>
于 2012-04-14T15:02:08.017 に答える