Java JSF页面未显示内容
在系统中,用户可以键入“URL”,如“”,并且该URL必须在服务器中处理,并且在内容显示在XHTML页面中之后处理 在我的XHTML中,我有:Java JSF页面未显示内容,java,jsf,jsf-2,Java,Jsf,Jsf 2,在系统中,用户可以键入“URL”,如“”,并且该URL必须在服务器中处理,并且在内容显示在XHTML页面中之后处理 在我的XHTML中,我有: <h:form id="formNavegador" enctype="multipart/form-data"> <p:inputText value="#{navegadorMB.url}" required="true" requiredMessage="A url é obriga
<h:form id="formNavegador" enctype="multipart/form-data">
<p:inputText value="#{navegadorMB.url}" required="true"
requiredMessage="A url é obrigatória"
type="Digite a url para navegar. Ex: http://www.google.com.br" />
<h:outputText value="#{navegadorMB.htmlContent}" escape="false"
id="htmlContent" />
<p:commandButton id="commandButtonProcessar" value="Ir"
update=":formNavegador:htmlContent" icon="ui-icon-play"
actionListener="#{navegadorMB.processaRequisicao}" />
</h:form>
在我的方法“processaRequisicaoOnServer”中,在返回站点内容后,打开URL并读取所有内容。见:
public String processaRequisicaoOnServer(String url) {
URL urlObj;
try {
urlObj = new URL(url.trim().toLowerCase());
BufferedReader conteudo = new BufferedReader(new InputStreamReader(urlObj.openStream()));
String linha = "";
StringBuffer sb = new StringBuffer();
while((linha = conteudo.readLine()) != null)
{
sb.append(linha);
}
return sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
throw new BOException(e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new BOException(e.getMessage());
}
}
因此,URL的内容显示在控制台中是因为“System.out..”,但h:outputText没有像我希望的那样更新。我在这里看不到您的整个bean,但我注意到了一些事情: 如果这是您的整个表单,则不需要
enctype=“multipart/form data”
,因此您可以将其删除
第一个p:inputText
中有一个奇怪的type
属性。你不是说标题吗
<p:inputText type="Digite a url para navegar. Ex: http://www.google.com.br" />
为此:
<p:commandButton id="commandButtonProcessar" value="Ir" update="htmlContent" icon="ui-icon-play" actionListener="#{navegadorMB.processaRequisicao}" />
public void processaRequisicao() { ...
此外,为了测试这一切,您可以分步进行,首先创建一个模拟方法,检查工作是否正常,然后添加业务内容
例如,要测试这一点,您可以将进程equisico
更改为如下内容:
public void processaRequisicaoMock() {
htmlContent = "Funcionou!";
}
然后调用它,检查视图是否正常工作。如果是,您可以继续添加业务层和所有
我希望有帮助。而不是
<p:commandButton id="commandButtonProcessar" value="Ir"
update=":formNavegador:htmlContent" icon="ui-icon-play"
actionListener="#{navegadorMB.processaRequisicao}" />
属性“type”在删除时没有任何区别,所以我认为这无关紧要。我删除了,但问题仍然存在,viewscope,@icwndStrange,我在这里复制了这个案例,它可以正常工作。你在显示所有的代码吗?您是在测试完全相同的情况,还是有一个表单的字段比此处显示的字段多?我做了测试:Putted@SessionScoped,完成所有过程后,我单击F5刷新浏览器,outputText中的值显示为正常,因此我们知道该值已传递,但未按我的要求更新。我正在更新此行中的“htmlContent”属性:“htmlContent=boPadrao.processaRequisicaoOnServer(url);“对不起,我没有注意到。我已经编辑了我的答案。
public void processaRequisicao() { ...
public void processaRequisicaoMock() {
htmlContent = "Funcionou!";
}
<p:commandButton id="commandButtonProcessar" value="Ir"
update=":formNavegador:htmlContent" icon="ui-icon-play"
actionListener="#{navegadorMB.processaRequisicao}" />
<p:commandButton id="commandButtonProcessar" value="Ir"
update="htmlContent" icon="ui-icon-play"
actionListener="#{navegadorMB.processaRequisicao}" ajax="false"/>
public void processaRequisicao(){
if (url.isEmpty()){
addErrorMessage("Você precisa digitar um endereço");
FacesContext.getCurrentInstance().validationFailed();
}else{
htmlContent = boPadrao.processaRequisicaoOnServer(url);
System.out.println(htmlContent);
}
}