菜鸟学习Spring——SpringMVC注解版在服务器端获取Json字符串并解析
一、概述。
SpringMVC在服务端把客户端传过来的JSON字符串,并把JSON字符串转成 JSON对象并取得其中的属性值,这个在项目中经常用到。
二、代码演示。
需要添加的jar包。
2.1 web.xml。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<filter>
<filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.spring</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>2.2springMVC-servlet.xml。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <context:component-scan base-package="com.gaowei.JSON" /> <mvc:annotation-driven /> </beans>2.3 JSP文件。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="jquery-1.3.2.js">
</script>
<script type="text/javascript" src="json2.js">
</script>
<script type="text/javascript">
function userinfo(username,password){
this.username=username;
this.password=password;
}
function sendAjax(){
var userinfoRef=new userinfo('高玮','12312');
var jsonStringRef=JSON.stringify(userinfoRef);
$.post("getJSONString.spring?t="+new Date().getTime(),{
jsonString:jsonStringRef
});
}
</script>
</head>
<body>
<input type="button" onclick="sendAjax()" value="登录" >
</body>
</html>2.4GetJSONString.java。
package com.gaowei.JSON; import net.sf.json.JSONObject; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; @Controller
public class GetJSONString { @RequestMapping(value="getJSONString")
public String getJSONString(@RequestParam("jsonString") String jsonString){
JSONObject object=JSONObject.fromObject(jsonString);
System.out.println(object.get("username"));
System.out.println(object.get("password"));
return "test.jsp";
}
}2.5效果图。
三、总结
页面与Contorller层的交互避免要用到JSON传值到后台,这中方法就可以让我们更好的实现界面层与Contorller的交互。
菜鸟学习Spring——SpringMVC注解版在服务器端获取Json字符串并解析的更多相关文章
- 菜鸟学习Spring——SpringMVC注解版解析不同格式的JSON串
一.概述 不同格式的JSON串传到后台来实现功能这个是我们经常要做的一件事,本篇博客就给大家介绍四种不同的JSON串传到后台后台如何用@RequestBody解析这些不同格式的JSON串的. 二.代码 ...
- 菜鸟学习Spring——SpringMVC注解版前台向后台传值的两种方式
一.概述. 在很多企业的开法中常常用到SpringMVC+Spring+Hibernate(mybatis)这样的架构,SpringMVC相当于Struts是页面到Contorller直接的交互的框架 ...
- 菜鸟学习Spring——SpringMVC注解版将URL中的参数转成实体
一.概述 将URL中参数转成实体在我们项目中用的很多比如界面提交表单请求后台的Contorller的时候通过URL传递了一串参数到后台,后台通过Spring让界面的字段与实体的字段映射来实现给后台的实 ...
- 菜鸟学习Spring——SpringMVC注解版控制层重定向到控制层
一.概述. SpringMVC中界面请求Contorller1,Contorller1需要重定向到Contorller2中显示其他页面或者做一些业务逻辑,Spring中提供了这个功能利用"r ...
- android客户端从服务器端获取json数据并解析的实现代码
今天总结一下android客户端从服务器端获取json数据的实现代码,需要的朋友可以参考下 首先客户端从服务器端获取json数据 1.利用HttpUrlConnection /** * 从指定的U ...
- (转)android客户端从服务器端获取json数据并解析的实现代码
今天总结一下android客户端从服务器端获取json数据的实现代码,需要的朋友可以参考下 首先客户端从服务器端获取json数据 1.利用HttpUrlConnection 复制代码 ...
- android客户端从服务器端获取json数据并解析的实现代码(重要)
首先客户端从服务器端获取json数据 1.利用HttpUrlConnection /** * 从指定的URL中获取数组 * @param urlPath * @return * @throws Exc ...
- 菜鸟学习Spring——60s配置XML方法实现简单AOP
一.概述. 上一篇博客讲述了用注解的形式实现AOP现在讲述另外一种AOP实现的方式利用XML来实现AOP. 二.代码演示. 准备工作参照上一篇博客<菜鸟学习Spring--60s使用annota ...
- springMVC(注解版笔记)
springMVC(注解版) 较之于非注解版本,发生一下变化: 1.配置文件需要配置的标签有: <!-- 包的扫描,此包下面的所有包都启用注解 --> <context:compon ...
随机推荐
- 5A - Matrix
#include <iostream> using namespace std; int n, m, q; struct node { int v; // 节点权值 int r; // 右 ...
- idea 激活
激活时选择License server,填入 http://idea.wlphp.com:1017 点击Active即可 2DZ8RPRSBU-eyJsaWNlbnNlSWQiOiIyRFo4UlBS ...
- 【转】idea中maven模块编程灰色
可能是设置中模块的pom.xml文件被忽略了 去掉对勾 转自:https://blog.csdn.net/ethan__xu/article/details/80794060
- Android Activity实例应用(选择QQ头像)
1.效果图 点击button,跳转到页面2 选择需要的头像,自动返回 3.XML文件布局 页面1 <?xml version="1.0" encoding="utf ...
- docker 部署 笔记
Docker虚拟机常用命令 先更新软件包 yum -y update 安装Docker虚拟机 yum install -y docker 运行.重启.关闭Docker虚拟机 service d ...
- 教你搭建SpringMVC框架( 附源码)
一.项目目录结构 二.SpringMVC需要使用的jar包 commons-logging-1.2.jar junit-4.10.jar log4j-api-2.0.2.jar log4j-core- ...
- Subsequence(二分)
A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...
- hdu1711 Number Sequence kmp模板
题目传送门 学习博客 学习了kmp算法,理解了算法思想,但还没有到能把这个思想用语言来描述出来. #include<bits/stdc++.h> #define clr(a,b) mems ...
- 零基础学QT编程
吴迪.2010.1 北京航空航天大学出版社 Qt资源 CSDN QT http://bbs.csdn.net/forums/Qt/ QT编程网 http://www.qtbcw.com/ 编程论坛 ...
- v-model 用在组件中
官方文档: 使用自定义事件的表单输入组件 官方也说明了,v-model只不过是一个语法糖而已,真正的实现靠的还是 1. v-bind : 绑定响应式数据 2. 触发 input 事件 并传递数据 (核 ...