spring mvc ajax請求

來源:互聯網
上載者:User

標籤:

jar包中增加

jackson-annotations-2.5.0.jar

jackson-core-2.5.0.jar

jackson-databind-2.5.0.jar

springmvx.xml

 

<?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:mvc="http://www.springframework.org/schema/mvc"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/mvc     http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd        http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-4.1.xsd        http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-4.1.xsd">    <!-- spring mvc 註解驅動 -->    <mvc:annotation-driven />    <!-- 掃描器 -->    <context:component-scan base-package="com" />    <!-- 配置視圖 解析器 -->    <bean        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <!-- 首碼和尾碼 -->        <property name="prefix" value="/"></property>        <property name="suffix" value=".jsp"></property>    </bean>    <!-- 從請求和響應讀取/編寫字串 -->    <bean id="stringHttpMessage"        class="org.springframework.http.converter.StringHttpMessageConverter">        <property name="supportedMediaTypes">            <list>                <value>text/plain;charset=UTF-8</value>            </list>        </property>    </bean>    <!-- 用於將對象轉換為JSON -->    <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="stringHttpMessage" />                <ref bean="jsonConverter" />            </list>        </property>    </bean></beans>

 

handler

 

package com.stone.controller;import java.io.PrintWriter;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.stone.bean.UserInfo;/** */@Controllerpublic class UserController {    @RequestMapping("/ajaxUser.do")    public void userNumber(String num, HttpServletResponse response) {        System.out.println(num);        try {            // 響應            response.setContentType("text/html");            response.setCharacterEncoding("utf-8");            PrintWriter out = response.getWriter();            if ("stone".equals(num)) {                out.println("對不起,請重新輸入帳號!");            } else {                out.println("恭喜你,帳號可以使用!");            }            out.flush();            out.close();        } catch (Exception e) {            e.printStackTrace();        }    }    @RequestMapping("/ajaxUser2.do")    public @ResponseBody    UserInfo ajaxUser(Integer userId) {        System.out.println("-------bianhao:" + userId);        UserInfo user = new UserInfo();        user.setUserId(userId);        user.setUserName("張三");        user.setUserSex("男");        return user;    }}

 

js

function btn01Click() {    var num = $(‘#userNumber‘).val();    if (num === ‘‘) {        alert(‘請輸入帳號!‘);    } else {        $.post(‘ajaxUser.do‘, {            num : num        }, function(data) {            alert(data);        });    }}function btn02Click() {    var num = $(‘#userId‘).val();    if (num === ‘‘) {        alert(‘請輸入帳號!‘);    } else {        $.post(‘ajaxUser2.do‘, {            userId : num        }, function(data) {            alert(data.userId+‘----‘+data.userName+‘----‘+data.userSex);        }, "json");    }}

java bean

package com.stone.bean;import java.util.Arrays;import java.util.Date;import org.springframework.format.annotation.DateTimeFormat;public class UserInfo {    private Integer userId;    private String userName;    private String userSex;    private String[] userxq;    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")    private Date userTime;    @Override    public String toString() {        return "UserInfo [userId=" + userId + ", userName=" + userName                + ", userSex=" + userSex + ", userxq="                + Arrays.toString(userxq) + ", userTime=" + userTime + "]";    }    public Date getUserTime() {        return userTime;    }    public void setUserTime(Date userTime) {        this.userTime = userTime;    }    public String[] getUserxq() {        return userxq;    }    public void setUserxq(String[] userxq) {        this.userxq = userxq;    }    public Integer getUserId() {        return userId;    }    public void setUserId(Integer userId) {        this.userId = userId;    }    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName;    }    public String getUserSex() {        return userSex;    }    public void setUserSex(String userSex) {        this.userSex = userSex;    }}

 

spring mvc ajax請求

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.