2013-10-22 40 views
0

JSF代码我怎样才能把从servlet的(了request.setAttribute( “studentProfile”,allProfile))数据JSF

<h:dataTable value="#{requestScope.allProfile}" var="allProfile"> 
<h:column>     
<f:facet name="header">StudentID</f:facet>   
<h:inputText value="#{allProfile.studentId}" 
size="10" rendered="#{allProfile.canEdit}" /> 
<h:outputText value="#{allProfile.studentId}" 
rendered="#{not allProfile.canEdit}" /> 
</h:column>` 

的Servlet

private void doProcess(HttpServletRequest request, 
    HttpServletResponse response) throws ServletException, IOException, Exception { 
    StudentManager stuManager = new StudentManager(); 
    List<studto>allProfile = stuManager.getAllstudentProfile(); 
    request.setAttribute("studentProfile", allProfile); 
    RequestDispatcher rd = request.getRequestDispatcher("/ViewPage.xhtml"); 
    rd.forward(request, response); 
} 

我已经能够从把我的数据数据库直到servlet。但我无法在xhtml页面中获取数据。我是否需要创建faces-config来编写托管bean?

+0

JSP背后的关键思想是它有一个BackingBean http://docs.oracle.com/javaee/5/tutorial/doc/bnaqm.html –

+0

请学习JSF,这已在基础知识中介绍过。 [StackOverflow JSF wiki](http://stackoverflow.com/tags/jsf/info)有很好的资源。 –

+0

@ user2310289这些资源用于旧的JSF 1.2。请注意,自从[2013年10月3日](https://javaserverfaces.java.net/)以来,JSF已经在JSF 1.2和JSF 2.0之间改变了很多**,并且目前在JSF 2.2.4中。 –

回答

0

对于为JSF表单准备表单数据的工作,servlet完全是错误的工具。您应该只是在与JSF表单关联的JSF支持bean的(后)构造函数中完成这项工作,就像每个JSF教程中所演示的一样。

E.g.

<h:dataTable value="#{bean.allProfile}" var="allProfile"> 
    <h:column> 
     <f:facet name="header">StudentID</f:facet>   
     <h:inputText value="#{allProfile.studentId}" 
      size="10" rendered="#{allProfile.canEdit}" /> 
     <h:outputText value="#{allProfile.studentId}" 
      rendered="#{not allProfile.canEdit}" /> 
    </h:column> 

与支持bean:

@ManagedBean 
@RequestScoped 
public class Bean { 

    private List<studto> allProfile; 

    @PostConstruct 
    public void init() { 
     allProfile = new StudentManager().getAllstudentProfile(); 
    } 

    public List<studto> getAllProfile() { 
     return allProfile; 
    } 

} 

现在只要在浏览器地址栏中打开的页面由/ViewPage.xhtml(假设你已经映射在*.xhtml<url-pattern>FacesServlet)。

顺便说一句,我会在你的Java naming conventions工作。类别名称必须以大写字母开头