0

i have action below

 @RequestMapping(value = "info_pure", method = RequestMethod.GET)
    public ModelAndView GetInfo(String ser_name, String[] p_input_week_days, int p_input_month_day) {
        ModelAndView mav = new ModelAndView(JspView.PureInfoAup);       
        String aaa = request.getParameter("p_input_week_days");

i send get request to this action via jquery ajax.

 var myarray = ['Element 1', 'Element 2', 'Element 3'];

        var dataobject = {
            ser_name: p_ser_name.trim(), p_input_week_days: myarray, p_input_month_day: p_month_day
        };

        $.ajax({
            url: '../info_pure',
            data: dataobject,
            type: 'GET',
            success: function(data) { /// go forward

but when request send it give me an error. it says that my array parameter is null

here is my result on firebug

enter image description here

2 Answers 2

1

Use Spring MVC @RequestParam annotation.

@RequestMapping(value = "info_pure", method = RequestMethod.GET)
public ModelAndView GetInfo(@RequestParam("p_input_week_days[]") String[] days) {   
    String[] aaa = days;

More details: http://docs.spring.io/spring/docs/4.0.0.RC1/spring-framework-reference/html/mvc.html#mvc-ann-requestparam

Sign up to request clarification or add additional context in comments.

3 Comments

add Jackson-Core and Jackson-mapper jars or dependencies in your project to have this functionality
yes it works after changing @RequestParam("p_input_week_days") to @RequestParam("p_input_week_days[]") ))
No need to add Jackson to classpath. This code does not use JSON conversion.
0

I had solved this problem using Google Gson library.

Update your controller method like this

 @RequestMapping(value = "info_pure", method = RequestMethod.GET)
    public ModelAndView GetInfo(String ser_name, String p_input_week_days, int p_input_month_day) {

      Gson gson=new Gson();
      String[] weeks=gson.fromJson(p_input_week_days, String[].class);
      // ....          

Update your Ajax call like this

    var myarray = ['Element 1', 'Element 2', 'Element 3'];

    var dataobject = {
        ser_name: p_ser_name.trim(), p_input_week_days: JSON.stringify(myarray), p_input_month_day: p_month_day
    };

    $.ajax({
        url: '../info_pure',
        data: dataobject,
        type: 'GET',
        success: function(data) { /// go forward

2 Comments

No necessity to USE Gson
i do not want to use another library. i worked asp.net mvc for a years. there was it simpler. But @shoorick code works after changing @RequestParam("p_input_week_days") to @RequestParam("p_input_week_days[])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.