[英]redirect from jsf?
我正在為我的大學項目使用jsp,jstl和jsf進行應用程序,據說,我對jsf也很新。
到目前為止,一切都很順利。 但是,我似乎有一個問題,想知道如何從托管bean重定向到dinamyc參數的頁面。 例如article.jsp?article_id=2
有人能告訴我它是如何完成的嗎?
我一直想嘗試使用像
FacesContext.getCurrentInstance().getExternalContext().dispatch("faces/article.jsp2?article_id=" + articleId);
但得到錯誤:
javax.servlet.ServletException: #{postComment.postClick}: javax.faces.FacesException: javax.servlet.ServletException: javax.faces.component.UIViewRoot cannot be cast to com.sun.faces.application.StateManagerImpl$TreeNode
javax.faces.webapp.FacesServlet.service(FacesServlet.java:256)
我一直在努力使用
response.sendRedirect("faces/article.jsp2?article_id=" + articleId);
return;
但又一次出錯了。
javax.servlet.ServletException: Cannot forward after response has been committed
javax.faces.webapp.FacesServlet.service(FacesServlet.java:256)
有人可以告訴我在使用jsf時如何從托管java bean重定向?
貝婁是我的代碼(可能是錯誤的,這就是為什么重定向不工作)。
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
String articleId = request.getSession().getAttribute("article_id").toString();
//String articleId = request.getParameter("article_id");
String authorName = request.getSession().getAttribute("user_name").toString();
java.util.Calendar calendar = java.util.Calendar.getInstance();
String commentDate = String.valueOf(calendar.get(java.util.Calendar.DAY_OF_MONTH)) + ".";
commentDate += String.valueOf(calendar.get(java.util.Calendar.MONTH)) + ".";
commentDate += String.valueOf(calendar.get(java.util.Calendar.YEAR));
ArrayList error = new ArrayList();
if(commentName.contains("<"))
{
error.add("Comment name contains illegal characters");
}
if(commentBody.isEmpty() && commentBody.contains("<script"))
{
error.add("Your message body contains illegal characters");
}
if(error.size() > 0)
{
request.getSession().setAttribute("error", error);
error.clear();
FacesContext.getCurrentInstance().getExternalContext().dispatch("article.jsp2?article_id=" + articleId);
}
else
{
Comment comment = new Comment();
comment.setCommentAuthor(authorName);
comment.setCommentBody(commentBody);
comment.setCommentDate(commentDate);
comment.setCommentName(commentName);
comment.setArticleId(articleId);
DisplayArticleIO addComment = new DisplayArticleIO();
addComment.postComment(comment);
// FacesContext.getCurrentInstance().getExternalContext().dispatch("faces/article.jsp2?article_id=" + articleId);
response.sendRedirect("faces/article.jsp2?article_id=" + articleId);
return;
}
先感謝您。
如果有人遇到同樣的問題。
這就解決了我的問題:
FacesContext.getCurrentInstance().getExternalContext().redirect("article.jsp?article_id=" + articleId);
你為什么在一個地方使用派遣並在另一個地方重定向? 這不是問題的根源 - 不是在發送響應后返回,但是,。 除此之外,如果你不介意,我有一些友好的建議:
ArrayList<String>
)。 <img>
標簽,其中src指向一個cookie竊取頁面,這將導致會話劫持。 FacesContext.getCurrentInstance().getExternalContext().redirect("http://www.myUrl.com");
勒內
基本上,在調用response.sendRedirect()之前,某些內容已經將輸出發送到客戶端。 將某些內容發送到瀏覽器后,您無法將其重定向或轉發到其他位置。
通常,應在請求上下文中盡早處理可能導致重定向或轉發的任何方案,以確保在將任何數據發送到客戶端之前進行重定向。 您是否正在通過視圖中的標記調用此代碼?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.