开发者

Spring MVC 2.x with ajax using jquery etc.

I have a scenario of ajax call to spring mvc controller to get the text.

Please provide an example of this. I have to use Spring 2.x not spring 3.x.

like:

Model m=new HashMap();
m.put("textToAjax","Sample");
return new ModelAndView("",m);

And i need to get this text, ie Sample on my jsp text box.

Tha ajax call开发者_StackOverflow中文版 is like

 $.ajax({
        type:"GET",
        url:"test.hmtl?method=getTextName",
                .............
});

Please correct this and help me with full example.enter code here


here's how I currently do and it works:

Map the test.html to a TestController:

/test.html=TestController

Declare TestController in the dispatcher-servlet.xml:

<bean id="TestController" class="com.aeps.planair.fo.controller.TestController"></bean>

Create a simple view file "result.jsp" for displaying ajax response:

${result}

Map this view:

result.(class)=org.springframework.web.servlet.view.JstlView
result.url=/WEB-INF/ajaxview/result.jsp

Develop the TestController:

public class DateController implements Controller {

    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String nowTime = Date().toString();
            ModelAndView modelAndView = new ModelAndView("result");
            modelAndView.addObject("result", nowTime);
            return modelAndView;
    }
}

Now, in your html file, you can call the TestController like this:

<head>
    <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
        function doAjax() {
            $.ajax({
                url : 'test.html',
                success : function(data) {
                    $('#time').html(data);
                }
            });
        }
    </script>
</head>

<body>
    <button id="demo" onclick="doAjax()" title="Button">Get the time!</button>
    <div id="time"></div>
</body>

In this example, the time will be displayed each time you click on the "Get time" button.

Best regards

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜