Dar*_*lar 1 jsf redirect primefaces jsf-2
我是 jsf 的新手,正在尝试构建应用程序。问题是当我单击登录命令按钮时没有任何反应。所以我输入了一些打印行,它告诉我与数据库的连接存在并且参数有效但它不会重定向。
这是 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>
Run Code Online (Sandbox Code Playgroud)
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";
}
}
Run Code Online (Sandbox Code Playgroud)
登录豆
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";
}
}
Run Code Online (Sandbox Code Playgroud)
人脸配置
<?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>
Run Code Online (Sandbox Code Playgroud)
和网络
<?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>
Run Code Online (Sandbox Code Playgroud)
单击按钮时,它会从 LoginBean 调用函数 loadUser,然后从 NavigationBean 调用重定向,并且应该根据用户的状态将我重定向到特定页面,但没有任何反应。此外,当我输入错误的数据时,它会显示给我
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.
Run Code Online (Sandbox Code Playgroud)
不知道这是否与此有关。
我使用的是 glassfish 服务器 4.1、primefaces 5.0、wamp、netbeans 8.0.2、jsf 2.2
如果需要,这里是树视图
从 JSF 操作方法返回的字符串必须表示一个 JSF 视图 ID,如下所示:
public String submit() {
// ...
return "/some.xhtml";
}
Run Code Online (Sandbox Code Playgroud)
或者一个真正的重定向(你所有的情况都是简单的转发,而不是重定向,这个术语也正确):
public String submit() {
// ...
return "/some.xhtml?faces-redirect=true";
}
Run Code Online (Sandbox Code Playgroud)
或者只是null
这意味着与“返回当前页面”相同:
public String submit() {
// ...
return null;
}
Run Code Online (Sandbox Code Playgroud)
如果你在使用导航案例 faces-config.xml
旧 JSF 1.x 样式的,则返回的字符串也可以表示导航案例结果。
至于您的具体问题,您在服务器日志中收到以下警告:
Warning: JSF1064: Unable to find or serve resource, /javax.faces.application.FacesMessage@67cc4160.
Run Code Online (Sandbox Code Playgroud)
这意味着 JSF 没有将 的字符串值识别/javax.faces.application.FacesMessage@67cc4160
为有效的 JSF 视图 ID。这基本上意味着您在操作方法中做了如下操作:
public String submit() {
// ...
return "/javax.faces.application.FacesMessage@67cc4160";
}
Run Code Online (Sandbox Code Playgroud)
事实上,您在以下几行中正是这样做的!
} 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();
}
Run Code Online (Sandbox Code Playgroud)
这不是返回带有添加到上下文中的人脸消息的当前视图的正确方法。更换回线如下:
context.addMessage(null, msg);
return null;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1697 次 |
最近记录: |