I have a scenario of ajax call to spring mvc controller to get the text.请提供一个这样的例子。我必须使用Spring2.x,而不是Spring3.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发布于 2012-04-22 10:08:14
下面是我目前的做法,它的工作原理如下:
将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>在本例中,您每次单击"Get time“按钮时都会显示时间。
诚挚的问候
https://stackoverflow.com/questions/7747066
复制相似问题