1. 新建一个项目
我们用eclipse新建项目,选择Dynamic Web Project(动态的Web项目)。
点击Next
Project name里面写上 springmvc,这就是我们项目的名称,其他不用改,直接点击Finish 。
OK,项目就建好了。
接下来一定要将项目的字符集改为UTF-8
右键项目——properties
改为UTF-8,点击OK。
2. 编写 web.xml
当我们打开WebContent/WEB-INF目录的时候,发现里面只有一个lib目录,这是存放各种jar包的地方。我们知道一个web项目必须要有一个web.xml文件才行。
既然没有,我们自己写一个咯。
右键WEB-INF——new——file,新建一个web.xml文件。
点击Finish
将以下内容填进去即可。
<?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" > <servlet> <!-- 声明Dispatcher,告诉Web容器我们将使用springMVC的DispatcherServlet --> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- DispatcherServlet在加载时会需要SpringMVC的配置文件,因此需要声明该配置文件的位置。 --> <!-- 如果不配置该项,则应用会默认的去WEB-INF下寻找名字为[servlet-name]-servlet.xml的文件。 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-config.xml</param-value> </init-param> </servlet> <!-- 配置url-pattern,设置为 / 拦截所有的URL请求,并映射到dispatcher --> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
在WebContent->Web-INF下新建springmvc-config.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <!-- 配置自定义扫描的包 --> <context:component-scan base-package="com.controller"></context:component-scan> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"/> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> </mvc:message-converters> </mvc:annotation-driven> <!-- Date日期转换问题的解决 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="stringHttpMessageConverter" /> </list> </property> </bean> <!-- String类型解析器,允许直接返回String类型的消息 --> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter" /> <!-- 配置视图解析器:如何把handler方法返回值解析为实际的物理视图(路径) --> <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property>记住前后都需要加斜杠 <property name="suffix" value=".jsp"></property> </bean> --> </beans>
3.然后在src下新建control层的类
package com.controller; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.dl.common.CSVTool; import com.dl.common.Result; import com.dl.common.ResultUtil; @Controller @RequestMapping(value="/app") public class ReadData { @org.springframework.web.bind.annotation.InitBinder public void InitBinder(WebDataBinder binder){ DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); CustomDateEditor dateEditor = new CustomDateEditor(df, true); binder.registerCustomEditor(Date.class,dateEditor); } @RequestMapping(value = "/getGPSPoints", method = RequestMethod.POST,produces = "application/json;charset=UTF-8") @ResponseBody public Result getGPSPoints(HttpServletRequest request) { try { List<Point> result =null; String csvPath="C:\Users\SWEETGUO\Desktop\毕业设计论文\数据\zyh.csv"; result=CSVTool.loadData(csvPath); return ResultUtil.success(result); } catch (Exception e) { return ResultUtil.error(e.getMessage()); } } }
涉及的相关类:
Result类
package com.dl.common; public class Result<T> { /* 错误码 */ private Integer code; /* 提示信息 */ private String msg; /* 具体的对象*/ private T data; /* 返回对象集合/数组 */ /*private List<T> list;*/ public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public T getData() { return data; } public void setData(T data) { this.data = data; } }
ResultUtil 类:
package com.dl.common; public class ResultUtil { //查询单个实体数据用 public static Result success(Object object) { Result result = new Result(); result.setCode(200); result.setMsg("success"); result.setData(object); System.out.println("success:"+object); return result; } /* //查询单个实体数据用 public static Result success(List<Object> list) { Result result = new Result(); result.setCode(200); result.setMsg("success"); result.setData(object); return result; }*/ //插入、删除、更新数据库用D public static Result success() { return success(null); } //后台抛出异常用 public static Result error(String msg) { Result result = new Result(); result.setCode(500); result.setMsg(msg); System.out.println(msg); return result; } /* public static Result error(Integer code, String msg) { Result result = new Result(); if(code!=0) { result.setCode(code); }else { result.setCode(500); } if(msg!="") { result.setMsg(msg); }else { result.setMsg("fail"); } //result.setData(object); return result; }*/ }
Point 类:
package com.controller; import java.util.Date; import org.springframework.format.annotation.DateTimeFormat; import com.fasterxml.jackson.annotation.JsonFormat; public class Point { private double x; private double y; private double speed; private double direction; @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")//鎺ュ彈鍓嶅彴鐨勬椂闂存牸寮� 浼犲埌鍚庡彴鐨勬牸寮�? @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")//浣滅敤锛氬悗鍙扮殑鏃堕棿 鏍煎紡鍖�? 鍙戦�佸埌鍓嶅彴 private Date date; public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } public double getSpeed() { return speed; } public void setSpeed(double speed) { this.speed = speed; } public double getDirection() { return direction; } public void setDirection(double direction) { this.direction = direction; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } }
目录结构: