Witam,
Próbuje wykorzystać PrettyFaces do kontroli nad wyglądem URL (RESTful url) w aplikacji PrimeFaces (JSF 2.2).
Aplikacja składa się z dwóch widoków:
- index.xhtml, na którym podaje kategorię, która jest potem parametrem w następnym widoku.
- Po przejściu do następnego widoku chcę, aby użytkownik widział zamiast:
/helloPrettyFaces/dispCategory.jsf?cat=nazwa_kategorii
następujący adres:
/helloPrettyFaces/dispCategory/nazwa_kategorii - Po powrocie do głównego widoku chcę, aby zamiast:
/helloPrettyFaces/index.jsf
widział:
/helloPrettyFaces/index
Główny widok (CategoryAdd) pobiera daną z formularza, a następnie tworzy query string i wykonuje redirect. Niestety, nie widać efektu zmian.
Załączam archiwum z kodem z małym projektem.
Utworzyłem plik pretty-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<pretty-config xmlns="http://ocpsoft.com/prettyfaces/3.3.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ocpsoft.com/prettyfaces/3.3.3 http://ocpsoft.com/xml/ns/prettyfaces/ocpsoft-pretty-faces-3.3.3.xsd">
<url-mapping id="categoryAdd">
<pattern value="/index" />
<view-id value="/index.jsf" />
</url-mapping>
<url-mapping id="categoryDisplay">
<pattern value="/categoryDisplay/{#{cat}}" />
<view-id value="/categoryDisplay.jsf" />
</url-mapping>
</pretty-config>
Konfiguracja w web.xml:
<?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>*.jsf</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsf</welcome-file>
</welcome-file-list>
</web-app>
Managed Bean dla indeksu:
package pl.test.helloprettyfaces;
import java.io.IOException;
import java.io.Serializable;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import org.omnifaces.util.Faces;
@Named("categoryAdd")
@ViewScoped
public class CategoryAdd implements Serializable {
private String name;
/*
* actions
*/
public void redirectView() throws IOException {
Faces.redirect("dispCategory.jsf?cat=%s", name);
}
/*
* getters and setters
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Managed Bean dla rezulatu:
package pl.test.helloprettyfaces;
import java.io.IOException;
import java.io.Serializable;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import org.omnifaces.util.Faces;
@Named("categoryDisplay")
@ViewScoped
public class CategoryDisplay implements Serializable {
private String category = "NOT SUPPORTED YET";
/*
* actions
*/
public void redirectIndex() throws IOException {
Faces.redirect("index.jsf");
}
/*
* getters and setters
*/
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}
Widok dla indeksu (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"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
Hello World: test PrettyFaces z PrimeFaces
<h:form id="catInpForm">
<p:growl autoUpdate="true"/>
<p:panelGrid columns="2">
<h:outputText value="Category name:" />
<p:inputText value="#{categoryAdd.name}"
id="categoryName"
required="true" />
</p:panelGrid>
<p:commandButton value="Wyświetl"
action="#{categoryAdd.redirectView()}" />
</h:form>
</h:body>
</html>
Widok dla rezulatu (dispcategory.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"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
Category display
<h:form>
<h:panelGrid columns="2">
<h:outputText value="Loaded category:" />
<h:outputText value="#{categoryDisplay.category}" />
</h:panelGrid>
<p:commandButton value="Index"
action="#{categoryDisplay.redirectIndex()}" />
</h:form>
</h:body>
</html>
Konfiguracja projektu pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pl.test</groupId>
<artifactId>helloPrettyFaces</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>helloPrettyFaces</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.omnifaces</groupId>
<artifactId>omnifaces</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>5.0</version>
</dependency>
<!-- end primefaces -->
<!-- pretty faces -->
<dependency>
<groupId>com.ocpsoft</groupId>
<artifactId>prettyfaces-jsf2</artifactId>
<version>3.3.3</version>
</dependency>
<!-- end pretty faces -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Aplikacja uruchamiana na serwerze Glassifh v4 JDK 1.7. Kod źródłowy do pobrania:
http://www.speedyshare.com/RapGH/helloPrettyFaces.zip
Dziękuję za każdą wskazówkę.