我有一个包含文件夹树视图的项目。
我决定创建一个自定义组件。
XHTML看起来很棒,我可以在解码方法上获取所选文件的请求参数。
问题是:我无法获得ManagedBean属性的值(请参阅Submit方法)...
这是我的代码,我不明白是哪里错了,或者我想念...
自定义组件
@FacesComponent(createTag = true, tagName = "indexComponent", namespace = "http://index.com/tags")
public class IndexComponent extends UISelectMany{
private String pathString = "/home/test/";
private List<Path> filesNames = new ArrayList<>();
private static String NAME_CHKBOX = "checkbox";
IndexComponent instance = this;
public IndexComponent getInstance() {
return instance;
}
public void setInstance(IndexComponent instance) {
this.instance = instance;
}
@Override
public String getFamily() {
return "treeview";
}
@Override
public void encodeBegin(FacesContext context) throws IOException{
ResponseWriter writer = context.getResponseWriter();
writer.startElement("ul", this);
writer.writeAttribute("data-role", "treeview", null);
writer.writeAttribute("id", this.getClientId(context), null);
Files.walkFileTree(Paths.get(pathString), new HashSet<>(),2, new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
filesNames.add(dir);
writer.startElement("li", instance);
writer.writeAttribute("data-caption", dir.getFileName() , null);
writer.writeAttribute("class", "" , null);
writer.startElement("ul", instance);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
filesNames.add(file);
writer.startElement("li", instance);
writer.startElement("input", instance);
writer.writeAttribute("type", "checkbox" , null);
writer.writeAttribute("name", getHtmlNameCheckBox(context) , null);
writer.writeAttribute("value", file.getFileName() , null);
writer.writeAttribute("data-role", "checkbox" , null);
writer.writeAttribute("data-caption", file.getFileName() , null);
writer.writeAttribute("title", "" , null);
writer.endElement("input");
writer.endElement("li");
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
writer.endElement("li");
writer.endElement("ul");
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
writer.startElement("li", instance);
writer.writeAttribute("data-caption", file.getFileName() , null);
writer.endElement("li");
return FileVisitResult.CONTINUE;
}
});
writer.endElement("ul");
instance.encodeEnd(context);
}
public List<Path> getFilesNames() {
return filesNames;
}
public void setFilesNames(List<Path> filesNames) {
this.filesNames = filesNames;
}
@Override
public void decode(FacesContext context) {
String[] result = context.getExternalContext().getRequestParameterValuesMap().get( getHtmlNameCheckBox(context) );
if(result != null && result.length > 0) {
List<String> liste = Arrays.asList( result );
this.setSubmittedValue(liste);
}
}
private String getHtmlNameCheckBox(FacesContext context){
return String.format("%s:%s", this.getClientId(context), NAME_CHKBOX);
}
}
ManadgeBean
@Named
@ViewScoped
public class IndexBean implements Serializable{
private static final long serialVersionUID = -1L;
private List<String> filesNames;
@PostConstruct
public void init() {
filesNames = new ArrayList<>();
}
public void submit() {
LogUtils.info("submit !!!");
for (String s : filesNames) {
LogUtils.info(s);
}
}
public List<String> getFilesNames() {
return filesNames;
}
public void setFilesNames(List<String> filesNames) {
this.filesNames = filesNames;
}
}
XHTML
<!DOCTYPE html>
<h:html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:t="http://index.com/tags"
xmlns:composite="http://java.sun.com/jsf/composite">
<h:head>
<h:outputStylesheet name="metro-all.css" library="css" />
<h:outputScript name="jquery-3.3.1.min.js" library="js" />
<h:outputScript name="metro.js" library="js" />
</h:head>
<h:body>
<h:form>
<t:indexComponent value="#{indexBean.filesNames}" />
<h:commandButton actionListener="#{indexBean.submit()}" type="submit" styleClass="button primary" value="Submit" />
</h:form>
</h:body>
</h:html>
注意:我不想使用Primefaces或其他框架。