asp.netmvc2.0中如何将datatable对象传到view的前台然后获取datatable中表字段得值

asp.netmvc2.0中如何将datatable对象传到view的前台然后获取datatable中表字段得值,第1张

在Controller中将datatable对象赋值给ViewData["MyTable"]=mydatatable,在前台使用强制类型转换就行了<% DataTable dt = (DataTable)ViewData["MyTable"]; 后面你的代码%>

一、使用注解式控制器注册PropertyEditor(针对具体的controller类处理)

1、使用WebDataBinder进行控制器级别的注册PropertyEditor(控制器独享)

Java代码

@InitBinder

// 此处的参数也可以是ServletRequestDataBinder类型

public void initBinder(WebDataBinder binder) throws Exception {

// 注册自定义的属性编辑器

// 1、日期

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//CustomDateEditor类是系统内部自带的类

CustomDateEditor dateEditor = new CustomDateEditor(df, true);

// 表示如果命令对象有Date类型的属性,将使用该属性编辑器进行类型转换

binderregisterCustomEditor(Dateclass, dateEditor);

// 自定义的电话号码编辑器(和4161、数据类型转换一样)

binderregisterCustomEditor(PhoneNumberModelclass, new PhoneNumberEditor());

}

备注:转换对象必须要实现PropertyEditor接口,例如CustomDateEditor类

Java代码

package orgspringframeworkbeanspropertyeditors;

import javabeansPropertyEditorSupport;

import javatextDateFormat;

import javatextParseException;

import javautilDate;

import orgspringframeworkutilStringUtils;

public class CustomDateEditor extends PropertyEditorSupport {

private final DateFormat dateFormat;

private final boolean allowEmpty;

private final int exactDateLength;

public CustomDateEditor(DateFormat dateFormat, boolean allowEmpty) {

thisdateFormat = dateFormat;

thisallowEmpty = allowEmpty;

exactDateLength = -1;

}

public CustomDateEditor(DateFormat dateFormat, boolean allowEmpty, int exactDateLength) {

thisdateFormat = dateFormat;

thisallowEmpty = allowEmpty;

thisexactDateLength = exactDateLength;

}

public void setAsText(String text) throws IllegalArgumentException {

if (allowEmpty && !StringUtilshasText(text)) {

setValue(null);

} else {

if (text != null && exactDateLength >= 0 && textlength() != exactDateLength)

throw new IllegalArgumentException((new StringBuilder("Could not parse date: it is not exactly"))append(exactDateLength)append("characters long")toString());

try {

setValue(dateFormatparse(text));

} catch (ParseException ex) {

throw new IllegalArgumentException((new StringBuilder("Could not parse date: "))append(exgetMessage())toString(), ex);

}

}

}

public String getAsText() {

Date value = (Date) getValue();

return value == null "" : dateFormatformat(value);

}

}

2、使用xml配置实现类型转换(系统全局转换器)

(1、注册ConversionService实现和自定义的类型转换器

Xml代码

<!-- ①注册ConversionService -->

<bean id="conversionService" class="orgspringframeworkformatsupportFormattingConversionServiceFactoryBean">

<property name="converters">

<list>

<bean class="hbbaseconvertDateConverter">

<constructor-arg value="yyyy-MM-dd"/>

</bean>

</list>

</property>

<!-- 格式化显示的配置

<property name="formatters">

<list>

<bean class="hbbaseconvertDateFormatter">

<constructor-arg value="yyyy-MM-dd"/>

</bean>

</list>

</property>

-->

</bean>

(2、使用 ConfigurableWebBindingInitializer 注册conversionService

Xml代码

<!-- ②使用 ConfigurableWebBindingInitializer 注册conversionService -->

<bean id="webBindingInitializer"

class="orgspringframeworkwebbindsupportConfigurableWebBindingInitializer">

<property name="conversionService" ref="conversionService" />

<property name="validator" ref="validator" />

</bean>

(3、注册ConfigurableWebBindingInitializer 到RequestMappingHandlerAdapter

Xml代码

<!--Spring31开始的注解 HandlerAdapter -->

<bean

class="orgspringframeworkwebservletmvcmethodannotationRequestMappingHandlerAdapter">

<!--线程安全的访问session -->

<property name="synchronizeOnSession" value="true" />

<property name="webBindingInitializer" ref="webBindingInitializer"/>

</bean>

此时可能有人会问,如果我同时使用 PropertyEditor 和 ConversionService,执行顺序是什么呢?内部首先查找PropertyEditor 进行类型转换,如果没有找到相应的 PropertyEditor 再通过 ConversionService进行转换。

pringMVC默认支持的绑定类型有:

>

以上就是关于asp.net mvc2.0中如何将datatable对象传到view的前台然后获取datatable中表字段得值全部的内容,包括:asp.net mvc2.0中如何将datatable对象传到view的前台然后获取datatable中表字段得值、spring mvc 怎样将获取到的string转成方法的参数类型、8,springmvc默认支持的参数类型有哪些分别有什么作用等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10195538.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-06
下一篇2023-05-06

发表评论

登录后才能评论

评论列表(0条)

    保存