當前我正在更新一個jsf項目,並實現了一個關於項目的奇怪的事情,當一個jsf頁面獲取請求和頁面返回給客戶端時,我在瀏覽器中完全看到頁面之後,對同一頁面的新請求到達,儘管事實我沒有點擊任何我正在使用導航處理navigation.I在我的項目中使用jsf(myfaces),richfaces。JSF導航問題
我在這些類上設置了兩個斷點,並且我看到大部分頁面,並非全部,發送的請求和請求經過menufilter - > myfacesservletwrapper(在此時瀏覽器完全顯示頁面)之後,此菜單過濾器中斷了另一個請求同一頁。
package com.endersys.itap.ui;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.endersys.itap.ui.module.user.User;
import com.endersys.itap.ui.module.user.UserManager;
import java.io.FileInputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MenuFilter implements Filter {
private String ALLOWED = "login.xhtml";
private String ALLOWED_FOLDER = "a4j";
private static boolean searchEnabled = false;
private static boolean syslogServiceEnabled = false;
private static String SYSLOG_PAGE= "syslogsettings.xhtml";
private Properties conf;
private String SEARCH_PAGE= "search.xhtml";
private static final String BASE_PATH = "/opt/itap/logmonitor/";
private static final String CONF_PATH = BASE_PATH + "etc/logmonitor.properties";
private static Logger logger = Logger.getLogger(MenuFilter.class.getName());
public void destroy() {
}
public void init(FilterConfig arg0) throws ServletException {
if(loadConfiguration())
{
if(conf.getProperty("search_enabled").equalsIgnoreCase("true"))
{
searchEnabled = true;
}
try
{
if(conf.getProperty("syslog_enabled").equalsIgnoreCase("true"))
{
syslogServiceEnabled = true;
}
}catch(Exception exc)
{
exc.printStackTrace();
}
}
}
private boolean loadConfiguration()
{
conf = new Properties();
FileInputStream fis = null;
try {
fis = new FileInputStream(CONF_PATH);
conf.load(fis);
} catch (Exception e) {
logger.log(Level.SEVERE, e.getMessage(), e);
return false;
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
logger.log(Level.SEVERE, e.getMessage(), e);
}
}
return true;
}
/**
* TODO Unit test this function extensively.
*/
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
try
{
UserManager userManager = (UserManager) ((HttpServletRequest) req)
.getSession(true).getAttribute("userManager");
// http://localhost:8080/itapgui-v2*/index.xhtml*
String relativePath = ((HttpServletRequest) req).getServletPath();
// Servlet path has a leading "/" but our menu items do not.
relativePath = relativePath.substring(1);
// http://localhost:8080*/itapgui-v2*/index.xhtml
String contextPath = ((HttpServletRequest) req).getContextPath();
if(!searchEnabled && relativePath.endsWith(SEARCH_PAGE))
{
((HttpServletResponse) res).sendRedirect(contextPath
+ "/index.xhtml");
return;
}
if(!syslogServiceEnabled && relativePath.endsWith(SYSLOG_PAGE))
{
((HttpServletResponse) res).sendRedirect(contextPath
+ "/index.xhtml");
return;
}
if (!relativePath.endsWith(ALLOWED)
&& !relativePath.startsWith(ALLOWED_FOLDER)) {
// Permission required.
// if (relativePath.endsWith("logout.xhtml")) {
// ((HttpServletRequest) req).getSession(true).invalidate();
// ((HttpServletResponse) res).sendRedirect(contextPath
// + "/login.xhtml");
// return; // Required.
// }
if (userManager == null) {
// Not authorized.
if(relativePath != null && relativePath.endsWith("index.xhtml"))
{
((HttpServletResponse) res).sendRedirect(contextPath
+ "/login.xhtml");
}else
{
((HttpServletResponse) res).sendRedirect(contextPath
+ "/login.xhtml?session=expired");
}
return; // Required.
}
User user = userManager.getUser();
if (user.getId() == null) {
// Not authorized.
((HttpServletResponse) res).sendRedirect(contextPath
+ "/login.xhtml");
return; // Required.
} else if (user.getId() != 1) {
Menu menu = (Menu) ((HttpServletRequest) req).getSession(true)
.getAttribute("menu");
MenuItem item = menu.getItemByPath(relativePath);
if(item != null)
{
if (!userManager.access(item.getPerms())) {
((HttpServletResponse) res).sendRedirect(contextPath
+ "/error.xhtml");
return; // Required.
}
}
}
}
chain.doFilter((HttpServletRequest) req, (HttpServletResponse) res);
}catch(Exception exc)
{
exc.printStackTrace();
if(exc instanceof IOException)
{
throw (IOException) exc;
}
else if(exc instanceof ServletException)
{
throw (ServletException) exc;
}
}
}
}
public class MyFacesServletWrapper extends MyFacesServlet {
private static final String CONN_ERROR_URI = "/dberror.xhtml";
private static final String OTHER_ERROR_URI = "/errors.xhtml";
@Override
public void service(ServletRequest request, ServletResponse response) throws IOException, ServletException {
try {
super.service(request, response);
} catch (ServletException e) {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
//if an database exception has occured
if (ExceptionUtils.indexOfType(e, javax.persistence.PersistenceException.class) != -1) {
res.sendRedirect(req.getContextPath() + CONN_ERROR_URI);
}
else {
// add the exception to the session scope attribute
// to show stack trace
req.getSession().setAttribute("exception", e);
res.sendRedirect(req.getContextPath() + OTHER_ERROR_URI);
}
}
}
}
我還沒有解決的問題,但我認爲它從RichFaces的結果。但我是不確定,任何人都對richfaces有深入的瞭解,而ajax可以提供很大的幫助。 – ayengin 2011-03-11 22:51:21
這很難說。 Richfaces可能提出兩個請求的原因有很多。我猜想有多個來自客戶端的請求,它不是您的servlet或過濾器中的問題。雖然我可能會誤解你的英語。 – Adam 2011-03-21 21:27:55
是否有可能兩個發送了兩個請求,當我點擊某個源動作時。例如,如果有一個頁面在另一個表格內有一個表格 – ayengin 2011-03-24 19:45:32