2010-11-11 126 views
0

我可以在Spring MVC中使用Ajax嗎?出<mvc:annotation-driven/>? 我是如何將我的控制器映射到app-servlet.xml中? 我已經看到了註釋,但沒有很多例子沒有註釋:(Spring mvc和Ajax

感謝。

回答

0

這裏是一個很好的例子。 http://loianegroner.com/2010/02/spring-mvc-and-ajax-with-json/
除了上述 ...

public ModelAndView getColumnsJson(HttpServletRequest request, 
     HttpServletResponse response) throws Exception { 
     String nvalue = request.getParameter("value"); 
     //do something with nvalue and send back the result.... 
    Map<String,Object> modelMap = new HashMap<String,Object>(2); 
    modelMap.put("result", "DONE"); 
    return new ModelAndView("jsonView", modelMap); 

} 
0

其實我也是一個初學者到春天mvc ..目前正在追求學士學位.. 我也是用spring mvc沒有註釋。突然我意識到我需要ajax來爲我的專業版JECT。我搜查了很多,但無法找到令人滿意的結果。 答案可能看起來很奇怪,但對我而言...... 您可以在攔截器中使用ajax調用。攔截器可以同時引用請求和響應對象。你可以擴展HandlerInterceptorAdapter類..下面是我試過的代碼..希望這樣可以幫助你...

package mypack.Interceptors; 

import java.io.IOException; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; 

public class AjaxInterceptor extends HandlerInterceptorAdapter { 

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws ServletException, IOException{ 

String name=null; 
name = "Hello "+request.getParameter("user"); 
if(request.getParameter("user").toString().equals("")){ 
name="Hello User"; 
} 
response.setContentType("text/plain"); 
response.setCharacterEncoding("UTF-8"); 
response.getWriter().write(name); 

System.out.println("The ajax method is working"); 
return false;}