SSM-SpringMVC-27:SpringMVC类型转换之日期类型初步
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
本案例是上面的异常和日期类型转换结合的一个小小的Demo
案例开始
1.自定义处理器和处理方法:
package cn.dawn.day19typeconverter; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest;
import java.util.Date; /**
* Created by Dawn on 2018/3/28.
*/
@Controller
public class TypeConverterController { /*作用于这俩个*/
@ExceptionHandler
public ModelAndView resolveException(Exception ex, HttpServletRequest request) {
ModelAndView modelAndView=new ModelAndView();
/*给username回显*/
modelAndView.addObject("username",request.getParameter("username"));
/*返回的异常对象*/
modelAndView.addObject("ex",ex);
/*发生异常返回登陆页*/
modelAndView.setViewName("login"); return modelAndView;
} /*做日期类型*/
@RequestMapping("/dateconverter1")
public String dateconverter1(String username, Integer userage, Date birthday) throws Exception {
System.out.println(username);
System.out.println(userage);
System.out.println(birthday);
return "success";
}
}
2.自己的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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!--包扫描器-->
<context:component-scan base-package="cn.dawn.day19typeconverter"></context:component-scan>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/day19/"></property>
<property name="suffix" value=".jsp"></property>
</bean> </beans>
3.修改web.xml中央调度器的上下文配置位置为上面那个xml
4.jsp页面:
4.1login.jsp
<%@ page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>登录</h2>
<form action="${pageContext.request.contextPath}/dateconverter1" method="post"> 用户名:<input name="username" value="${username}">
年龄:<input name="userage">
生日:<input name="birthday"> <input type="submit" value="登录"/>
</form>
</body>
</html>
4.2success.jsp
<%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %>
<html>
<body>
<%--<img src="data:image/1.jpg">--%>
<h2>Success!</h2>
</body>
</html>
结论:什么时候跳到成功页面?就是日期格式不出错,日期格式必须为yyyy/MM/dd,SpringMVC默认的自动转换日期格式必须传入的是这个
下篇博客就做自定义类型转换器
SSM-SpringMVC-27:SpringMVC类型转换之日期类型初步的更多相关文章
- SpringMVC 自定义参数绑定实现日期类型绑定
package cn.itcast.ssm.controller.converter; import java.text.ParseException; import java.text.Simple ...
- JavaScript 把字符串类型转换成日期类型
今天在写习题时,遇到些小问题,在这里把答案分享给大家,希望能帮助到大家! 一.把字符串转换成日期类型 var str = "1997-3-12"; var d = new Date ...
- springmvc使用spring自带日期类型验证
控制器 @Controller public class MyController { // 处理器方法 @RequestMapping(value = "/first.do") ...
- SSM-SpringMVC-28:SpringMVC类型转换之自定义日期类型转换器
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 例子很简易,要明白的是思路,话不多说,开讲 上篇博客不是说springmvc默认的日期转换格式是yyyy/M ...
- SpringMVC提交数据遭遇基础类型和日期类型报400错误解决方法
使用SpringMVC开发的时候,页面如果有日期格式的数据,后台接受也是java.util.Date,则报告400错误 .下面是解决方案的演示示例: 这个是实体类,里面createDate就是java ...
- SpringMVC由浅入深day01_12.4 pojo绑定_12.5自定义参数绑定实现日期类型绑定_12.6集合类
12.4 pojo绑定 页面中input的name和controller的pojo形参中的属性名称一致,将页面中数据绑定到pojo. 页面定义: controller的pojo形参的定义: 打断点测试 ...
- springMvc把client传过来一个String类型,转换为日期类型为例
springMvc--接受日期类型参数处理 目录 步骤 2.自定义类型转换规则 3.注册自定义的类型转换类 4.地址栏访问 这个问题,也即是springMvc如何进行参数类型的转换 , 以把cli ...
- SSM框架之SpringMVC(2)参数绑定及自定义类型转换
SpringMVC(2)参数绑定及自定义类型转换 1.请求参数的绑定 1.1. 请求参数的绑定说明 1.1.1.绑定机制 表单提交的数据都是k=v格式的 username=haha&passw ...
- SpringMVC 06: 日期类型的变量的注入和显示
日期处理和日期显示 日期处理 此时SpringMVC的项目配置和SpringMVC博客集中(指SpringMVC 02)配置相同 日期处理分为单个日期处理和类中全局日期处理 单个日期处理: 使用@Da ...
随机推荐
- Linux用户配置文件(第二版)
/etc/passwd文件剖析 文件格式: root:x:0:0:root:/root:/bin/bash 用户名:密码位:UID:GID[缺省组ID]:注释性的描述信息:宿主目录:shell[7部分 ...
- 《java第一季之入门篇》的想法
学习java也有一段时间了,但是考虑到自己现在上课.复习.考试等耗费很多时间,感觉没有静下心来的时间去写一个长期的博客.计划今年7月1号开始写一套关于java的入门篇博客文章,入门篇计划这样--涵盖j ...
- Workflow Notification Mailer Setup
Workflow notification mailer setup in R12 is similar to 11i ( In both release 11i (OWF.H and higher ...
- 如何在Android上编写高效的Java代码
转自:http://www.ituring.com.cn/article/177180 作者/ Erik Hellman Factor10咨询公司资深移动开发顾问,曾任索尼公司Android团队首席架 ...
- linux命令大全(自己慢慢看)
http://blog.zol.com.cn/874/article_873769.html rm -rf mydir /* 删除mydir目录 */ cd mydir /* 进入mydir目录 */ ...
- 【Android 应用开发】BluetoothDevice详解
一. BluetoothDevice简介 1. 继承关系 public static Class BluetoothDevice extends Object implement Parcelable ...
- 容器(list集合)
--为什么使用集合而不使用数组?why ·集合和数组相似点:都可以存储多个对象,对外作为一个整体存在: ··数组的缺点:1.长度必须在初始化时指定,且固定不变: 2.数组采用连续存储空间,删除和添加元 ...
- 面向对象(this的问题二)
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...
- 前端技术之_CSS详解第四天
前端技术之_CSS详解第四天 一.第三天的小总结 盒模型box model,什么是盒子? 所有的标签都是盒子.无论是div.span.a都是盒子.图片.表单元素一律看做文本. 盒模型有哪些组成: wi ...
- htmldom操作添加标签顺序
<!DOCTYPE html> <html> <body> <div id="div1"> </div> <scr ...