Spring MVC 2.x 与 ajax 使用 jquery 等。

发布于 2024-12-09 16:26:48 字数 524 浏览 0 评论 0原文

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

请提供一个例子。 我必须使用 Spring 2.x 而不是 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 is like

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

Please correct this and help me with full example.enter code here
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 is like

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

优雅的叶子 2024-12-16 16:26:48

这是我目前的做法和工作原理:

将 test.html 映射到 TestController:

/test.html=TestController

在dispatcher-servlet.xml 中声明 TestController:

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

创建一个简单的视图文件“result.jsp”用于显示 ajax 响应:

${result}

映射此视图:

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

开发 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;
    }
}

现在,在您的 html 文件中,您可以像这样调用 TestController:

<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>

在本例中,每次单击“获取时间”按钮时都会显示时间。

此致

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文