1
jsf를 primeface로 사용하여 웹 응용 프로그램을 개발 중이며 한 번의 로그인 작업과 2 개의 xhtml 페이지를 사용했습니다. 내가 로그인했을 때 액션 클래스를 호출하고 다음 xhtml 페이지로 돌아 오지 않습니다jsf를 사용하여 다음 페이지로 리디렉션
페이지를 리디렉션하는 중 문제가 발생했습니다. primefaces 3.2 버전 jar 파일을 사용했고 프로젝트 패싯에 jstl을 사용하여 jsf를 구성했습니다.
내 로그인 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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<body>
<h:form styleClass="loginPanelStyle">
<p:panel id="panel" header="Login">
<p:messages id="msgs"/>
<h:panelGrid columns="3">
<h:outputLabel for="firstname" value="UserName" />
<p:inputText id="firstname" value="#{home.username}" required="true" label="UserName">
<f:validateLength minimum="2" />
</p:inputText>
<p:message for="firstname" display="icon"/>
<h:outputLabel for="surname" value="Password" />
<p:inputText id="surname" value="#{home.password}" label="Password" required="true">
<f:validateLength minimum="2" />
<p:ajax update="msgSurname" event="keyup" />
</p:inputText>
<p:message for="surname" id="msgSurname" display="icon"/>
</h:panelGrid>
<p:commandButton id="btn" value="Login" update="panel" actionListener="#{home.validateUser}"/>
</p:panel>
</h:form>
</body>
</html>
내 액션 클래스
package com.cation.action;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import logon.Users;
public class LoginAction {
private String username;
private String password;
private static final String PERSISTENCE_UNIT_NAME = "Cation";
private static EntityManagerFactory factory;
@SuppressWarnings("unchecked")
public String validateUser() throws Exception {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
// Read the existing entries and write to console
Query q = em.createQuery("SELECT u FROM Users u where u.Login='"+username+"'");
List<Users> userList = q.getResultList();
Users user = (Users) userList.get(0);
if(user == null){
return "error";
}
return "home_page";
}
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;
}
}
<?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_2_5.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>Cation</display-name>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<welcome-file-list>
<welcome-file>faces/login.xhtml</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<description>
This parameter tells MyFaces if javascript code should be allowed in
the rendered HTML output.
If javascript is allowed, command_link anchors will have javascript code
that submits the corresponding form.
If javascript is not allowed, the state saving info and nested parameters
will be added as url parameters.
Default is 'true'</description>
<param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<description>
If true, rendered HTML code will be formatted, so that it is 'human-readable'
i.e. additional line separators and whitespace will be written, that do not
influence the HTML code.
Default is 'true'</description>
<param-name>org.apache.myfaces.PRETTY_HTML</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<description>
If true, a javascript function will be rendered that is able to restore the
former vertical scroll on every request. Convenient feature if you have pages
with long lists and you do not want the browser page to always jump to the top
if you trigger a link or button action that stays on the same page.
Default is 'false'
</description>
<param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
<param-value>true</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>
<url-pattern>*.jsf</url-pattern>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
내 web.xml 파일 내 얼굴-설정 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_1_2.xsd"
version="1.2">
<navigation-rule>
<navigation-case>
<from-outcome>login</from-outcome>
<to-view-id>/login.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<managed-bean>
<managed-bean-name>home</managed-bean-name>
<managed-bean-class>com.cation.action.LoginAction</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<navigation-case>
<from-outcome>home_page</from-outcome>
<to-view-id>/pages/homePage.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<managed-bean>
<managed-bean-name>user</managed-bean-name>
<managed-bean-class>com.cation.action.UserAction</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<navigation-case>
<from-outcome>addUser</from-outcome>
<to-view-id>/pages/addUser.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<navigation-case>
<from-outcome>successUser</from-outcome>
<to-view-id>/pages/success.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
어느 한 날