插入数据库文本字段,分隔值 Struts 2

Insert into database texfield, seperate values in Struts 2

我正在用 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 值出现在我的数据库中

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

private String[] name;
//getters and setters

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

我解决了我的 problem.I 拆分名称(标签名称,字符串类型)并且我为每个字符串创建了新的标签对象 comes.I 仅更改 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");

}

祝你好运!