我是JSF及其同事的新手。
我的开发环境如下 Java 6 雄猫6 日食 Primeface 2.5
我正在尝试使用即时primefaces启动器中描述的测试Web应用程序。
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>PrimeFaces Web Application</display-name>
<!-- Change to "Production" when you are ready to deploy -->
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<!-- Welcome page -->
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<!-- JSF mapping -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map these files with JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
faces-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_1.xsd"
version="2.1">
的beans.xml
是空文件
的index.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:p="http://primefaces.org/ui">
<h:head />
<body>
<ui:composition template="/WEB-INF/templates/main.xhtml">
<ui:define name="title">Welcome to our site</ui:define>
<ui:define name="content">
<h:form>
<h:panelGrid columns="2" border="0">
<p:panel header="Please Sign In">
<p:panelGrid columns="2" styleClass="no-border"
columnClasses="no-border,no-border">
<p:outputLabel value="Username" for="username" />
<p:inputText id="username" label="Username"
value="#{credentials.username}" />
<p:outputLabel value="Password" for="password" />
<p:password id="password" label="Password"
value="#{credentials.password}" />
<h:outputText value="" />
<p:commandLink value="Forgot your password?" />
<h:outputText value="" />
<p:commandButton value="Login" action="members"
actionListener="#{loginController.login}" />
<h:outputText value="" />
</p:panelGrid>
</p:panel>
<ul>
<li>Find out what's happening</li>
<li>Rate and review shows</li>
<li>Invite your friends</li>
<li><a href="/faces/register.xhtml">Join today!</a>
</li>
</ul>
</h:panelGrid>
</h:form>
</ui:define>
</ui:composition>
</body>
</html>
loginController.java
package com.example.starterexample.controller;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import com.example.starterexample.model.Credentials;
/**
* Controller class for desktop and mobile login functionality.
*
* @uml.dependency supplier="com.mycompany.controller.AbstractController"
*/
@ManagedBean(name="loginController", eager=true)
//@ViewScoped
@RequestScoped
/*
* @ViewScoped means the object is created by JSF
* and its lifecycle is scoped to the current view.
* Once the user navigates to another view, the LoginControllerobject is released and
* will eventually be garbage-collected by the JVM
*/
public class LoginController extends AbstractController {
private static final long serialVersionUID = -6710063228036238737L;
@ManagedProperty(value = "#{credentials}")
private Credentials credentials;
public String login() {
String outcome = null;
try {
String username = credentials.getUsername();
String password = credentials.getPassword();
//User user = userService.findUser(username, password);
//if (user == null) {
// addErrorMessage("Invalid login");
// userSession.setUser(null);
//} else {
// userSession.setUser(user);
// if (!activeUsers.contains(user)) {
// activeUsers.add(user);
// }
//outcome = "members";
//}
addErrorMessage("Invalid login");
} catch (Exception e) {
logger.error("Unable to login:", e);
}
return outcome;
}
public void setCredentials(Credentials credentials) {
this.credentials = credentials;
}
}
Credentials.java
package com.example.starterexample.model;
import javax.enterprise.inject.Model;
@Model
public class Credentials {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
错误:
Sep 21, 2013 5:03:34 PM com.sun.faces.lifecycle.ProcessValidationsPhase execute
WARNING: /index.xhtml @19,67 value="#{credentials.username}": Target Unreachable, identifier 'credentials' resolved to null
javax.el.PropertyNotFoundException: /index.xhtml @19,67 value="#{credentials.username}": Target Unreachable, identifier 'credentials' resolved to null
at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
at org.primefaces.renderkit.InputRenderer.findImplicitConverter(InputRenderer.java:170)
at org.primefaces.renderkit.InputRenderer.findConverter(InputRenderer.java:190)
at org.primefaces.renderkit.InputRenderer.getConvertedValue(InputRenderer.java:196)
at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
at javax.faces.component.UIInput.validate(UIInput.java:960)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
at javax.faces.component.UIInput.processValidators(UIInput.java:698)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at org.primefaces.component.panel.Panel.processValidators(Panel.java:297)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIForm.processValidators(UIForm.java:253)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
请帮我找一下我错过的东西。
我找了类似的问题,没有什么对我有用。
我检查了创建的war文件,并在WEB-INF / classes
中找到了类文件提前致谢。
答案 0 :(得分:2)
我也用这个例子进行自学等。其实我遇到了和你一样的问题。此示例有错误,因此您不应盲目使用它。这个article帮助我更好地理解bean类型(CDI,EJB,JSF bean)和编写可行的代码。
您不应该将JSF托管bean与CDI一起使用。 CDI bean本身更强大。 这对我有用。
LoginController就像
一样启动import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
@Named
@javax.faces.view.ViewScoped
public class LoginController extends AbstractController {
private static final long serialVersionUID = -6710063228036238737L;
@Inject
private ActiveUsers activeUsers;
@Inject
private Credentials credentials;
@Inject
private UserService userService;
....
}
ActiveUsers看起来像
import java.util.ArrayList;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
@Named("activeUsers")
@ApplicationScoped
public class ActiveUsers extends ArrayList<User> {
private static final long serialVersionUID = -3068333669859836249L;
}
凭证:
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
@Named("credentials")
@RequestScoped
public class Credentials {
private String password;
private String username;
public String getPassword() {
return password;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
}
希望这会对你有所帮助
答案 1 :(得分:1)
你不能做你想做的事:使用JSF注释访问CDI bean。 javax.enterprise.inject.model
是基于CDI的注释,因此,根据您的JSF版本,您有以下选项:
使用loginController
将@Named
转换为CDI bean,然后使用@Inject来获取模型 - JSF 2.1
升级到JSF 2.2,允许您将@Inject
和@ManagedProperty
与CDI bean一起使用
答案 2 :(得分:0)
只能在faces上下文中访问ManagedBeans。因此,您必须通过credentials
访问loginController
,例如#{loginController.credentials.username}
。您还需要向LoginController添加getCredentials()
方法。