当前位置:首页 > 开发 > 编程语言 > Spring > 正文

SpringMvc

发表于: 2014-09-23   作者:120153216   来源:转载   浏览:
摘要:     Spring配置文件不扫描Controller <context:component-scan base-package="com.test"> <context:exclude-filter type="annotation" expression="org.springframework

 

 

Spring配置文件不扫描Controller
<context:component-scan base-package="com.test">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

SpringMVC配置文件不扫描Service,不然事务不起作用
<?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-3.0.xsd
            http://www.springframework.org/schema/mvc
           	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
           	http://www.springframework.org/schema/context 
	   		http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	
	<!-- Controller层扫描 -->
	<context:component-scan base-package="com.test">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
	</context:component-scan>

	<!-- 请求处理映射器 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
	<!-- 视图映射转换器 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="stringConverter"/>
				<ref bean="jsonConverter"/>
			</list>
		</property>
		<!-- SpringMVC 前台日期STRING类型转换成DATE类型 -->
		<property name="webBindingInitializer">
			<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
				<property name="conversionService">
					<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean"/>
				</property>
			</bean>
		</property>
	</bean>
	<!-- JSON格式转换 -->
	<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
		<property name="supportedMediaTypes" value="application/json;charset=UTF-8"/>
	</bean>
	<!-- String格式转换 -->
	<bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>text/html;charset=UTF-8</value>
				<value>text/plain;charset=UTF-8</value>
			</list>
		</property>
	</bean>
	
	<!-- 视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
		<property name="prefix" value="/WEB-INF/page/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- 文件上传 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize" value="10000000"></property>	
	</bean>
</beans>

 

时间格式化:

        // 创建时间
	@DateTimeFormat(pattern="yyyy-MM-dd")
	private Date createDate;

	// 更新时间
	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
	private Date updateDate;

        @JsonSerialize(using = JsonDateSerializer.class )
	public Date getCreateDate() {
		return createDate;
	}
	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

        @JsonSerialize(using = JsonDateTimeSerializer.class )
	public Date getUpdateDate() {
		return updateDate;
	}
	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}

 

converter:

package com.test.converter;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
import org.springframework.stereotype.Component;

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

@Component
public class JsonDateSerializer extends JsonSerializer<Date> {
    @Override
    public void serialize( Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider ) throws IOException, JsonProcessingException {
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
        String str = sdf.format( date );
        jsonGenerator.writeString( str );
    }
}

package com.test.converter;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
import org.springframework.stereotype.Component;

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

@Component
public class JsonDateTimeSerializer extends JsonSerializer<Date> {
    @Override
    public void serialize( Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider ) throws IOException, JsonProcessingException {
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
        String str = sdf.format( date );
        jsonGenerator.writeString( str );
    }
}

 

SpringMvc

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

推荐文章
编辑推荐
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号