博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用maven快速搭建spring mvc的web项目(配置到jackson 和 任务调度)
阅读量:6993 次
发布时间:2019-06-27

本文共 8760 字,大约阅读时间需要 29 分钟。

hot3.png

pom.xml

4.0.0
ludd
test
war
0.0.1-SNAPSHOT
test Maven Webapp
http://maven.apache.org
4.2.0.RELEASE
junit
junit
3.8.1
test
org.springframework
spring-core
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-expression
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-test
${spring.version}
log4j
log4j
1.2.17
jstl
jstl
1.2
taglibs
standard
1.1.2
javax.servlet
javax.servlet-api
3.1.0
com.fasterxml.jackson.core
jackson-databind
2.6.3
com.fasterxml.jackson.core
jackson-annotations
2.6.3
com.fasterxml.jackson.core
jackson-databind
2.6.3
test

applicationContext.xml

spring-mvc.xml

text/html;charset=UTF-8
application/json;charset=UTF-8

web.xml

test-server
contextClass
org.springframework.web.context.support.XmlWebApplicationContext
contextConfigLocation
classpath:applicationContext.xml
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
/*
DispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
DispatcherServlet
*.do
org.springframework.web.context.ContextLoaderListener
index.jsp
3600

java后台: 接受和返回json

package com.test.controller;import java.beans.PropertyEditorSupport;import java.io.IOException;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import javax.servlet.http.HttpServletRequest;import org.apache.commons.lang.StringUtils;import org.springframework.stereotype.Controller;import org.springframework.web.bind.ServletRequestDataBinder;import org.springframework.web.bind.annotation.InitBinder;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.fasterxml.jackson.core.JsonParseException;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.core.type.TypeReference;import com.fasterxml.jackson.databind.JsonMappingException;import com.fasterxml.jackson.databind.ObjectMapper;import com.test.model.Goods;@Controller()@RequestMapping("/json")public class JsonController {	/**	 * 	 * @return	 */	@RequestMapping("/postJson.do")	@ResponseBody	public Object postJson(@RequestBody List
goods) { Goods goods2 = new Goods(); goods2.setName("好东西"); goods2.setCreateTime(new Date()); goods.add(goods2); System.out.println(goods); return goods; } @RequestMapping("/getJson.do") @ResponseBody public Object getJson(String goodsStr) { ObjectMapper mapper = new ObjectMapper(); //设置日期格式 SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); mapper.setDateFormat(fmt); List
list=null; try { list = mapper.readValue(goodsStr, new TypeReference
>() {}); //json 字符串转 对象 } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println(list.size()); String result=null; try { result = mapper.writeValueAsString(list); //对象 转json 字符串 } catch (JsonProcessingException e) { e.printStackTrace(); } return list; } public static void main(String[] args) { List
goods = new ArrayList
(); Goods goods2 = new Goods(); goods2.setName("好东西"); goods2.setCreateTime(new Date()); goods.add(goods2); String str = "[{\"id\":null,\"name\":\"啊啊\",\"type\":null,\"attr\":null,\"price\":1232,\"desc\":null,\"status\":null,\"createTime\":null}," + "{\"id\":null,\"name\":\"好东西\",\"type\":null,\"attr\":null,\"price\":null,\"desc\":null,\"status\":null," + "\"createTime\":\"2016-07-07 10:32:07\"}]"; String str2 = "{\"id\":null,\"name\":\"啊啊\",\"type\":null,\"attr\":null,\"price\":1232,\"desc\":null,\"status\":null,\"createTime\":null}"; // jackson ObjectMapper mapper = new ObjectMapper(); //设置日期格式 SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); mapper.setDateFormat(fmt); try { System.out.println(mapper.writeValueAsString(goods2)); System.out.println(mapper.writeValueAsString(goods)); mapper.writeValueAsString(goods); Goods good=mapper.readValue(str2,Goods.class); System.out.println(good); List
list = mapper.readValue(str, new TypeReference
>() {}); System.out.println(list.size()); } catch (JsonProcessingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>	bookstrap-table	

Hello World!

name:
price:
创建时间:

 

转载于:https://my.oschina.net/ludd79806329/blog/707615

你可能感兴趣的文章
Ubuntu 查看文件以及磁盘空间大小管理
查看>>
ExtJS与jQuery的一点细节上的对比
查看>>
Struts2源码浅析-初始化
查看>>
nginx安装
查看>>
angularjs 利用filter进行表单查询及分页查询
查看>>
stack
查看>>
SCAU 8588 表达式求值
查看>>
OD使用教程5 - 调试篇05|解密系列
查看>>
kindeditor 操作时同步到textarea
查看>>
修改已经释放了的请求号
查看>>
重写和强制转换再调用能编译但不能运行
查看>>
logging ,re 模块
查看>>
Android入门之GridView(表格控件)
查看>>
JavaScript基础篇
查看>>
Cesium 加载天地图
查看>>
Centos7中安装最新版maven3.5.0
查看>>
python学习之老男孩python全栈第九期_数据库day003 -- 作业
查看>>
深度优先遍历
查看>>
常用类型转换 一.常用int和string类型转换
查看>>
Ext Js简单Grid分页及选择器的使用
查看>>