我正在parseHtml()
方法在java(添加输入,标签等)生成我的JSF Form html元素。我需要处理它。当我输入每个输入值#{foo.bean}时,它会将这些符号显示在输入中。如何正确地做到这一点?
@ManagedBean(name = "parserBean")
@RequestScoped
public class DataFormParserBean {
@ManagedProperty(value = "#{fieldService}")
private FieldService fieldService;
@ManagedProperty(value = "#{builderBean}")
private BuilderManagedBean builderManagedBean;
private List<Field> fieldList;
private List<Object> listWithData;
private Integer index = 0;
@PostConstruct
public void init() {
fieldList = fieldService.getListOfFields();
listWithData = new ArrayList<>(fieldList.size());
}
public FieldService getFieldService() {
return fieldService;
}
public void setFieldService(FieldService fieldService) {
this.fieldService = fieldService;
}
public BuilderManagedBean getBuilderManagedBean() {
return builderManagedBean;
}
public void setBuilderManagedBean(BuilderManagedBean builderManagedBean) {
this.builderManagedBean = builderManagedBean;
}
/**
* Method provides parsing String description of fields(from database) to
* UIComponents and display them on screen
*
* @return HtmlForm object that contains
*/
public HtmlForm parseToHtml() {
HtmlForm form = new HtmlForm();
fieldList.forEach(item -> {
form.getChildren().add(createLabel(item));
form.getChildren().add(builderManagedBean.getMapWithBuilder().get(item.getType()).build(item, index));
index += 1;
});
form.getChildren().add(createSubmitButton());
return form;
}
private HtmlOutputLabel createLabel(Field field) {
HtmlOutputLabel label = new HtmlOutputLabel();
label.setValue(field.getLabel());
return label;
}
private HtmlCommandButton createSubmitButton() {
HtmlCommandButton button = new HtmlCommandButton();
button.setValue("Add");
button.setId("addDataButton");
return button;
}
}
在这里,我正在调用此方法,它向我显示了我的表单。但我怎么能处理呢?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:b="http://bootsfaces.net/ui"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
<h:head>
<title>Add data</title>
<h:outputScript library="js" name="jquery-3.2.1.min.js" />
</h:head>
<h:body>
<h:form>
<b:commandButton look="link" action="response?faces-redirect=true"
value="#{responseBean.count}" />
<b:commandButton look="link" action="fields?faces-redirect=true"
value="Fields" />
</h:form>
<h:form binding="#{parserBean.parseToHtml()}">
</h:form>
<p:socket onMessage="handleMessage" channel="/counter" />
<script type="text/javascript">
function handleMessage(data) {
$('.display').html(data);
}
</script>
</h:body>
</html>
更新 我的带有配置的web.xml文件
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>JSF Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JSF Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>JSF Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>fields.xhtml</welcome-file>
</welcome-file-list>
</web-app>