我刚开始学习jsf。我使用JSF2.0和java版本1.8以及tomcat服务器来构建一个简单的表单页面。
我正在尝试从表单中获取值并将其保存在列表中。以下是我写的代码。
createDemand.java(托管bean)
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean(name = "dem")
@RequestScoped
public class DemandBean implements Serializable
{
private static final long serialVersionUID = 1L;
private String role;
private String portfolio;
//getters and setter for role and portfolio
public String submitted(){
System.out.println("Bean executed");
setRole(role);
setPortfolio(portfolio);
List<String> form = new ArrayList<String>();
form.add(role);
form.add(portfolio);
System.out.println(form.toString());
return "new";
}
//tostring
}
当我提交页面时,没有进行任何操作。 demand.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Resource Manager</title>
</h:head>
<h:body>
<h3>Create Demand Request</h3>
<h:panelGrid columns="2">
<h:form>
<h:selectOneMenu value="#{dem.role}">
<f:selectItem itemValue="Project Manager"
itemLabel="Project Manager" />
<f:selectItem itemValue="Technical Lead"
itemLabel="TechnicalLead" />
<f:selectItem itemValue="Business Analyst"
itemLabel="Business Analyst" />
</h:selectOneMenu>
</h:form>
<h:form>
<h:selectOneMenu value="#{dem.portfolio}">
<f:selectItem itemValue="RND IT DDL" itemLabel="RND IT DDL" />
<f:selectItem itemValue="APX" itemLabel="APX" />
<f:selectItem itemValue="CMR" itemLabel="CMR" />
</h:selectOneMenu>
</h:form>
</h:panelGrid>
<h:commandButton id="submitButton" value="submit" type="submit"
action="#{dem.submitted}" />
</h:body>
</html>
这个html用于查看一些知道表单的消息已提交 new.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<head>
<h:outputStylesheet library="css" name="table-style.css" />
<title>Resource manager</title>
</head>
<body>
<h:outputText value="Form submitted" />
<h:outputText value="#{dem.role}"/>
</body>
</html>