c:forEach not working in tomcat 7

前端 未结 2 2015
后悔当初
后悔当初 2021-01-27 07:01

I created simple page using c:forEach tag. Its working in tomcat 6. But not working tomcat 7. Developed simple web application using JSF 2.0.

相关标签:
2条回答
  • 2021-01-27 07:43

    Remove standard.jar. It's from JSTL 1.1 and conflicting with your JSTL 1.2.

    See also:

    • Our JSTL wiki page
    0 讨论(0)
  • 2021-01-27 07:43

    There is a problem with form binding in your code (you do not create your form programmatically) and your form definition in your view, so removal of binding will solve most of your problems. The rest is metioned in comments and in this answer.

    1. The view

    First of all, there is no need to use the form binding. JSF is designed to simplify development, so use its features. So, your form will be defined in your view completely. Next, your usage of <c:forEach> is strange here. It must work in your code, but when your view grows it might cause subtle and hard-to-debug problems of the notorious view build time vs view render time problems. For excellent elaboration, consult Jstl in jsf2 facelets makes sense answer by BalusC here, remembering that <c:forEach> is a JSTL taghandler and <ui:repeat> is a JSF UI component.

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

    But note that your view with <c:forEach> instead of <ui:repeat> would also work.

    1. The model

    Note that User is not a managed bean, but your model data class (@Entity or POJO). Your managed bean (which you decided to put in session scope) will thus be holding current users in a list.

    I recommend turning to annotations for declaration of managed beans, but, of course, you are free to define them in a good old faces-config.xml.

    Also, the business method in getForm() is a bad practice. It should be better done either in @PostConstruct, or upon preRederView event / page action, but not in a getter method.

    @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
    
    }
    

    With this setup your form will be populated with a list of users upon retrieval of information (as it would be fully initialized in a constructor of a bean).

    0 讨论(0)
提交回复
热议问题