spring mvc 开发笔记

本文介绍如何在Spring MVC中实现日期与字符串类型的自定义绑定,包括全局与局部的WebDataBinder配置方法,并展示了如何将字符串转换为枚举类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.synnex.web.common;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;

public class DateBindingInitializer implements WebBindingInitializer {

	private String format;

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public void initBinder(WebDataBinder binder, WebRequest request) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat, false));
		binder.registerCustomEditor(String.class,new StringTrimmerEditor(false));
                binder.registerCustomEditor(EventStageEnum.class, new EnumPropertyEditor<EventStageEnum>(EventStageEnum.class));
	}
}

 

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
	<property name="webBindingInitializer">
		<bean class="com.synnex.web.common.DateBindingInitializer">
			<property name="format" value="MM/dd/yyyy"></property>
		</bean>
	</property>
</bean>

 

   如果是局部binder,则可以在Controller里这样写:

 

@InitBinder
public void initBinder(WebDataBinder binder) {
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	dateFormat.setLenient(false);
	binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}

 

 

    如果是String类型转Enum,则可以这样做:(String的值必须要与Enum对应,区分大小写的)

 

package com.synnex.web.bind;

import java.beans.PropertyEditorSupport;

public class EnumPropertyEditor<E extends Enum<E>> extends PropertyEditorSupport {

	private Class<E> clazz;

	public EnumPropertyEditor(Class<E> clazz) {
		this.clazz = clazz;
	}

	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		try {
			setValue(Enum.valueOf(clazz, text));
		} catch (Exception ex) {
			setValue(null);
		}
	}

}

 

   网上还发现另一种写法:http://quicker.iteye.com/blog/669061

 

   spring mvc 配置最小化非标签

   

   

<?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"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util" 
	xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="
					http://www.springframework.org/schema/mvc
					http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
					http://www.springframework.org/schema/util
					http://www.springframework.org/schema/util/spring-util-3.2.xsd
					http://www.springframework.org/schema/context
					http://www.springframework.org/schema/context/spring-context-3.2.xsd
					http://www.springframework.org/schema/beans
					http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
					http://www.springframework.org/schema/aop
					http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
					http://www.springframework.org/schema/tx
					http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
					http://www.springframework.org/schema/task
					http://www.springframework.org/schema/task/spring-task-3.2.xsd">

	<context:component-scan base-package="com.hike.wechat.controller" />
	
	
	<bean class="com.hike.common.exception.BizHandlerExceptionResolver">
		<property name="defaultErrorView" value="error/jsonError" />
	</bean>
	
	<!-- 
	<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
	    <property name="order" value="1" />
	    <property name="ignoreAcceptHeader" value="true" />
	    <property name="favorPathExtension" value="true"/>
	    <property name="mediaTypes">
	        <map>
	            <entry key="xml" value="application/xml" />
	            <entry key="json" value="application/json"/>
	        </map>
	    </property>
	    <property name="defaultViews">
	        <list>
	            <bean class="com.alibaba.fastjson.support.spring.FastJsonJsonView"/>
	            <bean class="com.hike.common.extend.MarshallingViewEx">
			        <constructor-arg ref="oxmMarshaller"></constructor-arg>
			    </bean>
	        </list>
	    </property>
	</bean>
	 -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
	
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<property name="messageConverters">
			<util:list id="beanList">
				<ref bean="jsonHttpMessageConverter" />
				<!--  <ref bean="xmlHttpMessageConverter" /> -->
			</util:list>
		</property>
		<property name="webBindingInitializer">
			<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
			    <property name="validator">
			        <bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"></bean>
			    </property>
			    <property name="conversionService">
			        <bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean"></bean>
			    </property>
			</bean>
		</property>
	</bean>

	<bean id="jsonHttpMessageConverter"
			class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter" />
	
	<bean id="xmlHttpMessageConverter"
			class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
		<property name="marshaller" ref="oxmMarshaller" />
		<property name="unmarshaller" ref="oxmMarshaller" />
	</bean>
	
	<bean id="oxmMarshaller" class="com.hike.common.extend.XStreamMarshallerEx"/>
	
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxInMemorySize">
			<value>10485760</value><!--1024*1024*10b=10485760b=10M  -->
		</property>
		<property name="defaultEncoding" value="UTF-8" />
	</bean>
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	  	<property name="order" value="2" />
	  	<property name="prefix" value="/"/>
	  	<property name="suffix" value=".jsp"/>
	</bean>
	
	<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jsp" />
	</bean> -->
</beans>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值