博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring MVC基础环境搭建
阅读量:4327 次
发布时间:2019-06-06

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

1、建立一个maven-archetype-webapp项目,pom.xml中添加Maven依赖

org.springframework
spring-webmvc
5.0.2.RELEASE
com.fasterxml.jackson.core
jackson-databind
2.9.3

 

2、在resource文件夹添加一个springmvc-config.xml文件,并设置相关配置 

/WEB-INF/jsp/
.jsp

 

3、在web.xml里面配置Spring MVC的DispatcherServlet跟编码过滤器

Archetype Created Web Application
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc-config.xml
springmvc
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
characterEncodingFilter
/*

到这一步基本的环境配置就可以了,以下的内容为测试环境可不可用使用,如果只是单纯配置环境,下面的步骤可以忽略。 

 

4、创建一个实体类User

package com.app.entity;import java.io.Serializable;import java.util.Date;import org.springframework.format.annotation.DateTimeFormat;import com.fasterxml.jackson.annotation.JsonFormat;@SuppressWarnings("serial")public class User implements Serializable {    private String userName;    @DateTimeFormat(pattern = "yyyy-MM-dd")    private Date birth;    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName;    }    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+08")    public Date getBirth() {        return birth;    }    public void setBirth(Date birth) {        this.birth = birth;    }}

  

5、创建一个UserController类,并添加一个测试方法,功能是将添加的数据返回到客户端显示

package com.app.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.app.entity.User;@Controllerpublic class UserController {    @RequestMapping(value = "/insertUser")    @ResponseBody    public User insertUser(User user) {        return user;    }}

  

6、新建一个html文件,添加如下代码

测试页面
姓名:
生日:

 

7、测试与结果

运行项目进入index.html,输入相关的信息

 

点击“测试”按钮,即可看到结果

  

 

-------------------- 额外内容 --------------------

1、@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+08")注解用于返回到客户端显示时的格式,里面的timezone不设置,返回时会少一天。

2、给自动生成的web.xml添加filter时会报错,将最前的以下内容

替换成

 即可。

 

转载于:https://www.cnblogs.com/QingXiaxu/p/8118024.html

你可能感兴趣的文章
Java与算法之(2) - 快速排序
查看>>
Windows之IOCP
查看>>
机器学习降维之主成分分析
查看>>
CTP2交易所成交回报
查看>>
WebSocket & websockets
查看>>
openssl 升级
查看>>
ASP.NET MVC:通过 FileResult 向 浏览器 发送文件
查看>>
CVE-2010-2883Adobe Reader和Acrobat CoolType.dll栈缓冲区溢出漏洞分析
查看>>
使用正确的姿势跨域
查看>>
AccountManager教程
查看>>
Android学习笔记(十一)——从意图返回结果
查看>>
算法导论笔记(四)算法分析常用符号
查看>>
ultraedit激活
查看>>
总结(6)--- python基础知识点小结(细全)
查看>>
亿级曝光品牌视频的幕后设定
查看>>
ARPA
查看>>
JSP开发模式
查看>>
我的Android进阶之旅------>Android嵌入图像InsetDrawable的使用方法
查看>>
Detours信息泄漏漏洞
查看>>
win32使用拖放文件
查看>>