日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring MVC初始化参数绑定

發布時間:2024/4/14 javascript 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring MVC初始化参数绑定 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

初始化參數綁定與類型轉換很類似,初始化綁定時,主要是參數類型

---單日期

在處理器類中配置綁定方法? 使用@InitBinder注解

在這里首先注冊一個用戶編輯器?參數一為目標類型?? propertyEditor為屬性編輯器,此處我們選用?CustomDateEditor屬性編輯器,

參數一為想轉換的日期格式,參數二表示是否允許為空

package cn.controller;import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.RequestMapping;/*** * @author 景佩佩**/ @Controller public class MyController{//處理器方法@RequestMapping(value="/first.do")public String doFirst(Date birthday,int age) {System.out.println("3333");return "/welcome.jsp";}//自定義一個方法 @InitBinderpublic void initBinder(WebDataBinder binder){DateFormat df=new SimpleDateFormat("yyyy-MM-dd");binder.registerCustomEditor(Date.class, new CustomDateEditor(df, true));}}

?

只需在applicationContext.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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 包掃描器 --><context:component-scan base-package="cn.controller"></context:component-scan></beans>

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><style type="text/css">form{background-color:pink;width:500px;}</style><title></title></head><body>${ex.message } <form action="${pageContext.request.contextPath }/first.do" method="post"><h1>自定義類型轉換器</h1>出生日期:<input name="birthday" value="${birthday }"/>${birthdayerror }<br/><br/>年齡:<input name="age" value="${age }"/>${ageerror }<br/><br/><input type="submit" value="注冊"/></form></body> </html>

welcome.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>歡迎頁面</title></head><body><h1>歡迎訪問${uname }${param.uage }</h1></body> </html>


---多日期格式

配置步驟:

?在處理器類中使用我們自定的屬性編輯器

MyController.java

package cn.controller;import java.util.Date; import org.springframework.stereotype.Controller; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.RequestMapping;@Controller public class MyController{//處理器方法@RequestMapping(value="/first.do")public String doFirst(Date birthday,int age) {System.out.println("3333");return "/welcome.jsp";}//自定義一個方法 @InitBinderpublic void initBinder(WebDataBinder binder){System.out.println("==========");binder.registerCustomEditor(Date.class, new MyDateEditor());}}

?

屬性編輯器

此時我們需要考慮使用哪個屬性編輯器,需要定義自己的屬性編輯器

大致的配置方式如單日期相似,只需要更換屬性編輯即可

自定義的屬性編輯器,需要我們繼承PropertiesEditor,重寫里面的setAsText方法,使用setValue方法賦值

MyDateEditor.java

?

package cn.controller;import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.regex.Pattern;import org.springframework.beans.TypeMismatchException; import org.springframework.beans.propertyeditors.PropertiesEditor; /*** * @author 景佩佩**/ public class MyDateEditor extends PropertiesEditor{/*** source:字符串型的日期*/@Overridepublic void setAsText(String source) throws IllegalArgumentException {//一旦報錯,自動調度到異常處理器SimpleDateFormat sdf=getDateFormat(source);try {Date date = sdf.parse(source);setValue(date);} catch (ParseException e) {e.printStackTrace();}}private SimpleDateFormat getDateFormat(String source){SimpleDateFormat sdf=new SimpleDateFormat();if (Pattern.matches("^\\d{4}-\\d{2}-\\d{2}$", source)) {sdf=new SimpleDateFormat("yyyy-MM-dd");}else if (Pattern.matches("^\\d{4}/\\d{2}/\\d{2}$", source)) {sdf=new SimpleDateFormat("yyyy/MM/dd");}else if (Pattern.matches("^\\d{4}\\d{2}\\d{2}$", source)) {sdf=new SimpleDateFormat("yyyyMMdd");}else {throw new TypeMismatchException("",Date.class);}return sdf;} }

?

轉載于:https://www.cnblogs.com/jingpeipei/p/6259975.html

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的Spring MVC初始化参数绑定的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。