插入数据库texfield,在Struts 2中分隔值

时间:2015-03-07 11:31:55

标签: jquery hibernate struts2

我正在使用Tag System实施Struts。我在数据库中有两个表,BlogTag使用Hibernate,有很多关系。我已整合JQuery tagEditor。当我插入单个值时,它没问题,但是当我插入多个值(标签)时,它会像数据库中的单个值一样插入。 Tag table

我没有javascript经验。如何在textfield中分隔值并将此值发送到服务器端以插入数据库?

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值显示在我的数据库中

2 个答案:

答案 0 :(得分:1)

Struts支持将逗号分隔值转换为数组或List。您需要将属性类型更改为其中一种类型。例如,Tag将具有属性

private String[] name;
//getters and setters

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

答案 1 :(得分:0)

我解决了我的问题。我拆分名称(标签名称,字符串类型),我为每个字符串创建新的Tag对象。我只更改了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");

}

祝你好运!