【问题标题】:Insert into database texfield, seperate values in Struts 2插入数据库texfield,在Struts 2中单独的值
【发布时间】:2015-03-07 11:31:54
【问题描述】:

我正在用Struts 实现Tag System。我在数据库中有两个表,BlogTag 使用具有多对多关系的 Hibernate。我已经整合了JQuery tagEditor。当我插入单个值时没关系,但是当我插入多个值(标签)时,它就像在数据库中插入单个值一样。

我没有使用 javascript 的经验。如何分隔文本字段中的值并将此值发送到服务器端以插入数据库?

create.jsp:

    <s:form action="execCreate">
        <div class="form-group">
            <s:label for="title" key="global.title" />
            <s:textfield cssClass="form-control" key="blog.title" 
                name="blog.title" id="title" />
        </div>
        <div>
            <s:textarea  id="wysihtml5-editor" cssStyle="height:400px" name="blog.description"
                key="blog.description" placeholder="Enter Description..."/>
        </div>
        
    <div class="taginput">
            <s:label for="tag" value="Tag"/>
            <s:textfield cssClass="form-control" key="tag.name" cssStyle="height:50px;"
                name="tag.name" id="tag" />
        </div>  
        
    <s:submit type="button" cssClass="btn btn-primary" key="global.submit"/>
</s:form>


<script>
 $('#tag').tagEditor({ 
    autocomplete: {
        
        delay: 0, // show suggestions immediately
        clickDelete:true,
        position: { collision: 'flip' }, // automatic menu position up/down
        placeholder: 'Enter tags ...',
        source: function(request, response) {
            $.ajax({
                url : 'blog/listTag.html',
                type : "POST",
                data : {
                    term : request.term
                },
                dataType : "json",
                success : function(jsonResponse) {
                    response(jsonResponse.tagList);
                    
                }
            });
            },

    },

    }); 

</script>

BlogAction.java:

public String execCreate() {


         try {
        facade.createBlog(blog,tag);
        return "success";
    } catch (Exception e) {
        logger.error(
                Logger.EVENT_FAILURE,
                "could not insert blog values, error: *"
                        + e.getMessage() + "*");
    }

    return "input";

}

BlogService.java:

   @Transactional(readOnly = false)
@Override
public void createBlog(Blog blog,Tag tags) {

 
            Blog newBlog = new Blog();

    User user = (User) ESAPI.authenticator().getCurrentUser();
    
    Tag tag=new Tag();
    String name[]=tags.getName();
for(int i=0; i<name.length; i++){
    tag.setName(tags.getName());
    tag.setDate(new Date());
    em.persist(tag);
    em.flush();
}

    try {
        Set<Tag> listTag = blog.getTag();
        listTag.add(tag);

        newBlog.setTag(listTag);
        newBlog.setTitle(blog.getTitle());
        newBlog.setDescription(blog.getDescription());
        newBlog.setCreated(new Date());
        newBlog.setUser(user);
        em.merge(newBlog);
        em.flush();

    } catch (Exception e) {
        logger.error(Logger.EVENT_FAILURE, e.getMessage());
    }

    logger.info(Logger.SECURITY_SUCCESS, "blog created successfully");


}

我编辑了我的问题,并且 BLOB 值出现在我的数据库中

【问题讨论】:

  • 你有一个具体的问题,你有什么错误?
  • 修改如何将值发送到操作或在操作中解析它们。
  • @RomanC ,我处于这种情况:博客和标签表之间存在多对多关系。当我插入博客属性(标题、描述、标签)时,我想发送单独的标签值到服务器,因为一个博客可能有很多标签。我想我应该从客户端与 javscript 分开并将这些单独的值发送到服务器,但我不清楚。我的“错误”是当标签插入数据库时​​它正在插入作为一个字符串,而不是单独的值。
  • @AleksandrM,你能帮我写代码吗?
  • 用代码做什么?拆分字符串?

标签: jquery hibernate jsp struts2 jquery-tageditor


【解决方案1】:

Struts 支持将逗号分隔值转换为数组或List。您需要将您的属性类型更改为这些类型之一。例如Tag 会有一个属性

private String[] name;
//getters and setters

name 属性中获得数组后,您应该相应地更改代码。

【讨论】:

  • 我更改了名称类型,它在数据库中显示了我无法理解的 Blob 值。例如:我插入(java,struts,hiberante)标签名称,在数据库中它正在插入 BLOB 值,可以当您说:“您应该相应地更改代码。”时,您可以帮助我。我应该在代码中做哪些更改。谢谢!
  • 它应该是字符串类型,因为你知道如何保存单个值你可以继续数组迭代,这部分代码应该做相应的处理。
  • 是的,我有你告诉我的字符串类型。你的意思是我应该在 BlogService.java 中迭代这个数组并保留这些值?为什么 BLOB 它在数据库中显示为名称值,而不是标签名称?谢谢你,对不起我的重复问题。
  • 不应该是 BLOB,而是 VARCHAR2
  • 我编辑了我的问题。我更改了数据库照片和 BlogService.java,它的 BLOB 值出现在我的数据库中。你能编辑我的问题吗?谢谢!
【解决方案2】:

我解决了我的问题。我拆分名称(标签名称,字符串类型)并为每个字符串创建新标签对象。我只更改 BlogService.java。

BlogService.java(更新)

@Transactional(readOnly = false)
@Override
public void createBlog(Blog blog, Tag tags) {

    logger.info(Logger.EVENT_SUCCESS, "Trying to add a throwException: "
            + throwException);

    Blog newBlog = new Blog();


    Set<Tag> listTag = blog.getTag();

    String name = tags.getName();
    ArrayList aList = new ArrayList(Arrays.asList(name.split(",")));

    for (int i = 0; i < aList.size(); i++) {
        Tag tag = new Tag();
        tag.setName((String) aList.get(i));
        tag.setDate(new Date());
        listTag.add(tag);
        em.persist(tag);
        em.flush();

    }

    try {

        newBlog.setTag(listTag);
        newBlog.setTitle(blog.getTitle());
        newBlog.setDescription(blog.getDescription());
        newBlog.setCreated(new Date());
        newBlog.setUser(user);

        Set<Blog> listBlog = tags.getBlog();
        listBlog.add(newBlog);

        em.persist(newBlog);
        em.flush();

    } catch (Exception e) {
        logger.error(Logger.EVENT_FAILURE, e.getMessage());
    }

    logger.info(Logger.SECURITY_SUCCESS,
            "Blog updated successfully");

}

祝你好运!

【讨论】:

    猜你喜欢
    • 2013-05-28
    • 2013-09-20
    • 2013-04-17
    • 2016-01-01
    • 2014-06-28
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 2015-04-10
    相关资源
    最近更新 更多