【问题标题】:Type conversion from string to byte array in Struts 2Struts 2中从字符串到字节数组的类型转换
【发布时间】:2014-11-20 03:01:15
【问题描述】:

我从客户端的用户那里得到一些输入,没有字符限制。所以我将输入存储为 BLOB 数据类型。

我直接使用 getter 和 setter 填充我的实体。

动作类:

public class OperatorNotesAction extends ActionSupport {
    

    private static final long serialVersionUID = 1L;
    
    private OperatorNotesInfo note;
....    
}

OperatorNotesInfo 是我想要填充的实体。在客户端,我使用 JavaScript 将用户填写的输入发送到操作类:

JS:

$.ajax({
    type: 'POST',
    url: "<s:url action='updateNote'/>",
    data:
    {                   
        'note.title':$('#title').val(),
        'note.id.operatorId':$('#operatorId').val(),
        'note.content':$('textarea').val()

    },

这里的内容是动作类中的字节数组类型,因为它存储为 BLOB。

如何将用户输入的输入转换为字节数组,以便保存实体note的内容属性?

【问题讨论】:

  • 你想从字符串转换为字节数组?
  • 你有没有试过用谷歌搜索如何在java中将字符串转换为字节数组?
  • 谷歌搜索或只是 Stackoverflowing :P

标签: javascript java jsp struts2 type-conversion


【解决方案1】:

您需要编写一个自定义类型转换器。默认情况下,输入的字符串转换为String 类型。你可以阅读which types conversion is supported via built-in type conversion。如果要将字符串转换为字节数组,则需要编写转换器。然后将其与您的财产一起使用

@TypeConversion(converter="org.conversion.StringToBytearrayConverter")
public void setContent(byte[] bytes) {
  this.content = bytes;
}

【讨论】:

    猜你喜欢
    • 2016-08-04
    • 2016-05-26
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    相关资源
    最近更新 更多