springmvc框架写到现在终于牵扯到小程序了(所以别说我“不务正业”),对于一个应用程序来说,它的本质其实就是无数个对数据进行增删改查的操作,这里起到至关重要的就是数据,于是这篇帖子的目的就是实现小程序与后台数据的交互。
小程序使用的是wx.request的api来提交和接收数据,最常见的就是接收后台传过来的json数据,并对其进行解析
先看运行结果:
这里总结springmvc框架的三种转json方法
后台 |
前台 |
返回前台的json格式 |
对象/bean/实体类 |
json |
{“id”: 0,”username”: “”,”age”: 0} |
List<实体类> |
json |
[{“id”:1,”username”:”2”,”age”:1},{“id”:2,”username”:”3”,”age”:2}] |
Map<String,实体类> |
json |
{“success”:true,”detail”:[ ] } |
按步骤,从头开始来:
后台
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/ybajax.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.mn</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
导入springmvc所需要的jar包:http://pan.baidu.com/s/1jIK6sb8 导入json所需要的jar包:http://pan.baidu.com/s/1i5bnggP
配置文件 ybajax.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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="action"/>
<!-- 注册适配器 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
注意json需要对应的适配器类
实体类:User.java (实现bean用到)和 Bean.java(实现List、Map用到)
package bean;
public class User {
private int id;
private String username;
private int age;
public User() {}
public User(int id, String username, int age) {
this.id = id;
this.username = username;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package bean;
import java.util.ArrayList;
import java.util.List;
public class Bean {
private List<User> listuser= new ArrayList<User>();
private Bean(){}
public List<User> getListuser() {
return listuser;
}
public void setListuser(List<User> listuser) {
this.listuser = listuser;
}
}
AjaxAction.java
package action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
-
今天小编告诉大家如何对win10系统删除桌面回收站图标进行设置,可能很多用户都不知道
阅读:8315|2022-11-06
|
请发表评论