请在下面找到我的xml示例:
<product>
<shoes brand="Nike" price="58 Euros" size="43" />
</product>
我可以阅读xml并将品牌,价格和尺寸的值放在3输入文本中。
现在我在inputtext中有了值,我希望能够修改它们并生成一个新的xml文件。我遇到的问题是,当我生成在带有inputextarea的对话框中显示的新xml时,它仍然是初始值。请在下面找到我的完整代码:
index.xhtml:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta charset="utf-8" />
<title>XStream</title>
</h:head>
<h:body id="mybody">
<h:panelGrid columns="2" cellpadding="5" id="mongrid">
<h:outputLabel for="brand" value="Brand :" />
<p:inputText id="brand" value="#{xmlreader.product.shoes.brand}"/>
<h:outputLabel for="size" value="Size :" />
<p:inputText id="size" value="#{xmlreader.product.shoes.size}"/>
<h:outputLabel for="price" value="Price :" />
<p:inputText id="price" value="#{xmlreader.product.shoes.price}"/>
<p:commandButton value="Generate XML" actionListener="# {xmlreader.testDataModelGeneration()}" />
</h:panelGrid>
<h:form id="readxml" enctype="multipart/form-data">
<p:fileUpload id="upload"
mode="advanced"
required="true"
cancelLabel="Cancel XML"
style="margin-top: 15px;"
requiredMessage="At least one file needs to be selected"
allowTypes="/(\.|\/)(xml)$/"
invalidFileMessage="Not allowed file type"
invalidSizeMessage="The file is too large (500 kb max)"
uploadLabel="Process XML"
fileLimit="1"
fileLimitMessage="Please load one file at a time"
dragDropSupport="true"
label="Select XML"
multiple="false"
fileUploadListener="#{xmlreader.allocation}"
sizeLimit="500000"
update=":mongrid"
/>
</h:form>
<p:dialog id="dialogId"
draggable="false"
dynamic="true"
header="XML Generated"
widgetVar="dlgxml"
width="1115"
height="650"
closable="true"
modal="true"
><!-- Affichage du XML -->
<h:form id="xml">
<p:inputTextarea value="#{xmlreader.xmlFinal}"
cols="115"
autoResize="true"
rows="20"
/>
</h:form>
</p:dialog>
</h:body>
类产品:
@XStreamAlias("product")
public class Product implements Serializable{
private Shoes shoes;
public Product() {
}
public Product(String className) {
shoes = new Shoes();
}
public Shoes getShoes() {
return shoes;
}
public void setShoes(Shoes shoes) {
this.shoes = shoes;
}
}
班级鞋子:
@XStreamAlias("shoes")
public class Shoes implements Serializable{
@XStreamAsAttribute
private String brand;
@XStreamAsAttribute
private String price;
@XStreamAsAttribute
private String size;
public Shoes() {
}
public Shoes(String brand, String price, String size) {
this.brand = brand;
this.price = price;
this.size = size;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
}
我的豆子:
@SessionScoped
@ManagedBean(名称= “的XMLReader”) 公共类XmlReader实现Serializable {
private Product product;
private Shoes shoes;
private String xmlFinal;
private StringBuilder xml;
@PostConstruct
public void init() {
this.product = new Product();
this.shoes = new Shoes();
this.xml = new StringBuilder();
}
/*read an existing xml file and complete the input text*/
public void allocation(FileUploadEvent event) throws IOException{
this.xml = new StringBuilder();
try {
try (BufferedReader br = new BufferedReader(new InputStreamReader(event.getFile().getInputstream(), "UTF-8"))) {
String line;
while ((line = br.readLine()) != null) {
this.xml.append(line);
// System.out.println(line);
}
}
} catch (Exception e) {
System.out.println(e);
}
try {
XStream xstream = new XStream();
xstream.processAnnotations(Product.class);
xstream.autodetectAnnotations(true);
Product resultat = (Product) xstream.fromXML(this.xml.toString());
this.product = resultat;
} catch (Exception e) {
e.printStackTrace();
}
}
/*Generate a new xml displayed in a dialog*/
public void testDataModelGeneration(){
StringBuilder xml = new StringBuilder();
this.xmlFinal = new String();
XStream xstream = new XStream();
xstream.autodetectAnnotations(true);
String xml2 = xstream.toXML(this.product);
xml.append(xml2);
this.xmlFinal = xml.toString();
RequestContext.getCurrentInstance().execute("PF('dlgxml').show();");
//this.xmlFinal = xml.toString();
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Shoes getShoes() {
return shoes;
}
public void setShoes(Shoes shoes) {
this.shoes = shoes;
}
public String getXmlFinal() {
return xmlFinal;
}
public void setXmlFinal(String xmlFinal) {
this.xmlFinal = xmlFinal;
}
public StringBuilder getXml() {
return xml;
}
public void setXml(StringBuilder xml) {
this.xml = xml;
}
}
亲切的问候
答案 0 :(得分:0)
您需要刷新对话框内容才能显示Backing Bean的最新更改。
RequestContext.getCurrentInstance().update(":dialogId:contentId");
RequestContext.getCurrentInstance().execute("PF('dlgxml').show();");
在XHtML中,如果您使用按钮显示对话框,则需要具有更新属性,如下所示update=":dialogId:contentId"
答案 1 :(得分:0)
要更新对话框中的输入文本值,您只需:
1)将inputtext放在表单中。 2)在fileUpload中,通过其id更新此表单。
非常感谢我的同事。