我是 JSF、Java EE 的新手,我在尝试使用 Faces.xml、JSF 和 ManagedBeans 的一个小测试用例时遇到了一点问题。
我有一个名为 beanManager.java 的 managedBean,在其中,我有两个字段(名称和测试名称)和一个名为 testcase1() 的方法,它只返回一个字符串。现在,在 frontpage.xhtml 中,我有一个输入文本框,它从用户那里获取名称,一旦用户单击提交按钮,就会调用 testcase1() 方法,它所做的只是设置 BeanManager 的 testname 字段.java 类与用户的输入,但出于某些原因 - 显然为什么我要发送此消息 - 当用户输入名称并点击搜索按钮时,页面导航到 displaypage.xhtml 并且页面不显示任何内容。它应该显示用户输入的名称,但它是空白的。有人建议我使用 @SessionScoped 而不是 @RequestScoped,但现在的问题是,当我在 glassfish 上部署 Java EE 应用程序时,出现以下错误:
Exception Occurred :Error occurred during deployment: Exception while loading the app :
java.lang.IllegalStateException: ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: java.lang.IllegalArgumentException:
javax.servlet.ServletException: com.sun.enterprise.container.common.spi.util.InjectionException:
Error creating managed object for class: class org.jboss.weld.servlet.WeldListener
下面是我的文件..
BeanManager.java
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
@ManagedBean(name = "user")
@SessionScoped
public class BeanManager {
private String name = "";
private String testname = "";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String testcase1(){
setTestname(this.name);
return "test1";
}
public String getTestname() {
return testname;
}
public void setTestname(String testname) {
this.testname = testname;
}
}
前端页面.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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">
<ui:composition template="WEB-INF/templates/origin.xhtml">
<ui:define name="content">
<f:view>
<h:form>
<h:panelGrid>
<h:inputText value="#{user.name}" required = "true"/>
</h:panelGrid>
<h:commandButton
action="#{user.testcase1()}"
value="Search"></h:commandButton>
</h:form>
</f:view>
</ui:define>
</ui:composition>
</html>
显示页面.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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">
<ui:composition template="/WEB-INF/templates/origin.xhtml">
<ui:define name="content">
<h:panelGrid>
<h:outputText value="#{user.testname}"/>
<h:commandButton id="back" value="GoBack" action="frontpage"/>
</h:panelGrid>
</ui:define>
</ui:composition>
</html>
面孔-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<navigation-rule>
<from-view-id>/frontpage.xhtml</from-view-id>
<navigation-case>
<from-action>#{user.testcase1()}</from-action>
<from-outcome>test1</from-outcome>
<to-view-id>/displaypage.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/displaypage.xhtml</from-view-id>
<navigation-case>
<from-outcome>GoBack</from-outcome>
<to-view-id>/frontpage.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>