1

This is the jsp,

<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
var a=12;
$(document).ready(function(){

  $("button").click(function(){
      alert("inside click");
    $.post('test.htm',{inputNumber1: $("#inputNumber1").val()},function(data){
        alert(data);
        });
    alert("returned");
  });
});
</script>
</head>
<body>
<input id="inputNumber1" type="text" size="5">
<button>print</button><br>
</body>
</html>

And this is the controller

package web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class TestController {
    @RequestMapping(value="/test.htm")
    public @ResponseBody Integer add(@RequestParam(value="inputNumber1", required=true) Integer inputNumber1,
            Model model) {
        System.out.println(inputNumber1);
        return inputNumber1;
    }
}

The inputNumber1 is being sent to the controller and it is printed, but it is not being sent back to the javascript, i.e. function(data){ alert(data) };, this function is not called and so is the alert.

I have included these two jar files apart from the usual spring jars,

  1. jackson-mapper-asl-1.5.2.jar

  2. spring-json-1.3.1.jar

Why isn't that function being called? Is there something more that I have to include? Please help, I'm new to spring3 and jquery.

4
  • when you are sending ajax request what response you are getting in firebug? also update to latest version jackson-mapper-asl-1.6.4.jar Commented Apr 27, 2011 at 5:52
  • i'm not using firebug. When I add the jar, jackson-core-asl-1.5.5.jar, I get the error, java.lang.NoClassDefFoundError: Could not initialize class org.codehaus.jackson.map.ObjectMapper Commented Apr 27, 2011 at 5:58
  • i will suggest you to use firebug...it helps in debugging...but for now hard code the value in your controller as return 1 and see if you are getting it..also change the return type to int instead of Integer Commented Apr 27, 2011 at 6:04
  • i'm getting an exception, BeanInstantiationException: Could not instantiate bean class [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.codehaus.jackson.map.ObjectMapper Commented Apr 27, 2011 at 6:07

2 Answers 2

0

try this

public class TestController {
        @RequestMapping(value="/test.htm")
        public @ResponseBody int add(HttpServletRequest request) {
         int num = Integer.valueOf(request.getParameter("inputNumber1"));
            System.out.println(num);
            return num;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

The following jars have to be added to the classpath 1.jackson-core-asl-1.0.0.jar 2.jackson-mapper-asl-1.0.0.jar

and the following should be added to the dispatcher servlet

to enable annotation driven controllers, validation etc...

Comments

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.