32、(知识篇)SpringMVC09 SpringMVC自定义转换类/@InitBinder注解

/**

* SpringMVC自定义转换类/@InitBinder注解:

* 自定义转换类方法:

* 1、转换类继承org.springframework.core.convert.converter.Converter<S, T>

* S 需要转换的类型

* T 转换后的类型

* 2、在springmvc.xml种设置org.springframework.context.support.ConversionServiceFactoryBean

*   其中property属性设置 set 为自己定义的转换类

* 3、Controller方法可以直接用实体类参数就可以了

* 实例:testConverter

* @InitBinder 表示的方法

* 可以通过参数WebDataBinder 设置字段不进行表单回显

* (即modelattribute设置的值不进行回显(无效))

* 访问地址:http://127.0.0.1:8080/32SpringMVC09/testConverter?user=1_youname

* @param user

* @return

*/


测试类:

package com.spring;

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;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {
	
	/**
	 * SpringMVC自定义转换类/@InitBinder注解:
	 * 自定义转换类方法:
	 * 1、转换类继承org.springframework.core.convert.converter.Converter<S, T>
	 * S 需要转换的类型
	 * T 转换后的类型
	 * 2、在springmvc.xml种设置org.springframework.context.support.ConversionServiceFactoryBean
	 *   其中property属性设置 set 为自己定义的转换类
	 * 3、Controller方法可以直接用实体类参数就可以了
	 * 
	 * 实例:testConverter
	 * 
	 * @InitBinder 表示的方法
	 * 可以通过参数WebDataBinder 设置字段不进行表单回显
	 * (即modelattribute设置的值不进行回显(无效))
	 * 
	 * 
	 * 访问地址:http://127.0.0.1:8080/32SpringMVC09/testConverter?user=1_youname
	 * @param user
	 * @return
	 */
	
	@ResponseBody
	@RequestMapping("/testConverter")
	public String testConverter(User user){
		System.out.println(user);
		return "OK";
	}
	
	@InitBinder
	public void testInitBuiler(WebDataBinder wdb){
		wdb.setDisallowedFields("userName");
	}
	
}

转换类:

package com.spring;

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

@Component
public class CustomConverter implements Converter<String,User>{

	
	@Override
	public User convert(String params) {
		// TODO Auto-generated method stub
		
		System.out.println("params == "+params);
		String[] values = params.split("_");
		
		User user = new User(Integer.parseInt(values[0]), values[1]);
		return user;
	}

}

user类:

package com.spring;

import org.springframework.stereotype.Component;

@Component
public class User {
	private int id;
	private String userName;
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public User(int id, String userName) {
		super();
		this.id = id;
		this.userName = userName;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", userName=" + userName + "]";
	}
	
	
}

applicationmvc.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:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		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.3.xsd">

	<context:component-scan base-package="com.spring"></context:component-scan>
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<mvc:default-servlet-handler/>
	<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
	
	<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
		<property name="converters">
			<set>
				<ref bean="customConverter"/>
			</set>
		</property>
	</bean>
	
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>32SpringMVC09</display-name>
  <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:applicationmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<filter>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值