標籤:
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請求