loky23
12-07-2010, 10:12
Salve a tutti,
sono appena entrato nel mondo Javaserver faces.. sto leggendo il libro "core javaserver faces 3rd edition" e sto cercando di riprodurre gli esempi riportati sul libro. Il libro consiglia di salvare messaggi e label delle varie pagine in un file a parte. Il mio problema è che questi messaggi non vengono poi visualizzati sulla mia pagina quando faccio girare l'applicazione. Non credo che si tratti di un errore di sintassi poichè i file sono quelli del libro: io li ho solo importati. Forse un errore nei percorsi.. nn so proprio dove mettere le mani.. Ogni suggerimento è ben accetto! Io sto programmando con Eclipse Java EE IDE for Web Developers - Helios Release, uso jdk1.6.0_20 e apache-tomcat-6.0.26.
Allego un'immagine della directory structure:
http://dl.dropbox.com/u/4145197/Directory%20structure.jpg
ed il codice dei vari file:
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_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
</web-app>
faces-config.xml:
<?xml version="1.0"?>
<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_0.xsd"
version="2.0">
<application>
<resource-bundle>
<base-name>com.corejsf.messages</base-name>
<var>msgs</var>
</resource-bundle>
</application>
</faces-config>
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:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>#{msgs.windowTitle}</title>
<h:outputStylesheet library="css" name="styles.css"/>
</h:head>
<h:body>
<h:form>
<h:outputText value="#{msgs.greeting}" styleClass="emphasis"/>
<br/>
<h:messages errorClass="errors" layout="table"/>
<h:panelGrid columns="3">
#{msgs.namePrompt}:
<h:inputText id="name" value="#{user.name}" required="true"
label="#{msgs.namePrompt}"/>
<h:message for="name" errorClass="errors"/>
#{msgs.agePrompt}:
<h:inputText id="age" value="#{user.age}" required="true"
size="3" label="#{msgs.agePrompt}"/>
<h:message for="age" errorClass="errors"/>
</h:panelGrid>
<h:commandButton value="#{msgs.submitPrompt}"/>
</h:form>
</h:body>
</html>
styles.css:
.errors {
font-style: italic;
color: red;
}
.emphasis {
font-size: 1.3em;
}
UserBean.java:
package com.corejsf;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
// or import javax.inject.Named;
import javax.faces.bean.SessionScoped;
// or import javax.enterprise.context.SessionScoped;
@ManagedBean(name="user") // or @Named("user")
@SessionScoped
public class UserBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private int age;
public String getName() { return name; }
public void setName(String newValue) { name = newValue; }
public int getAge() { return age; }
public void setAge(int newValue) { age = newValue; }
}
messages.properties:
windowTitle=Using h:messages and h:message
greeting=Please fill out the following information
namePrompt=Name
agePrompt=Age
submitPrompt=Submit form
Se può essere utile qui trovate i files dell'esempio scaricati direttamente dal sito del libro:
http://dl.dropbox.com/u/4145197/messages.zip
Questo è il risultato che ottengo quando faccio girare l'applicazione e mi collego all'URL: http://localhost:8080/messaggi/index.xhtml
http://dl.dropbox.com/u/4145197/index.jpg
Come potete vedere compare il codice, ma non i messaggi e neanche i controlli (h:inputText). Non capisco proprio dove sbaglio.. sono abbastanza sicuro che è un errore stupido! Grazie a tutti per i consigli che, sono sicuro, saprete darmi e scusate se mi sono dilungato..
sono appena entrato nel mondo Javaserver faces.. sto leggendo il libro "core javaserver faces 3rd edition" e sto cercando di riprodurre gli esempi riportati sul libro. Il libro consiglia di salvare messaggi e label delle varie pagine in un file a parte. Il mio problema è che questi messaggi non vengono poi visualizzati sulla mia pagina quando faccio girare l'applicazione. Non credo che si tratti di un errore di sintassi poichè i file sono quelli del libro: io li ho solo importati. Forse un errore nei percorsi.. nn so proprio dove mettere le mani.. Ogni suggerimento è ben accetto! Io sto programmando con Eclipse Java EE IDE for Web Developers - Helios Release, uso jdk1.6.0_20 e apache-tomcat-6.0.26.
Allego un'immagine della directory structure:
http://dl.dropbox.com/u/4145197/Directory%20structure.jpg
ed il codice dei vari file:
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_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
</web-app>
faces-config.xml:
<?xml version="1.0"?>
<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_0.xsd"
version="2.0">
<application>
<resource-bundle>
<base-name>com.corejsf.messages</base-name>
<var>msgs</var>
</resource-bundle>
</application>
</faces-config>
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:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>#{msgs.windowTitle}</title>
<h:outputStylesheet library="css" name="styles.css"/>
</h:head>
<h:body>
<h:form>
<h:outputText value="#{msgs.greeting}" styleClass="emphasis"/>
<br/>
<h:messages errorClass="errors" layout="table"/>
<h:panelGrid columns="3">
#{msgs.namePrompt}:
<h:inputText id="name" value="#{user.name}" required="true"
label="#{msgs.namePrompt}"/>
<h:message for="name" errorClass="errors"/>
#{msgs.agePrompt}:
<h:inputText id="age" value="#{user.age}" required="true"
size="3" label="#{msgs.agePrompt}"/>
<h:message for="age" errorClass="errors"/>
</h:panelGrid>
<h:commandButton value="#{msgs.submitPrompt}"/>
</h:form>
</h:body>
</html>
styles.css:
.errors {
font-style: italic;
color: red;
}
.emphasis {
font-size: 1.3em;
}
UserBean.java:
package com.corejsf;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
// or import javax.inject.Named;
import javax.faces.bean.SessionScoped;
// or import javax.enterprise.context.SessionScoped;
@ManagedBean(name="user") // or @Named("user")
@SessionScoped
public class UserBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private int age;
public String getName() { return name; }
public void setName(String newValue) { name = newValue; }
public int getAge() { return age; }
public void setAge(int newValue) { age = newValue; }
}
messages.properties:
windowTitle=Using h:messages and h:message
greeting=Please fill out the following information
namePrompt=Name
agePrompt=Age
submitPrompt=Submit form
Se può essere utile qui trovate i files dell'esempio scaricati direttamente dal sito del libro:
http://dl.dropbox.com/u/4145197/messages.zip
Questo è il risultato che ottengo quando faccio girare l'applicazione e mi collego all'URL: http://localhost:8080/messaggi/index.xhtml
http://dl.dropbox.com/u/4145197/index.jpg
Come potete vedere compare il codice, ma non i messaggi e neanche i controlli (h:inputText). Non capisco proprio dove sbaglio.. sono abbastanza sicuro che è un errore stupido! Grazie a tutti per i consigli che, sono sicuro, saprete darmi e scusate se mi sono dilungato..