of the java spring mvc ajax

1. You can use servletAPI be implemented ajax

Controller class

@Controller
public class AjaxController {
    @RequestMapping("/ajax.do")
    public String ajax(HttpServletResponse resp) throws IOException{
        resp.getWriter().print("ajax hello");
        return null;
    }
}

Jsp

<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(function(){
    $('#btn').click(function(){
        $.post("ajax.do",function(data){
            alert(data);
        });
    });
});
</script>
</head>
<body>
<button id="btn">异步获取数据信息</button>
</body>

2. Use the components springmvc provide to fulfill ajax

Introducing jackson related packages:

 

Controller processing

@RequestMapping ( " /json.do " ) 
    @ResponseBody // returns the page content into 
    public List <the User> List () { 
        List <the User> List = new new the ArrayList <the User> (); 
        List.add ( new new the User ( 1, "Joe Smith", 22 is )); 
        List.add ( new new the User (2, "John Doe", 32 ));
         return List; 
    }

Profiles

<!-- json配置 -->
     <bean id="stringConverter"  
        class="org.springframework.http.converter.StringHttpMessageConverter">  
        <property name="supportedMediaTypes">  
            <list>  
                <value>text/plain;charset=UTF-8</value>  
            </list>  
        </property>  
    </bean>  
    <bean id="jsonConverter"  
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>  
    <bean  
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
        <property name="messageConverters">  
            <list>  
                <ref bean="stringConverter" />  
                <ref bean="jsonConverter" />  
            </list>  
        </property>  
    </bean>  

Jsp page

<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(function(){
    $('#btn').click(function(){
        $.post("json.do",function(data){
            var html="";
            for(var i=0;i<data.length;i++){
                html+="<tr><td>"+data[i].id+"</td><td>"+data[i].name+"</td><td>"+data[i].age+"</td></tr>"
            }
            $("#content").html(html);
        });
    });
});
</script>
</head>
<body>
<button id = "btn"> asynchronous access to data information </ Button> 
<Table width = "80%" align = left = "Center"> 
    <TR> 
        <TD> ID </ TD> 
        <TD> Name </ TD> 
        < td> Age </ td> 
    </ TR> 
<tbody ID = "Content"> </ tbody> 
</ Table> 
</ body> 
</ HTML>

Configuration optimization 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http:// www.springframework.org/schema/context/spring-context.xsd 
        HTTP: // www.springframework.org/schema/mvc 
        HTTP: // www.springframework.org/schema/mvc/spring-mvc.xsd " > 
    <! - resolver configuration view -> 
    <the bean ID = "the viewResolver" class = "org.springframework.web.servlet.view.UrlBasedViewResolver"> 
        <Property name = "the viewClass" value = "org.springframework.web .servlet.view.JstlView "/> 
           <-! view name is prefixed response -> 
        <Property name =" prefix "value =" / the WEB-INF / JSP / "/> 
        <-! response the view name suffix -> 
        <Property name = "suffix" value =. "jsp"/>
    </bean><mvc:annotation-driven/>
        
    
    <context:component-scan base-package="cn.sxt.controller"/>
</beans>

 github Address: https://github.com/Vincent-yuan/springmvc_ajax

Guess you like

Origin www.cnblogs.com/Vincent-yuan/p/11279971.html