spring mvc ajax请求
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请求的更多相关文章
- spring MVC处理请求过程及配置详解
本文主要梳理下Spring MVC处理http请求的过程,以及配置servlet及业务application需要的常用标签,及其包含的意义. spring MVC处理请求过程 首先看一个整体图 简单说 ...
- spring MVC处理请求过程
spring MVC处理请求过程 首先看一个整体图 简单说下各步骤: handlerMapping handlerMapping将请求映射到处理器,即图中的HandlerExecutionChain. ...
- spring mvc get请求也可以接受DTO对象
spring mvc get请求也可以接受DTO对象,比如:url上面你还是将参数&符号连接起来,并自动封装进一个DTO对象里. 只有@RequestBody注解spring mvc才会从ht ...
- spring mvc ajax 提交复杂数组类型
The server refused this request because the request entity is in a format not supported by the reque ...
- spring mvc ajax异步文件的上传和普通文件上传
表单提交方式文件上传和ajax异步文件上传 一:首先是我在spring mvc下的表单提交方式上传 ssm的包配置我就不一一详细列出来了,但是上传的包我还是列出来 这一段我也不知道怎么给大家讲解就是直 ...
- Spring MVC+ajax进行信息验证
本文是一个ajax结合Spring MVC使用的入门,首先我们来了解一下什么是Ajax AJAX 不是新的编程语言,而是一种使用现有标准的新方法.AJAX 最大的优点是在不重新加载整个页面的情况下,可 ...
- Spring MVC ajax:post/get 的具体实现
Post 方式 1.自动注入 a. pom.xml ---- 配置Maven,添加必要的jar包 <!--用于 String-JSONObject 转换 --> <dependenc ...
- spring mvc get请求中文乱码问题
使用Spring MVC进行get请求时发现get请求带上中文参数,后台收到的是乱码,即使加了encoding filter也没用. 原因是,encoding filter 是针对post请求的,to ...
- Spring MVC 之请求参数和路径变量
请求参数和路径变量都可以用于发送值给服务器.二者都是URL的一部分.请求参数采用key=value形式,并用“&”分隔. 例如,下面的URL带有一个名为productId的请求参数,其值为3: ...
随机推荐
- P3P解决cookie存取的跨域问题
最近在做一个流量统计的东西的时候,偶然发现IE在对iframe里面的页面写Cookie的时候有一些安全限制,导致读取Cookie不成功,找了好长时间的解决办法,重要找到如下的办法: 1.页面里的COO ...
- oracle监听无法启动
昨天再监听里新加了个地址,重启电脑后监听无法启动,删除新加地址就好了 # listener.ora Network Configuration File: d:\oracle\product\10.2 ...
- POJ 2482 Stars in Your Window
线段树+离散化+扫描线 AC之后,又认真读了一遍题目,好文章. #include<cstdio> #include<map> #include<algorithm> ...
- [数据结构]Treap简介
[写在前面的话] 如果想学Treap,请先了解BST和BST的旋转 二叉搜索树(BST)(百度百科):[here] 英文好的读者可以戳这里(维基百科) 自己的博客:关于旋转(很水,顶多就算是了解怎么旋 ...
- JS基础知识——缓动动画
基于距离的缓动动画 原理:设定起始位置 start 和终止位置 end,变化会越来越慢. 公式:start=start+(end-start)/10; 这个10不是固定的,想分成多少份就分成 ...
- a标签的target的四个值
特殊的目标 有 4 个保留的目标名称用作特殊的文档重定向操作: _blank 浏览器总在一个新打开.未命名的窗口中载入目标文档. _self 这个目标的值对所有没有指定目标的 <a> 标签 ...
- jsp显示计算数值, 四舍五入
<script>document.write(Math.round(<%= rs_MFM.getInt("PVRCompl") %>/<%= rs_M ...
- Ubuntu和win10双系统Grup无法引导解决方案
通常我们经常安装双系统, 但是有时候安装完系统无法正常引导, 以下就说明Ubuntu和win10双系统, win10在grub界面不断循环的解决方案 直接在win10启动项目上按e进入编辑模式 在文档 ...
- 日志文件 统计 网站PV IP
1. 安装rrdtool yum install rrdtool 2. 创建 rrdtool 数据库 rrdtool create /opt/local/rrdtool/jicki.rrd -s 30 ...
- 产品需求文档写作方法(三)用例文档(UML用例图、流程图)
在产品和技术领域里都有UML的技能知识,而对于产品人员的UML则更多的是指用例图,也就是我所称呼的用户流程图.在讲PRD文档写作的第二篇文章里,我提到了用户流程图的制作,实际上用户流程图是我在产品规则 ...