我正在使用 Struts 2 实现
Tag System
。我在数据库中有两个表,Blog
和 Tag
使用具有多对多关系的 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
属性中获得数组,您应该相应地更改代码。
我解决了我的问题。我拆分名称(标签名称,字符串类型),并为每个字符串创建新的标签对象。我只更改 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");
}
祝你好运!