2013-05-02 4 views
0

У меня проблема с тем, что мои запросы AJAX не запускаются в JSF по неизвестным причинам.AJAX не работает в JSF 2.0

admin.xhtml Отрывок:

<h:form id="adminPanel"> 
... 
<f:subview id="editCustomer#{customer.id}"> 
    <p class="#{adminService.getEditCustomerClass(customer.id)}"> 
     <h:inputText id="email#{customer.id}" value="#{adminService.customerEmail}"/><br/> 
     <h:inputText id="firstName#{customer.id}" value="#{adminService.customerFirstName}"/> 
     <h:inputText id="lastName#{customer.id}" value="#{adminService.customerLastName}"/><br/> 
     <h:commandButton id="saveEdit#{customer.id}" type="button" value="Save"> 
      <f:ajax render="@form" event="click" listener="#{adminService.saveCustomer()}"/> 
     </h:commandButton> 
     <h:commandButton id="cancelEdit#{customer.id}" type="button" value="Cancel"> 
      <f:ajax render="@form" event="click" listener="#{adminService.cancelEdit()}"/> 
     </h:commandButton> 
    </p> 
</f:subview> 
... 
</h:form> 

AdminService.java Отрывок:

@Named 
@Stateless 
@LocalBean 
public class AdminService { 

    public String getEditCustomerClass(int id) { 
     return id != customerId ? "hidden" : ""; 
    } 

    public void saveCustomer() { 
     cancelEdit(); 
    } 

    public void cancelEdit() { 
     movieId = -1; 
     customerId = -1; 
     orderId = -1; 
     actorId = -1; 
     employeeId = -1; //if none of the id's match, p should return 'hidden' class and not be seen. 
    } 

} 

Изначально у меня были проблемы, потому что вместо того, чтобы прятаться и показывая с помощью CSS, я с помощью " rendered = ". Тем не менее, я слышал, что частичное рендеринг представлений может сломать AJAX, поэтому я поэтапно отказался от этого, надеясь, что частичное отображение представления (только скрытие и отображение) устранит проблему.

Однако этот аякс по-прежнему не вызывает метод, указанный в атрибуте прослушивателя (вся страница намного больше и использует намного больше AJAX, хотя остальная часть работает до тех пор, пока эти кнопки не будут нажаты, а затем другие кнопки ajax перестать отвечать.)

Если я изменил тип кнопки для отправки, она фактически выполнит метод прослушивателя, если я дважды нажму кнопку, но тогда другие ссылки ajax не вызовут соответствующие методы слушателя.

Как я могу заставить это работать?

Update:

Вот информация из запроса POST, что JSF пожаров по щелчку:

заголовков запроса:

Accept:*/* 
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3 
Accept-Encoding:gzip,deflate,sdch 
Accept-Language:en-US,en;q=0.8 
Connection:keep-alive 
Content-Length:2186 
Content-type:application/x-www-form-urlencoded;charset=UTF-8 
Cookie:JSESSIONID=7342e0de92edc023eecbf706dae3 
Faces-Request:partial/ajax 
Host:www.minimalcomputers.com:8181 
Origin:https://www.minimalcomputers.com:8181 
Referer:https://www.minimalcomputers.com:8181/MovieProject/Admin/admin.xhtml 
User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31 

Форма данных:

adminPanel:adminPanel 
adminPanel:representativeActionPane:customerPane:customersList:0:editCustomer1:email1:[email protected] 
adminPanel:representativeActionPane:customerPane:customersList:0:editCustomer1:firstName1: 
adminPanel:representativeActionPane:customerPane:customersList:0:editCustomer1:lastName1: 
adminPanel:representativeActionPane:customerPane:customersList:1:editCustomer1:email1:[email protected] 
adminPanel:representativeActionPane:customerPane:customersList:1:editCustomer1:firstName1: 
adminPanel:representativeActionPane:customerPane:customersList:1:editCustomer1:lastName1: 
adminPanel:representativeActionPane:customerPane:customersList:2:editCustomer1:email1:[email protected] 
adminPanel:representativeActionPane:customerPane:customersList:2:editCustomer1:firstName1: 
adminPanel:representativeActionPane:customerPane:customersList:2:editCustomer1:lastName1: 
adminPanel:representativeActionPane:customerPane:customersList:3:editCustomer1:email1:[email protected] 
adminPanel:representativeActionPane:customerPane:customersList:3:editCustomer1:firstName1: 
adminPanel:representativeActionPane:customerPane:customersList:3:editCustomer1:lastName1: 
adminPanel:representativeActionPane:customerPane:customersList:4:editCustomer1:email1:[email protected] 
adminPanel:representativeActionPane:customerPane:customersList:4:editCustomer1:firstName1: 
adminPanel:representativeActionPane:customerPane:customersList:4:editCustomer1:lastName1: 
javax.faces.ViewState:1088200739038195170:4402027985833798256 
javax.faces.source:adminPanel:representativeActionPane:customerPane:customersList:0:editCustomer1:cancelEdit1 
javax.faces.partial.event:click 
javax.faces.partial.execute:adminPanel:representativeActionPane:customerPane:customersList:0:editCustomer1:cancelEdit1    adminPanel:representativeActionPane:customerPane:customersList:0:editCustomer1:cancelEdit1 
javax.faces.partial.render:adminPanel 
javax.faces.behavior.event:click 
javax.faces.partial.ajax:true 

заголовка ответа

Cache-Control:no-cache 
Content-Type:text/xml;charset=UTF-8 
Date:Thu, 02 May 2013 21:45:05 GMT 
Server:GlassFish Server Open Source Edition 3.1.2.2 
Transfer-Encoding:chunked 
X-Powered-By:Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.2.2 Java/Sun Microsystems Inc./1.6) 
X-Powered-By:JSF/2.0 

Update 2:

Когда я переключаюсь на commandButtons типа "представить" без AJAX, он работает, как ожидалось (за исключением некоторых кнопок требуется два щелчка на активировать). Таким образом, проблема локализована в тегах f: ajax.

Обновление 3:

Вся база кода для admin.xhtml. Это немного нечисто, потому что он находится в процессе отладки и пытается заставить его работать.

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
     xmlns:f="http://java.sun.com/jsf/core"> 
    <h:head> 
     <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"/> 
     <h:outputStylesheet name="header.css" library="css"/> 
     <link href='http://fonts.googleapis.com/css?family=Noto+Sans' rel='stylesheet' type='text/css'/> 
     <title>VideoPile - Administration</title> 
    </h:head> 
    <h:body> 
     <div id="body"> 
      <ui:insert name="header" > 
       <ui:include src="/templates/header.xhtml" /> 
      </ui:insert> 
      <div id="content"> 
       Admin. 
       <h:form id="adminPanel"> 
        <!--<h:commandButton id="admin" value="Administrative Actions" rendered="{adminService.hasAdminPrivileges()}"> 
         <f:ajax render="@form" event="click" listener="{adminService.toggleAdminPane()}"/> 
        </h:commandButton>--> 
        <h:commandButton id="manager" type="button" value="Manager Actions" rendered="#{adminService.hasManagerPrivileges()}"> 
         <f:ajax render="@form" event="click" listener="#{adminService.toggleManagerPane()}"/> 
        </h:commandButton> 
        <h:commandButton id="representative" type="button" value="Representative Actions" rendered="#{adminService.hasRepresentativePrivileges()}"> 
         <f:ajax render="@form" event="click" listener="#{adminService.toggleRepresentativePane()}" /> 
        </h:commandButton> 
        <br/> 
        <!--<f:subview id="administrativeActionPane" rendered="{userService.showAdminPane}"> 
         Admin Pane 
        </f:subview>--> 
        <f:subview id="managerialActionPane"> 
         <div class="#{adminService.getShowManagerClass()}"> 
         Manager Pane: 
         <h:commandLink id="editmovies" value="Movies"> 
          <f:ajax render="@form" event="click" listener="#{adminService.toggleMoviePane()}"/> 
         </h:commandLink> 
         <h:commandLink id="employes" value="Employees"> 
          <f:ajax render="@form" event="click" listener="#{adminService.toggleEmployeePane()}"/> 
         </h:commandLink> 
         <h:commandLink id="sales" target="_blank" value="View Sales Report" action="/Admin/salesreport"/> 
         <h:commandLink id="employees" target="_blank" value="View Most Active Employees" action="/Admin/activeemployees"/> 
         <h:commandLink id="customers" target="_blank" value="View Most Active Customers" action="/Admin/activecustomers"/> 
         <h:commandLink id="movies" target="_blank" value="View Most Active Movies" action="/Admin/activemovies"/> 
         <br/> 
         <f:subview id="moviesEditPane"> 
          <span class="#{adminService.getShowMovieClass()}"> 
           Movies. 
           <ui:repeat value="#{adminService.currentMoviePage}" var="movie"> 
            #{movie.name} 
            <h:commandLink id="edit" value="Edit"> 
             <f:ajax render="@form" event="click" listener="#{adminService.editMovie(movie.id)}"/> 
            </h:commandLink> 
            <h:outputText rendered="#{adminService.movieId eq movie.id}" value="edit"/> 
            <br/> 
           </ui:repeat> 
          </span> 
         </f:subview> 
         <f:subview id="employeesEditPane"> 
          <span class="#{adminService.getShowEmployeeClass()}"> 
           Employees. 
           <ui:repeat value="#{adminService.currentEmployeePage}" var="employee"> 
            #{employee.firstName} #{employee.lastName} 
            <h:commandLink id="edit" value="Edit"> 
             <f:ajax render="@form" event="click" listener="#{adminService.editEmployee(employee.id)}"/> 
            </h:commandLink> 
            <h:outputText rendered="#{adminService.employeeId eq employee.id}" value="edit"/> 
            <br/> 
           </ui:repeat> 
          </span> 
         </f:subview> 
         </div> 
        </f:subview> 
        <f:subview id="representativeActionPane"> 
         <div class="#{adminService.getShowRepresentativeClass()}"> 
          Customer Representative Pane: 
          <h:commandLink id="recordOrder" value="Record Order"> 
           <f:ajax render="@form" event="click" listener="#{adminService.toggleOrderPane()}"/> 
          </h:commandLink> 
          <h:commandLink id="customers" value="Customers"> 
           <f:ajax render="@form" event="click" listener="#{adminService.toggleCustomerPane()}"/> 
          </h:commandLink> 
          <h:commandLink id="mailingList" target="_blank" value="View Mailing List" action="/Admin/mailinglist"/> 
          <f:subview id="orderPane"> 
           <span class="#{adminService.getShowOrderClass()}"> 
            Create new order. 
           </span> 
          </f:subview> 
          <f:subview id="customerPane"> 
           <span class="#{adminService.getShowCustomerClass()}"> 
            Customers. 
            <ui:repeat id="customersList" value="#{adminService.currentCustomerPage}" var="customer"> 
             <f:subview id="editCustomer#{customer.id}"> 
              <p class="#{adminService.getEditCustomerClass(customer.id)}"> 
               <h:inputText id="email#{customer.id}" value="#{adminService.customerEmail}"/><br/> 
               <h:inputText id="firstName#{customer.id}" value="#{adminService.customerFirstName}"/> 
               <h:inputText id="lastName#{customer.id}" value="#{adminService.customerLastName}"/><br/> 
               <h:commandButton id="saveEdit#{customer.id}" type="submit" value="Save" actionListener="#{adminService.saveCustomer()}"> 
                <f:ajax render="@form" event="click" execute="@form"/> 
               </h:commandButton> 
               <h:commandButton id="cancelEdit#{customer.id}" type="submit" value="Cancel" actionListener="#{adminService.cancelEdit()}"> 
                <f:ajax render="@form" event="click" execute="@form" /> 
               </h:commandButton> 
              </p> 
             </f:subview> 
             <f:subview id="viewCustomer#{customer.id}"> 
              <p class="#{adminService.getViewCustomerClass(customer.id)}"> 
               #{customer.email}<br/> 
               #{customer.firstName} #{customer.lastName}<br/> 
               <h:commandLink id="suggestion" target="_blank" value="View Suggestions" action="/Admin/customersuggestions"> 
                <f:param name="user" value="#{customer.id}"/> 
               </h:commandLink> 
               <h:commandButton id="edit" type="submit" value="Edit" action="#{adminService.editCustomer(customer)}"> 
                <f:ajax render="@form" event="click" execute="@form" /> 
               </h:commandButton> 
              </p> 
             </f:subview> 
             <br/> 
            </ui:repeat> 
           </span> 
          </f:subview> 
         </div> 
        </f:subview> 
       </h:form> 
      </div> 
     </div> 
    </h:body> 
</html> 
+0

не должны там быть ? –

+0

@AkselWillgert Существует форма h: на более высоком уровне. Я не включил это, моя ошибка. –

+0

Пожалуйста, уточните, что «не работайте» в перспективе разработчика, а не в перспективе enduser. Для начала загляните в консоль JS консоли и монитор трафика HTTP. – BalusC

ответ

1

Я нашел ответ, и причиной этого является действительно довольно странно, но это имеет смысл.

В моих идентификаторах я использовал EL для определения уникальных идентификаторов для UIComponents (хотя это и не было необходимо). Когда я удаляю EL из ID, работает ajax!

Я предполагаю (хотя я уверен, что BalusC даст более подробный ответ), было то, что идентификаторы не являются статическими, хотя идентификатор в концепции будет одним и тем же, это влияет на способ JSF находит UIComponents.

Теперь я чувствую себя глупым, потому что проблему можно было бы полностью избежать, если бы я не добавлял лишних элементов.

Например, код в моей должности должен быть следующим:

<h:form id="adminPanel"> 
... 
<f:subview id="editCustomer"> 
    <p class="#{adminService.getEditCustomerClass(customer.id)}"> 
     <h:inputText id="email" value="#{adminService.customerEmail}"/><br/> 
     <h:inputText id="firstName" value="#{adminService.customerFirstName}"/> 
     <h:inputText id="lastName" value="#{adminService.customerLastName}"/><br/> 
     <h:commandButton id="saveEdit" type="button" value="Save"> 
      <f:ajax render="@form" event="click" listener="#{adminService.saveCustomer()}"/> 
     </h:commandButton> 
     <h:commandButton id="cancelEdit" type="button" value="Cancel"> 
      <f:ajax render="@form" event="click" listener="#{adminService.cancelEdit()}"/> 
     </h:commandButton> 
    </p> 
</f:subview> 
... 
</h:form> 
0

Есть ли какая-либо причина для отправки формы событием Javascript onclick? Я предлагаю вам назвать действия, как это:

<h:commandButton id="saveEdit#{customer.id}" value="Submit" actionListener="#{adminService.saveCustomer()}"> 
    <f:ajax render="@form" execute="@form"/> 
</h:commandButton> 
+0

Изменение моего кода для использования «execute =» и «actionListener =» вместо этого также не работает. Это может быть ошибка с JSF или просто мой код слишком запутан. –

+0

Я не мог воспроизвести это поведение. Ваш код работает для меня, поэтому должно быть что-то не так с остальной частью вашего кода. Попытайтесь найти проблему, частично добавив больше кода к вашему фрагменту. Возможно, у вас есть где-то еще h: form open. – Sonic

+0

Это небольшой фрагмент страницы большего размера. Я, вероятно, мог бы лучше абстрагировать взгляды, и я немного смущен кодом, но я опубликую его. –

Смежные вопросы