JSF Bean 在特定方法调用后不会 运行 代码
JSF Bean doens´t run code after specific method call
我有一个 commandButton
,它在支持 bean 中调用一个方法。这个 bean 是 ViewScoped 并调用一个服务方法,即 ApplicationScoped(在里面它调用存储库方法,该方法将表单中的数据保存在数据库中).
现在服务和存储库方法 运行 正常,但是这些调用之后的代码不是 运行(例如日志和重定向)。
这是支持 bean:
@Named
@ViewScoped
public class SubmitController implements Serializable {
private static final Logger LOG = LoggerFactory.getLogger(SubmitController.class);
private String userName;
private String title;
private String url;
private String text;
@Inject
PostService postService;
@PostConstruct
void init() {
LOG.info("Initialize SubmitController");
}
public void submitPost() throws IOException {
long postID = this.postService.submitPost(this.userName, this.title, this.url, this.text);
LOG.info("postID: {}", postID);
FacesContext.getCurrentInstance()
.getExternalContext()
.redirect("item.xhtml");
}
}
服务:
@Service
public class PostService {
private static final Logger LOG = LoggerFactory.getLogger(PostService.class);
@Inject
PostRepository postRepository;
public long submitPost(final String userName, final String title, final String url, final String text) throws IOException {
Post post = Post.newPost()
.withUser(new User(userName))
.withTitle(new Title(title))
.withUrl(new Url(url))
.withText(new Text(text))
.build();
LOG.info("Submit post: {}", post.toString());
this.postRepository.submitPost(post);
Post refrence = this.postRepository.getRefrence(post);
LOG.info("Refrence: {}", refrence.toString());
return refrence.getId();
}
}
和存储库:
@Repository
public class PostRepository {
private static final Logger LOG = LoggerFactory.getLogger(PostRepository.class);
@PersistenceContext(unitName = "default")
EntityManager entityManager;
public Post getRefrence(final Post post) {
return this.entityManager.getReference(Post.class, post);
}
@Transactional
public void submitPost(final Post post) {
LOG.info("Save post");
this.entityManager.persist(post);
}
}
请注意,存储库和服务注释是 ApplicationScoped。
它与作用域有关吗?为什么 long postID = this.postService.submitPost(this.userName, this.title, this.url, this.text);
运行 之后的代码不是?
我没有在存储库方法中添加 @Transactional
注释。这解决了我的问题
我有一个 commandButton
,它在支持 bean 中调用一个方法。这个 bean 是 ViewScoped 并调用一个服务方法,即 ApplicationScoped(在里面它调用存储库方法,该方法将表单中的数据保存在数据库中).
现在服务和存储库方法 运行 正常,但是这些调用之后的代码不是 运行(例如日志和重定向)。
这是支持 bean:
@Named
@ViewScoped
public class SubmitController implements Serializable {
private static final Logger LOG = LoggerFactory.getLogger(SubmitController.class);
private String userName;
private String title;
private String url;
private String text;
@Inject
PostService postService;
@PostConstruct
void init() {
LOG.info("Initialize SubmitController");
}
public void submitPost() throws IOException {
long postID = this.postService.submitPost(this.userName, this.title, this.url, this.text);
LOG.info("postID: {}", postID);
FacesContext.getCurrentInstance()
.getExternalContext()
.redirect("item.xhtml");
}
}
服务:
@Service
public class PostService {
private static final Logger LOG = LoggerFactory.getLogger(PostService.class);
@Inject
PostRepository postRepository;
public long submitPost(final String userName, final String title, final String url, final String text) throws IOException {
Post post = Post.newPost()
.withUser(new User(userName))
.withTitle(new Title(title))
.withUrl(new Url(url))
.withText(new Text(text))
.build();
LOG.info("Submit post: {}", post.toString());
this.postRepository.submitPost(post);
Post refrence = this.postRepository.getRefrence(post);
LOG.info("Refrence: {}", refrence.toString());
return refrence.getId();
}
}
和存储库:
@Repository
public class PostRepository {
private static final Logger LOG = LoggerFactory.getLogger(PostRepository.class);
@PersistenceContext(unitName = "default")
EntityManager entityManager;
public Post getRefrence(final Post post) {
return this.entityManager.getReference(Post.class, post);
}
@Transactional
public void submitPost(final Post post) {
LOG.info("Save post");
this.entityManager.persist(post);
}
}
请注意,存储库和服务注释是 ApplicationScoped。
它与作用域有关吗?为什么 long postID = this.postService.submitPost(this.userName, this.title, this.url, this.text);
运行 之后的代码不是?
我没有在存储库方法中添加 @Transactional
注释。这解决了我的问题