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;
} }

spring mvc ajax请求的更多相关文章

  1. spring MVC处理请求过程及配置详解

    本文主要梳理下Spring MVC处理http请求的过程,以及配置servlet及业务application需要的常用标签,及其包含的意义. spring MVC处理请求过程 首先看一个整体图 简单说 ...

  2. spring MVC处理请求过程

    spring MVC处理请求过程 首先看一个整体图 简单说下各步骤: handlerMapping handlerMapping将请求映射到处理器,即图中的HandlerExecutionChain. ...

  3. spring mvc get请求也可以接受DTO对象

    spring mvc get请求也可以接受DTO对象,比如:url上面你还是将参数&符号连接起来,并自动封装进一个DTO对象里. 只有@RequestBody注解spring mvc才会从ht ...

  4. spring mvc ajax 提交复杂数组类型

    The server refused this request because the request entity is in a format not supported by the reque ...

  5. spring mvc ajax异步文件的上传和普通文件上传

    表单提交方式文件上传和ajax异步文件上传 一:首先是我在spring mvc下的表单提交方式上传 ssm的包配置我就不一一详细列出来了,但是上传的包我还是列出来 这一段我也不知道怎么给大家讲解就是直 ...

  6. Spring MVC+ajax进行信息验证

    本文是一个ajax结合Spring MVC使用的入门,首先我们来了解一下什么是Ajax AJAX 不是新的编程语言,而是一种使用现有标准的新方法.AJAX 最大的优点是在不重新加载整个页面的情况下,可 ...

  7. Spring MVC ajax:post/get 的具体实现

    Post 方式 1.自动注入 a. pom.xml ---- 配置Maven,添加必要的jar包 <!--用于 String-JSONObject 转换 --> <dependenc ...

  8. spring mvc get请求中文乱码问题

    使用Spring MVC进行get请求时发现get请求带上中文参数,后台收到的是乱码,即使加了encoding filter也没用. 原因是,encoding filter 是针对post请求的,to ...

  9. Spring MVC 之请求参数和路径变量

    请求参数和路径变量都可以用于发送值给服务器.二者都是URL的一部分.请求参数采用key=value形式,并用“&”分隔. 例如,下面的URL带有一个名为productId的请求参数,其值为3: ...

随机推荐

  1. AIM Tech Round 3 (Div. 2)D. Recover the String(贪心+字符串)

    D. Recover the String time limit per test 1 second memory limit per test 256 megabytes input standar ...

  2. download plugin update site for offline installation

    Reference Eclipse Launcher Running update manager from command line   好多次为window下的eclipse不能拿到linux下直 ...

  3. ural2014 Zhenya moves from parents

    Zhenya moves from parents Time limit: 1.0 secondMemory limit: 64 MB Zhenya moved from his parents’ h ...

  4. PAT (Advanced Level) 1004. Counting Leaves (30)

    简单DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...

  5. Email:2017

    Hi, 2017,我对自己有一个小小的期望:写写文字,安安心. Enjoy a simple life. 如我所愿吧! 明年再来问候你.

  6. office web apps部署(二)

    1.安装composer 参考 http://docs.phpcomposer.com/00-intro.md 根据系统选择安装方式 修改php.ini   去掉extension=php_opens ...

  7. JQuery收集

  8. Is it possible to run native sql with entity framework?

    For .NET Framework version 4 and above: use ObjectContext.ExecuteStoreCommand() if your query return ...

  9. 17.4.3 使用MulticastSocket实现多点广播(2)

    // 让该类实现Runnable接口,该类的实例可作为线程的target public class MulticastSocketTest implements Runnable { // 使用常量作 ...

  10. 绿色版Tomcat 启动 + 停止 + 随系统自动启动 - - 博客频道 - CSDN.NET

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...