命令按钮在JSF页面中不可见

命令按钮在JSF页面中不可见,jsf,Jsf,我正在尝试测试一个示例JSF项目,但由于某些原因,命令按钮没有显示在下面的页面中 <?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/1

我正在尝试测试一个示例JSF项目,但由于某些原因,命令按钮没有显示在下面的页面中

<?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:f="http://java.sun.com/jsf/core"    
   xmlns:h="http://java.sun.com/jsf/html">

   <h:body>
      <h2>Implicit Navigation</h2>
      <hr />
      <h:form>
         <h3>Using Managed Bean</h3>
         <h:commandButton action="#{navigationController.moveToPage1}" 
            value="Page1" />
         <h3>Using JSF outcome</h3>
         <h:commandButton action="page2" value="Page2" />                           
      </h:form> 
      <br/>
      <h2>Conditional Navigation</h2>
      <hr />
      <h:form>          
         <h:commandLink action="#{navigationController.showPage}"
            value="Page1">
            <f:param name="pageId" value="1" />
         </h:commandLink>   

         <h:commandLink action="#{navigationController.showPage}"
            value="Page2">
            <f:param name="pageId" value="2" />
         </h:commandLink>   

         <h:commandLink action="#{navigationController.showPage}" 
            value="Home">
            <f:param name="pageId" value="3" />
         </h:commandLink>   
      </h:form> 
      <br/>
      <h2>"From Action" Navigation</h2>
      <hr />
      <h:form>          
         <h:commandLink action="#{navigationController.processPage1}" 
         value="Page1" />               

         <h:commandLink action="#{navigationController.processPage2}" 
         value="Page2" />       

      </h:form> 
      <br/>
      <h2>Forward vs Redirection Navigation</h2>
      <hr />
      <h:form>          
         <h3>Forward</h3>
         <h:commandButton action="page1" value="Page1" />       
         <h3>Redirect</h3>
         <h:commandButton action="page1?faces-redirect=true" 
         value="Page1" />                   
      </h:form> 
   </h:body>
</html> 
page1.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">
   <h:body>
      <h2>This is Page1</h2>        
      <h:form>
         <h:commandButton action="home?faces-redirect=true" 
            value="Back To Home Page" />            
      </h:form>     
   </h:body>
</html> 

这是第一页
page2.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">
   <h:body>
      <h2>This is Page2</h2>        
      <h:form>
         <h:commandButton action="home?faces-redirect=true" 
            value="Back To Home Page" />            
      </h:form>     
   </h:body>
</html> 

这是第二页

上面已经给出了答案,但我会具体说明。在web.xml中,指定为*.xhtml,jsf文件的扩展名为.xhtml。注意模式规范中缺少的字母。因此,您的文件无法通过FacesServlet识别和路由。只要将“l”添加到web.xml中的url模式中,它就会开始工作。

看起来您正在使用与Faces Servlet url模式不匹配的url模式访问页面。为了确保这一点,请发布您的web.xml Faces Servlet配置以及您访问此页面的方式,即您使用的URL。在webbrowser中打开页面后,右键单击并选择查看源。您是否看到JSF生成的HTML输出或未解析的原始JSF源代码?如果是后者,这就完全解释了你的具体问题,解决方案已经在上面Luiggi这里暗示/回答了。顺便说一下,你的问题重复了,等等。我已经添加了两个文件供你审阅。页面仅显示命令按钮,不可见
   <h:body>
      <h2>Implicit Navigation</h2>
      <hr />
      <h:form>
         <h3>Using Managed Bean</h3>
         <h:commandButton action="#{navigationController.moveToPage1}" 
            value="Page1" />
         <h3>Using JSF outcome</h3>
         <h:commandButton action="page2" value="Page2" />                           
      </h:form> 
      <br/>
      <h2>Conditional Navigation</h2>
      <hr />
      <h:form>          
         <h:commandLink action="#{navigationController.showPage}"
            value="Page1">
            <f:param name="pageId" value="1" />
         </h:commandLink>   

         <h:commandLink action="#{navigationController.showPage}"
            value="Page2">
            <f:param name="pageId" value="2" />
         </h:commandLink>   

         <h:commandLink action="#{navigationController.showPage}" 
            value="Home">
            <f:param name="pageId" value="3" />
         </h:commandLink>   
      </h:form> 
      <br/>
      <h2>"From Action" Navigation</h2>
      <hr />
      <h:form>          
         <h:commandLink action="#{navigationController.processPage1}" 
         value="Page1" />               

         <h:commandLink action="#{navigationController.processPage2}" 
         value="Page2" />       

      </h:form> 
      <br/>
      <h2>Forward vs Redirection Navigation</h2>
      <hr />
      <h:form>          
         <h3>Forward</h3>
         <h:commandButton action="page1" value="Page1" />       
         <h3>Redirect</h3>
         <h:commandButton action="page1?faces-redirect=true" 
         value="Page1" />                   
      </h:form> 
   </h:body>
</html> 
package com.tutorialspoint.test;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

@ManagedBean(name = "navigationController", eager = true)
@RequestScoped
public class NavigationController implements Serializable {

   private static final long serialVersionUID = 1L;

   @ManagedProperty(value="#{param.pageId}")
   private String pageId;

   public String moveToPage1(){
      return "page1"; 
   }

   public String moveToPage2(){
      return "page2"; 
   }

   public String moveToHomePage(){
      return "home"; 
   }

   public String processPage1(){
      return "page"; 
   }

   public String processPage2(){
      return "page"; 
   }

   public String showPage(){
      if(pageId == null){
         return "home";
      }
      if(pageId.equals("1")){
         return "page1";
      }else if(pageId.equals("2")){
         return "page2";
      }else{
         return "home";
      }     
   }

   public String getPageId() {
      return pageId;
   }

   public void setPageId(String pageId) {
      this.pageId = pageId;
   }
}
<?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">
   <h:body>
      <h2>This is Page1</h2>        
      <h:form>
         <h:commandButton action="home?faces-redirect=true" 
            value="Back To Home Page" />            
      </h:form>     
   </h:body>
</html> 
<?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">
   <h:body>
      <h2>This is Page2</h2>        
      <h:form>
         <h:commandButton action="home?faces-redirect=true" 
            value="Back To Home Page" />            
      </h:form>     
   </h:body>
</html>