Empio di utilizzo della classe Locale con JSF

Forum relativo ai framework per l'utilizzo della tecnologia JSP

Empio di utilizzo della classe Locale con JSF

Messaggioda alesslud » mar set 29, 2009 3:39 pm

Il seguente esempio illustra come utilizzare la classe java.util.Locale con JSF, rendendo così le applicazioni multilingua.
Allegati
faceslocale.zip
Testato con Eclipse Galileo
(13.24 KiB) Scaricato 390 volte
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

index.jsp

Messaggioda alesslud » mar set 29, 2009 3:48 pm

<html>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
<head>
<link href="styles.css" rel="stylesheet" type="text/css"/>
<f:loadBundle basename="messages" var="msgs"/>
<title>
<h:outputText value="#{msgs.title}"/>
</title>
</head>
<body>
<h:form>
<table>
<tr>
<td>
<h:commandLink immediate="true"
action="#{localeChanger.italianAction}">
<h:graphicImage value="/ico_flags_ita.gif"
style="border: 0px"/>
</h:commandLink>
</td>
<td>
<h:commandLink immediate="true"
action="#{localeChanger.englishAction}">
<h:graphicImage value="/ico_flags_eng.gif"
style="border: 0px"/>
</h:commandLink>
</td>
</tr>
</table>
<p>
<h:outputText value="#{msgs.indexPageTitle}"
style="font-style: italic; font-size: 1.3em"/>
</p>
<table>
<tr>
<td width="250">
<h:outputText value="#{msgs.nomePrompt}"/>
</td>
<td width="200">
<h:inputText value="#{utente.nome}"/>
</td>
</tr>
<tr>
<td>
<h:outputText value="#{msgs.passwordPrompt}"/>
</td>
<td>
<h:inputSecret value="#{utente.password}"/>
</td>
</tr>
<tr>
<td style="vertical-align: top">
<h:outputText value="#{msgs.aboutYou}"/>
</td>
<td>
<h:inputTextarea value="#{utente.aboutYourself}" rows="5"
cols="35"/>
</td>
</tr>
<tr>
<td>
<h:commandButton value="#{msgs.submit}"
action="grazie"/>
</td>
</tr>
</table>
</h:form>
</body>
</f:view>
</html>
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

grazie.jsp

Messaggioda alesslud » mar set 29, 2009 3:48 pm

<html>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:view>
<head>
<f:loadBundle basename="messages" var="resources"/>
<title>
<h:outputText value="#{resources.grazieWindowTitle}"/>
</title>
</head>

<body>
<h:outputText value="#{resources.nomePrompt}"
style="font-style: italic"/>
<h:outputText value="#{utente.nome}"/>
<p>
<h:outputText value="#{resources.aboutYourselfPrompt}"
style="font-style: italic"/>
<p>
<pre><h:outputText value="#{utente.aboutYourself}"/></pre>
</body>
</f:view>
</html>
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

web.xml

Messaggioda alesslud » mar set 29, 2009 3:49 pm

<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<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>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

faces-config.xml

Messaggioda alesslud » mar set 29, 2009 3:49 pm

<?xml version="1.0"?>

<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
"http://java.sun.com/dtd/web-facesconfig_1_0.dtd">

<faces-config>

<navigation-rule>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>grazie</from-outcome>
<to-view-id>/grazie.jsp</to-view-id>
</navigation-case>
</navigation-rule>

<managed-bean>
<managed-bean-name>localeChanger</managed-bean-name>
<managed-bean-class>com.corejsf.ChangeLocaleBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

<managed-bean>
<managed-bean-name>utente</managed-bean-name>
<managed-bean-class>com.corejsf.Utente</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

</faces-config>
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

messages_it.properties

Messaggioda alesslud » mar set 29, 2009 3:50 pm

title=Usare il Tag Command Link
grazieWindowTitle=Grazie per averci inviato le informazioni
graziePageTitle=Grazie!
indexPageTitle=Per favore, inserisca le seguenti informazioni personali.
nomePrompt=Nome:
passwordPrompt=Password:
aboutYou=Ci parli di lei:
aboutYourselfPrompt=Alcune informazioni su di lei:
submit=Invia
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

messages_en.properties

Messaggioda alesslud » mar set 29, 2009 3:51 pm

title=Command Links with Locale
grazieWindowTitle=Thank you for submitting your information
graziePageTitle=Thank you!
indexPageTitle=Please enter the following personal information
nomePrompt=Name:
passwordPrompt=Password:
aboutYou=Please tell us about yourself:
aboutYourselfPrompt=Some information about you:
submit=Submit your information
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

Utente.java

Messaggioda alesslud » mar set 29, 2009 3:51 pm

package com.corejsf;

public class Utente {
private String nome;
private String password;
private String aboutYourself;

public String getNome() {
return nome;
}

public void setNome(String newValue) {
nome = newValue;
}

public String getPassword() {
return password;
}

public void setPassword(String newValue) {
password = newValue;
}

public String getAboutYourself() {
return aboutYourself;
}

public void setAboutYourself(String newValue) {
aboutYourself = newValue;
}
}
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

ChangeLocaleBean.java

Messaggioda alesslud » mar set 29, 2009 3:51 pm

package com.corejsf;

import java.util.Locale;
import javax.faces.context.FacesContext;

public class ChangeLocaleBean {
public String italianAction() {
FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().setLocale(Locale.ITALIAN);
return null;
}

public String englishAction() {
FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().setLocale(Locale.ENGLISH);
return null;
}
}
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

Re: Empio di utilizzo della classe Locale con JSF

Messaggioda alesslud » mer set 30, 2009 12:16 pm

La classe precedente ha return null; Questo significa che la pagina ricaricata è la stessa di quella che ha eseguito il metodo.
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am


Torna a JSF e Struts

Chi c’è in linea

Visitano il forum: Nessuno e 1 ospite

cron