我是JSF
的新手,当我尝试测试一个简单的代码时,我得到了这个error
。
我有一个课程Person.java(ManagedBean)
:-
@ManagedBean
public class Person {
private String firstName;
private String lastName;
private int age;
// Setters/Getters for firstName, lastName, age
public String concatMyInfo(){
return "My name is : " + firstName + " " + lastName + " ... and my age is :" + age;
}
}
我有一个名为test.xhtml
:-的文件
<h:form>
FirstName :
<h:inputText value="#{person.firstName}" /><br/>
LastName :
<h:inputText value="#{person.lastName}" /><br/>
Age :
<h:inputText value="#{person.age}" /><br/>
<h:commandButton value="click me" action="#{person.concatMyInfo}" />
</h:form>
我得到了这个错误:-WARNING: JSF1091: No mime type could be found
当您从UIInput
(在本例中为<h:commandButton>
)调用的方法返回String
时,String
应该是处理请求后要转发的视图的名称。
如果您只想测试JSF是否正在执行该方法,那么请使用简单的System.out.println
,并将该方法更改为void
。例如:
public void concatMyInfo() {
System.out.println("My name is : " + firstName + " " + lastName + " ... and my age is :" + age);
}
如果您想更新将在下一个视图中显示给用户的值,那么在托管bean中使用一个新字段并返回视图的名称。例如:
@ManagedBean
public class Person {
private String firstName;
private String lastName;
private int age;
private String resultMessage;
// Setters/Getters for firstName, lastName, age and resultMessage
public String concatMyInfo() {
resultMessage = "My name is : " + firstName + " " + lastName + " ... and my age is :" + age;
return "result";
}
}
然后,创建一个新页面result.xhtml
:
<!--
Boilerplate code not added here. The page should contain
<html>, <h:head>, etc. No need of a <h:form>
-->
<h:body>
Hello! #{person.resultMessage}
</h:body>
如果你想在同一个页面中显示数据,那么就不要从你的方法中返回任何东西。当拥有void方法时,JSF将刷新当前视图。代码应该是:
public void concatMyInfo() {
resultMessage = "My name is : " + firstName + " " + lastName + " ... and my age is :" + age;
}
然后,在您的视图中,将消息添加到任何位置,可能在底部:
<h:form>
FirstName :
<h:inputText value="#{person.firstName}" /><br/>
LastName :
<h:inputText value="#{person.lastName}" /><br/>
Age :
<h:inputText value="#{person.age}" /><br/>
<h:commandButton value="click me" action="#{person.concatMyInfo}" />
</h:form>
<br />
Result: Hello! #{person.resultMessage}