码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC数据转换ConversionService

时间:2017-06-15 10:23:05      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:转换   source   string   方法   logs   converter   new   servlet   scope   

package com.controller;

import java.io.UnsupportedEncodingException;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.domain.User;

@Controller
public class UserController {

    @RequestMapping("/{loginForm}")
    public String loginForm(@PathVariable String loginForm)
    {
          return loginForm;
    }
    
    @RequestMapping(value="/register",method=RequestMethod.POST)
    public String register(@ModelAttribute User user,Model model) throws UnsupportedEncodingException
    {
        String name = new String(user.getLoginname().getBytes("ISO8859_1"),"UTF-8");
        user.setLoginname(name);
        model.addAttribute("user", user);
        return "success";
    }
    
}

 

package com.converter;

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

import org.springframework.core.convert.converter.Converter;

public class StringToDateConverter implements Converter<String, Date> {

    private String datePattern;
    
    public void setDatePattern(String datePattern)
    {
        this.datePattern = datePattern;
    }
    
    @Override
    public Date convert(String arg0) {
        // TODO Auto-generated method stub
        SimpleDateFormat sf = new SimpleDateFormat(this.datePattern);
        try {
            Date d = sf.parse(arg0);
            return d;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}
package com.domain;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {
    private String loginname;
    private Date shengri;
    public User()
    {
        
    }
    
    public String getLoginname() {
        return loginname;
    }

    public void setLoginname(String loginname) {
        this.loginname = loginname;
    }

    public Date getShengri() {
        return shengri;
    }

    public void setShengri(Date shengri) {
        this.shengri = shengri;
    }
    
}
<?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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
        
        
         <!-- spring可以自动去扫描base-pack下边的包或者子包下的java类,如果有扫描到spring相关的注解类,则把这些类注册为spring的bean -->
        <context:component-scan base-package="com.controller"></context:component-scan>
        
         <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
        <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
            <property name="converters">
                <list>
                    <!-- 自定义转换器 -->
                    <bean  class="com.converter.StringToDateConverter" >
                        <property name="datePattern" value="yyyy-MM-dd"></property> 
                    </bean>
                </list>
            </property>
        </bean>
        <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix">
                <value>/WEB-INF/content/</value>
            </property>
            <property name="suffix">
                <value>.jsp</value>
            </property>
        </bean>
        
</beans>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>  

<body>
    登录名:${ requestScope.user.loginname }<br>
    生日:<fmt:formatDate value="${requestScope.user.shengri}" pattern="yyyy年MM月dd日"/>
  </body>

额外的jar包

技术分享

技术分享

技术分享

SpringMVC数据转换ConversionService

标签:转换   source   string   方法   logs   converter   new   servlet   scope   

原文地址:http://www.cnblogs.com/tengfeixinxing/p/7012958.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!