360SDN.COM

SpringMVC数据转换ConversionService

来源:  2018-09-06 20:15:12    评论:0点击:

来源:https://www.cnblogs.com/tengfeixinxing/p/7012958.html

SpringMVC数据转换ConversionService

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 {

    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>

 

额外的jar包 jstl.jar standard.jar

<%@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>

为您推荐

友情链接 |九搜汽车网 |手机ok生活信息网|ok生活信息网|ok微生活
 Powered by www.360SDN.COM   京ICP备11022651号-4 © 2012-2016 版权