0

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>`

小服务程序

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?

4

1 回答 1

0

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

例如

<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(假设您FacesServlet已经映射到 的<url-pattern>*.xhtml)。

顺便说一句,我会处理您的Java 命名约定。类名必须以大写开头。

于 2013-10-30T13:19:25.667 回答