c:forEach在tomcat 7中不起作用

时间:2013-02-19 06:56:24

标签: jsf-2 foreach tomcat7

我使用 c:forEach 标记创建了简单页面。它在tomcat 6中工作。但是not working tomcat 7。使用JSF 2.0开发简单的Web应用程序。

我在tomcat 6中运行我的代码。 我部署在tomcat 7.它无法正常工作。 c:forEach标记结果未显示。

了welcomeJSF.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@taglib  uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<f:view>
   <html>
      <head>
         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
      </head>
      <body>
            <h:form id="forEachForm" binding="#{simpleDemo.initForm}">
            <c:forEach items="#{simpleDemo.userBeanList}" var="userBean"  varStatus="status">
                <h:panelGrid columns="2" border="1"> 

                    <h:outputText value="#{userBean.userName}"/>
                    <h:outputText value="#{userBean.role}"/>

                </h:panelGrid>

            </c:forEach>
</h:form>
 </body></html></f:view>

我使用了以下jar

1. jsf-api.jar
2. jsf-impl.jar
3. jstl-1.2.jar
4. standard.jar

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>faces/welcomeJSF.jsp</welcome-file>
</welcome-file-list>
</web-app>

面-config.xml中

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns
/javaee/web-facesconfig_2_0.xsd">

<managed-bean>
    <managed-bean-name>simpleDemo</managed-bean-name>
    <managed-bean-class>com.tomcat.foreach.SimpleDemo</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>    
</faces-config>

SimpleDemo.java

package com.tomcat.foreach;

import java.util.ArrayList;
import java.util.List;
import javax.faces.component.html.HtmlForm;

public class SimpleDemo
{
private HtmlForm initForm;
private List<UserBean> userBeanList = new ArrayList<UserBean>();    

public HtmlForm getInitForm()
{
    userBeanList.clear();

    UserBean userBean = new UserBean();
    userBean.setUserName("jack");
    userBean.setRole("sample Role");
    userBeanList.add(userBean);

    userBean = new UserBean();
    userBean.setUserName("adminuser");
    userBean.setRole("Admin Role");
    userBeanList.add(userBean);

    userBean = new UserBean();
    userBean.setUserName("Test User");
    userBean.setRole("Test role");
    userBeanList.add(userBean);

    return initForm;
}

public void setInitForm(HtmlForm initForm){
    this.initForm = initForm;
}
public List<UserBean> getUserBeanList(){
    return userBeanList;
}
public void setUserBeanList(List<UserBean> userBeanList){
    this.userBeanList = userBeanList;
}  }       

UserBean.java

package com.tomcat.foreach;

public class UserBean
{
private String userName;
private String role;

public String getUserName(){
    return userName;
}
public void setUserName(String userName){
    this.userName = userName;
}
public String getRole(){
    return role;
}
public void setRole(String role){
    this.role = role;
}

}

帮帮我, 提前谢谢。

2 个答案:

答案 0 :(得分:3)

删除standard.jar。它来自JSTL 1.1并与您的JSTL 1.2冲突。

另见:

答案 1 :(得分:0)

您的代码中的表单绑定存在问题(您不以编程方式创建表单)和视图中的表单定义,因此删除绑定将解决您的大多数问题。其余部分在评论和答案中提到。

  1. 视图
  2. 首先,不需要使用表单绑定。 JSF旨在简化开发,因此使用其功能。因此,您的表单将在您的视图中完全定义。接下来,您对<c:forEach>的使用很奇怪。它必须在您的代码中工作,但是当您的视图增长时,它可能会导致臭名昭着的视图构建时间与视图渲染时间问题的细微且难以调试的问题。有关精彩的详细说明,请参阅BalusC的Jstl in jsf2 facelets makes sense回答,记住<c:forEach>是JSTL标记处理程序,<ui:repeat>是JSF UI组件。

    <h:form id="forEachForm" >
        <ui:repeat value="#{simpleDemo.userList}" var="user">
            <h:panelGrid columns="2" border="1"> 
                <h:outputText value="#{userBean.userName}"/>
                <h:outputText value="#{userBean.role}"/>
            </h:panelGrid>
        </ui:repeat>
    </h:form>
    

    但请注意,使用<c:forEach>代替<ui:repeat>的视图也可以使用。

    1. 模型
    2. 请注意,User不是托管bean,而是模型数据类(@Entity或POJO)。您的托管bean(您决定放入会话范围)将因此将当前用户保留在列表中。

      我建议使用注释来声明托管bean,但是,当然,您可以自由地在一个好的faces-config.xml中定义它们。

      此外,getForm()中的商业方法是一种不好的做法。最好在@PostConstructpreRederView事件/页面action上完成,但不能在getter方法中完成。

      @ManagedBean
      @SessionScoped
      public class SimpleDemo {
      
          private List<User> userList = new ArrayList<>();
      
          public SimpleDemo() {
              //mock data
              User user1 = new User("User 1", "Role 1");
              User user2 = new User("User 2", "Role 2");
              userList.add(user1);
              userList.add(user2);
          }
      
          //getters and setters for list
      
      }
      
      public class User {
      
          private String userName;
          private String role;
      
          //constructors, setters, getters
      
      }
      

      使用此设置,您的表单将在检索信息时填充用户列表(因为它将在bean的构造函数中完全初始化)。