Search code examples
struts2jquery-tageditor

Inserting into database a texfield containing multiple values in Struts 2?


I am implementing a Tag System with Struts 2. I have two tables in database,Blog and Tag using Hibernate with many-to-many relationship. I have integrated JQuery tagEditor.

When I insert a single value it's ok, but when I insert more than one values (Tags), it is inserting like a single value in database.

Tag table

I haven't experience in JavaScript. How to separate values in textfield and send this values to server side to insert in database?

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");


}

I edited my question and BLOB values appear in my database.


Solution

  • I solve my problem.I split name(Tag name,type of String) and i create new Tag object for every string comes.I change only BlogService.java.

    BlogService.java(updated)

    @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");
    
    }
    

    Good Luck!