0

我有一个使用 jquery 显示/隐藏某些字段的视图(jsp)。我正在使用 jquery 使用 ajax 将数据发布到控制器(spring mvc)。当我只是在页面中输入值时效果很好,但是如果我单击一个链接以取消隐藏 2 个文本框,将值输入其中然后发送到控制器,我得到了这个异常。我检查了 spring 日志,我可以看到所有属性都转换为与 spring 绑定,但是在引发异常之后不久。以下是我在日志中可以看到的内容。我已经多次检查了所有参数,并且在 jsp/js/java 中没有任何遗漏/拼写错误。任何帮助将非常感激

 19:40:16,875 DEBUG GenericConversionService:254 - Caching under ConverterCacheKey [sourceType = java.lang.String, targetType = @org.springframework.web.bind.annotation.RequestParam java.lang.String]
java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,640 DEBUG GenericConversionService:186 - Converted to 540
19:40:17,644 DEBUG GenericConversionService:139 - Checking if I can convert java.lang.String to @org.springframework.web.bind.annotation.RequestParam int
19:40:17,646 DEBUG GenericConversionService:358 - Searching for converters indexed by sourceType [java.lang.String]
19:40:17,647 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Integer]
19:40:17,649 DEBUG GenericConversionService:473 - Found matchable converters java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,651 DEBUG GenericConversionService:560 - Matching java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,653 DEBUG GenericConversionService:570 - Did not match converter java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,654 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Comparable]
19:40:17,656 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Number]
19:40:17,658 DEBUG GenericConversionService:473 - Found matchable converters java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,660 DEBUG GenericConversionService:576 - Matched converter java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,661 DEBUG GenericConversionService:254 - Caching under ConverterCacheKey [sourceType = java.lang.String, targetType = @org.springframework.web.bind.annotation.RequestParam int]
19:40:17,663 DEBUG GenericConversionService:147 - Yes, I can convert
19:40:17,665 DEBUG GenericConversionService:159 - Converting value '1380' of java.lang.String to @org.springframework.web.bind.annotation.RequestParam int
19:40:17,667 DEBUG GenericConversionService:246 - Matched cached converter java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,668 DEBUG GenericConversionService:186 - Converted to 1380
19:40:17,672 DEBUG GenericConversionService:139 - Checking if I can convert java.lang.String to @org.springframework.web.bind.annotation.RequestParam int
19:40:17,673 DEBUG GenericConversionService:358 - Searching for converters indexed by sourceType [java.lang.String]
19:40:17,675 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Integer]
19:40:17,677 DEBUG GenericConversionService:473 - Found matchable converters java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,679 DEBUG GenericConversionService:560 - Matching java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,680 DEBUG GenericConversionService:570 - Did not match converter java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,682 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Comparable]
19:40:17,684 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Number]
19:40:17,685 DEBUG GenericConversionService:473 - Found matchable converters java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,687 DEBUG GenericConversionService:576 - Matched converter java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,689 DEBUG GenericConversionService:254 - Caching under ConverterCacheKey [sourceType = java.lang.String, targetType = @org.springframework.web.bind.annotation.RequestParam int]
19:40:17,691 DEBUG GenericConversionService:147 - Yes, I can convert
19:40:17,693 DEBUG GenericConversionService:159 - Converting value '540' of java.lang.String to @org.springframework.web.bind.annotation.RequestParam int
19:40:17,694 DEBUG GenericConversionService:246 - Matched cached converter java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,696 DEBUG GenericConversionService:186 - Converted to 540
19:40:17,699 DEBUG GenericConversionService:139 - Checking if I can convert java.lang.String to @org.springframework.web.bind.annotation.RequestParam int
19:40:17,701 DEBUG GenericConversionService:358 - Searching for converters indexed by sourceType [java.lang.String]
19:40:17,703 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Integer]
19:40:17,705 DEBUG GenericConversionService:473 - Found matchable converters java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,707 DEBUG GenericConversionService:560 - Matching java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,708 DEBUG GenericConversionService:570 - Did not match converter java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,710 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Comparable]
19:40:17,712 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Number]
19:40:17,713 DEBUG GenericConversionService:473 - Found matchable converters java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,715 DEBUG GenericConversionService:576 - Matched converter java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,717 DEBUG GenericConversionService:254 - Caching under ConverterCacheKey [sourceType = java.lang.String, targetType = @org.springframework.web.bind.annotation.RequestParam int]
19:40:17,719 DEBUG GenericConversionService:147 - Yes, I can convert
19:40:17,720 DEBUG GenericConversionService:159 - Converting value '1380' of java.lang.String to @org.springframework.web.bind.annotation.RequestParam int
19:40:17,722 DEBUG GenericConversionService:246 - Matched cached converter java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,724 DEBUG GenericConversionService:186 - Converted to 1380
19:40:17,727 DEBUG GenericConversionService:139 - Checking if I can convert java.lang.String to @org.springframework.web.bind.annotation.RequestParam int
19:40:17,729 DEBUG GenericConversionService:358 - Searching for converters indexed by sourceType [java.lang.String]
19:40:17,731 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Integer]
19:40:17,732 DEBUG GenericConversionService:473 - Found matchable converters java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,734 DEBUG GenericConversionService:560 - Matching java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,736 DEBUG GenericConversionService:570 - Did not match converter java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,737 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Comparable]
19:40:17,739 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Number]
19:40:17,741 DEBUG GenericConversionService:473 - Found matchable converters java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,743 DEBUG GenericConversionService:576 - Matched converter java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,744 DEBUG GenericConversionService:254 - Caching under ConverterCacheKey [sourceType = java.lang.String, targetType = @org.springframework.web.bind.annotation.RequestParam int]
19:40:17,746 DEBUG GenericConversionService:147 - Yes, I can convert
19:40:17,748 DEBUG GenericConversionService:159 - Converting value '540' of java.lang.String to @org.springframework.web.bind.annotation.RequestParam int
19:40:17,750 DEBUG GenericConversionService:246 - Matched cached converter java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,780 DEBUG GenericConversionService:186 - Converted to 540
19:40:17,784 DEBUG GenericConversionService:139 - Checking if I can convert java.lang.String to @org.springframework.web.bind.annotation.RequestParam int
19:40:17,786 DEBUG GenericConversionService:358 - Searching for converters indexed by sourceType [java.lang.String]
19:40:17,788 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Integer]
19:40:17,790 DEBUG GenericConversionService:473 - Found matchable converters java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,792 DEBUG GenericConversionService:560 - Matching java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,794 DEBUG GenericConversionService:570 - Did not match converter java.lang.String -> @org.springframework.format.annotation.NumberFormat java.lang.Integer: org.springframework.format.number.NumberFormatAnnotationFormatterFactory@4a6ca1a6
19:40:17,795 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Comparable]
19:40:17,797 DEBUG GenericConversionService:429 - and indexed by targetType [java.lang.Number]
19:40:17,799 DEBUG GenericConversionService:473 - Found matchable converters java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,800 DEBUG GenericConversionService:576 - Matched converter java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,802 DEBUG GenericConversionService:254 - Caching under ConverterCacheKey [sourceType = java.lang.String, targetType = @org.springframework.web.bind.annotation.RequestParam int]
19:40:17,804 DEBUG GenericConversionService:147 - Yes, I can convert
19:40:17,806 DEBUG GenericConversionService:159 - Converting value '1380' of java.lang.String to @org.springframework.web.bind.annotation.RequestParam int
19:40:17,807 DEBUG GenericConversionService:246 - Matched cached converter java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@21e8bf76
19:40:17,809 DEBUG GenericConversionService:186 - Converted to 1380
19:40:17,812 DEBUG HandlerMethodInvoker:173 - Invoking request handler method: public com.zeater.guru.admin.command.ResultVO com.zeater.guru.admin.controller.RestaurantController.addRestaurant(java.lang.String,java.lang.String,java.lang.String,int,java.lang.String,java.lang.String,java.lang.String,java.lang.String,int,int,int,float,float,int,float,boolean,boolean,int,java.lang.String,java.lang.String,java.lang.String,int,int,int,int,int,int,int,int,int,int,int,int,int,int,org.springframework.ui.Model)
19:40:17,815 DEBUG AnnotationMethodHandlerExceptionResolver:132 - Resolving exception from handler [com.zeater.guru.admin.controller.RestaurantController@144683c2]: java.lang.IllegalArgumentException
19:40:17,818 DEBUG ResponseStatusExceptionResolver:132 - Resolving exception from handler [com.zeater.guru.admin.controller.RestaurantController@144683c2]: java.lang.IllegalArgumentException
19:40:17,820 DEBUG DefaultHandlerExceptionResolver:132 - Resolving exception from handler [com.zeater.guru.admin.controller.RestaurantController@144683c2]: java.lang.IllegalArgumentException
19:40:17,822 DEBUG DispatcherServlet:667 - Cleared thread-bound request context: org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper@188edd79
19:40:17,824 DEBUG DispatcherServlet:671 - Could not complete request
java.lang.IllegalArgumentException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
4

1 回答 1

1

一个完整的堆栈跟踪将不胜感激。问题可能是,spring 无法转换您的方法参数之一。你应该重构那个方法,它的参数太多了。您可以创建一个包含所有参数的 dto(Spring 提供的参数除外)。

编辑:

而不是使用大量的@RequestParam注释。您可以将所有方法参数包装在一个包装对象 (DTO) 中。

例子:

public class MyWrapperDTO {
    private String someStrValue;
    private String anotherStrValue;
    private int someIntValue;

    // getters/setters omitted
}

从这里您可以选择 2 个选项:使用@ModelAttribute@RequestBody

对于第一个:

@Controller
public class Controller {
    @RequestMapping(value = "/mypage/view")
    public ModelAndView viewPage() {
        MyWrapperDTO dto = new MyWrapperDTO();
        // you could provide default values for your form here
        return new ModelAndView("/mypage").
            .addObject("form", dto);
    }

    @RequestMapping(value = "/mypage/save", method = RequestMethod.POST)
    @ResponseBody
    public void doSomething(@ModelAttribute("form") MyWrapperDTO dto) {
        // process your ajax call here
        // do something
    }
}

您的 jsp 应如下所示:

<spring:url var="myAjaxCall" value="/mypage/save" />

<!-- you should use spring form tag -->
<form:form commandName="form" action="${myAjaxCall}">
   <form:input path="someStrValue"></form:input>
   <form:input path="anotherStrValue"></form:input>
   <form:input path="someIntValue"></form:input>
   <input type="submit" value="Submit"></input>
</form:form>

第二个选项 ( @RequestBody),假设您要发送一个 JSON 对象(这涉及一些 jquery):

@Controller
public class Controller {
    @RequestMapping(value = "/mypage/view")
    public ModelAndView viewPage() {
        MyWrapperDTO dto = new MyWrapperDTO();
        // you could provide default values for your form here
        return new ModelAndView("/mypage").
            .addObject("form", dto);
    }

    @RequestMapping(value = "/mypage/save", method = RequestMethod.POST)
    @ResponseBody
    public void doSomething(@RequestBody MyWrapperDTO dto) {
        // process your ajax call here
        // do something
    }
}

对于你的jsp:

<spring:url var="myAjaxCall" value="/mypage/save" />

<script type="text/javascript">
   $(document).ready(function() {
      $(document).on('click', '#submit', function() {
          // not the best way to do it
          var data = {
            'someStrValue': $('#someStrValue').val(),
            'anotherStrValue': $('#anotherStrValue').val(),
            'someIntValue': $('#someIntValue').val()
          };

          // use jQuery.ajax
          jQuery.ajax({
              'data': JSON.stringify(data),
              'type': 'POST',
              'ur': '${myAjaxCall}',
              'success': function(data) {
                   // yehey!
               }
          });
      });
    });
</script>

<!-- you should use spring form tag -->
<form:form commandName="form">
   <form:input id="someStrValue" path="someStrValue"></form:input>
   <form:input id="anotherStrValue" path="anotherStrValue"></form:input>
   <form:input id="someIntValue" path="someIntValue"></form:input>
   <button id="submit" type="button">Submit</button>
</form:form>

Spring 将处理转换/绑定到您的包装器对象。第二个选项要求您在类路径中有杰克逊

于 2012-10-09T07:57:06.677 回答