1

関連する質問を見て、どれも役に立たなかったものを試しました。次のようにjqueryでPOSTリクエストを送信しています:

var data = {};          
            //this works every time and it's not issue
            var statusArray = $("#status").val().split(',');  
            var testvalue = $("#test").val();

                     data.test = testvalue;
            data.status = statusArray ;

             $.post("<c:url value="${webappRoot}/save" />", data, function() {
        })

コントローラー側では、次のことを試しました:

public void saveStatus(ModelMap model, Principal principal, HttpSession session, final HttpServletResponse response, @RequestParam String test, @RequestBody String [] status) {

        //I never get to this point, but when I set statusArray to required false test variable is being populated correctly
        }


public void saveStatus(ModelMap model, Principal principal, HttpSession session, final HttpServletResponse response, @RequestParam String test, @RequestParam String [] status) {

        //I never get to this point, but when I set statusArray to required false test variable is being populated correctly
        }



public void saveStatus(ModelMap model, Principal principal, HttpSession session, final HttpServletResponse response, @RequestParam String test, @RequestParam("status") String [] status) {

        //I never get to this point, but when I set statusArray to required false test variable is being populated correctly
        }


public void saveStatus(ModelMap model, Principal principal, HttpSession session, final HttpServletResponse response, @RequestParam String test, @RequestParam(name="status") String [] status) {

        //I never get to this point, but when I set statusArray to required false test variable is being populated correctly
        }

これらはどれもうまくいきませんでしたBad request

4

3 に答える 3

1

Your status param should be @RequestParam(value = "status[]") String[] status (Spring 3.1).

于 2012-10-20T00:51:45.987 に答える
0

あなたの問題は、実際にパラメーターを複数回送信する必要があるものに配列を送信することであると思います。

GET 操作の場合: ?status=FOO&status=BAR

春がカンマ区切りの文字列を自動的に配列に変換するかどうかはわかりません。ただし、PropertyEditor (PropertyEditorSupport を参照) を追加して、文字列をカンマで分割することもできます。

@InitBinder
public void initBinder(WebDataBinder binder) {
   binder.registerCustomEditor(String[].class, new PropertyEditorSupport() {
        @Override
        public String getAsText() {
            String value[] = (String[]) getValue();
            if (value == null) {
                return "";
            }
            else {
                return StringUtils.join(value, ",");
            }
        }

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            if (text == null || text.trim().length() == 0) {
                setValue(null);
            }
            else {
                setValue(StrTokenizer.getCSVInstance(text).getTokenArray());
            }
        }

    });
}

注意してください、私は commons-lang を使用して文字列の結合と分割の両方を行っていますが、必要な手段を使用して自分で簡単に行うことができます。

これを行うと、パラメーターを単一の文字列から String[] にバインドする必要があるときはいつでも、Spring が自動的に変換します。

于 2012-10-20T01:00:25.793 に答える