spring mvc ajax请求

时间:2023-03-08 16:24:40

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; /**
*/
@Controller
public 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;
} }