我是jsf的新手并尝试构建应用。问题是当我点击Login commandButton时没有任何反应。所以我输入了一些打印行,它显示我存在与数据库的连接,参数有效,但它不会重定向。
这是index.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Dezurstvo</title>
</h:head>
<h:body>
Login
<br />
<h:form>
<p:growl id="msgs" showDetail="true"/>
<h:panelGrid columns="2" cellpadding="5">
<h:outputText value="Username"/>
<p:inputText value="#{logIn.username}" required="true"/>
<h:outputText value="Password"/>
<p:password id="pass" value="#{logIn.password}" feedback="false" required="true"/>
</h:panelGrid>
<p:commandButton value="Login" action="#{logIn.loadUser()}" update="msgs" />
<p:commandButton value="Regisracija" />
</h:form>
<h:link outcome="adminMain" value="Primefaces welcome page" />
</h:body>
</html>
NavigationBean类
package beans;
import dataBeans.Korisnik;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
@ManagedBean(name = "navigation")
@RequestScoped
class NavigationBean {
public String getHomepage(){
FacesContext context = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
Korisnik kor = (Korisnik)session.getAttribute("username");
if(!"A".equals(kor.getStatus()) || !"D".equals(kor.getStatus()) || !"N".equals(kor.getStatus()))
return "index";
else if("A".equals(kor.getStatus()))
return "adminMain";
else if("D".equals(kor.getStatus()))
return "demonstratorMain";
else if("N".equals(kor.getStatus()))
return "nastavnikMain";
return "";
}
public static String redirect(String status){
if(!"A".equals(status) || !"D".equals(status) || !"N".equals(status))
return "index";
else if("A".equals(status))
return "adminMain";
else if("D".equals(status))
return "demonstratorMain";
else if("N".equals(status))
return "nastavnikMain";
return "error";
}
}
登录Bean
package beans;
import dataBeans.Korisnik;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;
@ManagedBean(name="logIn")
@RequestScoped
public class LoginBean {
private String user;
private String pass;
private UIComponent component;
public UIComponent getComponent() {
return component;
}
public void setComponent(UIComponent component) {
this.component = component;
}
public LoginBean() {}
/**
* @return the user
*/
public String getUsername() {
return user;
}
/**
* @param username the user to set
*/
public void setUsername(String username) {
this.user = username;
}
/**
* @return the pass
*/
public String getPassword() {
return pass;
}
/**
* @param password the pass to set
*/
public void setPassword(String password) {
this.pass = password;
}
//Loading user into session
public String loadUser() {
FacesContext context = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
FacesMessage msg;
Korisnik kor = Korisnik.getUser(user);
if (user.equals(kor.getUsername())) {
System.out.println( "user je = " + kor.getUsername());
//User exists in database
if (pass.equals(kor.getPassword())) {
System.out.println( "Sifra je = " + kor.getPassword());
System.out.println( "Status = " + kor.getStatus());
//Pasword is OK
session.setAttribute(user, kor);
return NavigationBean.redirect(kor.getStatus());
} else {
//Wrong pass
// System.out.println( "Sifra je = " + kor.getPassword());
msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Pogrešna šifra.");
return msg.toString();
}
} else {
//Wrog user
msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Korisnik sa datim korisničkim imenom ne postoji.");
return msg.toString();
}
}
public String signOut(){
FacesContext context = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
session.invalidate();
return "logIn";
}
}
面对配置
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<navigation-rule>
<from-view-id>/index.xhtml</from-view-id>
<navigation-case>
<from-outcome>adminMain</from-outcome>
<to-view-id>admin/adminMain.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>demonstratorMain</from-outcome>
<to-view-id>demonstrator/demonstratorMain.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>nastavnikMain</from-outcome>
<to-view-id>nastavnik/nastavnikMain.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>error</from-outcome>
<to-view-id>error.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
和网站
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
当单击按钮时,它从LoginBean调用函数loadUser,并且他从NavigationBean调用redirect,并且应该根据用户的状态将我重定向到特定页面,但没有任何反应。此外,当我输入错误数据时,它显示我
Warning: JSF1091: No mime type could be found for file /javax.faces.application.FacesMessage@67cc4160. To resolve this, add a mime-type mapping to the applications web.xml.
Warning: JSF1064: Unable to find or serve resource, /javax.faces.application.FacesMessage@67cc4160.
不知道这与此有什么联系。
我使用glassfish服务器4.1,primefaces 5.0,wamp,netbeans 8.0.2,jsf 2.2
如果需要,这是树视图
答案 0 :(得分:4)
从JSF操作方法返回的字符串必须表示JSF视图ID,如下所示:
public String submit() {
// ...
return "/some.xhtml";
}
或真正的重定向(您的所有案例都是simple forwards, not redirects,也可以使用此术语):
public String submit() {
// ...
return "/some.xhtml?faces-redirect=true";
}
或仅null
,这意味着与“返回当前页面”相同:
public String submit() {
// ...
return null;
}
如果您在faces-config.xml
旧JSF 1.x样式中使用导航案例,则返回的字符串也可以表示导航案例结果。
关于您的具体问题,您在服务器日志中收到以下警告:
Warning: JSF1064: Unable to find or serve resource, /javax.faces.application.FacesMessage@67cc4160.
这意味着JSF无法将/javax.faces.application.FacesMessage@67cc4160
的字符串值识别为有效的JSF视图ID。这基本上意味着您在操作方法中确实如下所示:
public String submit() {
// ...
return "/javax.faces.application.FacesMessage@67cc4160";
}
事实上,你正在以下几行中做到这一点!
} else {
//Wrong pass
// System.out.println( "Sifra je = " + kor.getPassword());
msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Pogrešna šifra.");
return msg.toString();
}
} else {
//Wrog user
msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Korisnik sa datim korisničkim imenom ne postoji.");
return msg.toString();
}
这不是通过添加到上下文的faces消息返回当前视图的正确方法。如下所示更换返回行:
context.addMessage(null, msg);
return null;