问题
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.
I run my code in tomcat 6. its working. I deployed in tomcat 7. Its not working. c:forEach tag result not appearing.
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>
I used the following 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>
faces-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;
}
}
Help me, Thanks in advance.
回答1:
Remove standard.jar
. It's from JSTL 1.1 and conflicting with your JSTL 1.2.
See also:
- Our JSTL wiki page
回答2:
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.
- 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.
- 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).
来源:https://stackoverflow.com/questions/14951545/cforeach-not-working-in-tomcat-7