f:param, f:attribute, f:setPropertyActionListener tutorial

Forum relativo ai framework per l'utilizzo della tecnologia JSP

f:param, f:attribute, f:setPropertyActionListener tutorial

Messaggioda alesslud » mer ott 28, 2009 11:05 am

Un esempio sull'utilizzo dei tags JSF f:param, f:attribute, f:setPropertyActionListener.

L'esempio, oltre ad impostare la lingua dell'applicazione, indica come utilizzare i tags sopra riportati.
Allegati
facesparamattributeset.zip
Testato con Eclipse Galileo, Web Tools e JBoss 5
(17.18 KiB) Scaricato 417 volte
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

index.jsp

Messaggioda alesslud » mer ott 28, 2009 11:06 am

<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="com.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" />
<f:param id="valore" name="valore" value="valoreValue"></f:param>
<f:setPropertyActionListener target="#{localeChanger.country}"
value="pippo" />
</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:selectOneMenu value="#{localeChanger.country}"
onchange="submit()"
valueChangeListener="#{localeChanger.countryChanged}">
<f:attribute name="attributo" value="valoreAttributoValue"></f:attribute>
<f:selectItems value="#{localeChanger.countryNames}" />
</h:selectOneMenu></td>
</tr>
<tr>
<td><h:commandButton value="#{msgs.submit}"
action="#{localeChanger.invia}">
</h:commandButton></td>
</tr>
</table>
<p><h:outputText value="#{localeChanger.param}"></h:outputText>"</p>
<p><h:outputText value="#{localeChanger.attribute}"></h:outputText>"</p>
<p><h:outputText value="#{localeChanger.country}"></h:outputText>"</p>
</h:form>
</body>
</f:view>
</html>
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

grazie.jsp

Messaggioda alesslud » mer ott 28, 2009 11:06 am

<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="com.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

styles.css

Messaggioda alesslud » mer ott 28, 2009 11:07 am

.columns {
vertical-align: top;
}
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

web.xml

Messaggioda alesslud » mer ott 28, 2009 11:07 am

<?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 » mer ott 28, 2009 11:07 am

<?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.aquilasiti.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.aquilasiti.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

Utente.java

Messaggioda alesslud » mer ott 28, 2009 11:07 am

package com.aquilasiti;

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

private String param;
private String attribute;
private String myActionListener;

public String getParam() {
return param;
}

public void setParam(String param) {
this.param = param;
}

public String getAttribute() {
return attribute;
}

public void setAttribute(String attribute) {
this.attribute = attribute;
}

public String getMyActionListener() {
return myActionListener;
}

public void setMyActionListener(String myActionListener) {
this.myActionListener = myActionListener;
}

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 » mer ott 28, 2009 11:08 am

package com.aquilasiti;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Locale;
import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

public class ChangeLocaleBean {



public String invia() {
return "grazie";
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

private String country;
private String param;
private String attribute;

public String getParam() {
return param;
}

public void setParam(String param) {
this.param = param;
}

public String getAttribute() {
return attribute;
}

public void setAttribute(String attribute) {
this.attribute = attribute;
}

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

public String englishAction() {
FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().setLocale(Locale.ENGLISH);
return null;
}

private static final String US = "United States";
private static final String ITALIA = "Italia";
private static final String[] COUNTRY_NAMES = { US, ITALIA };
private static ArrayList countryItems = null;

public Collection getCountryNames() {
if (countryItems == null) {
countryItems = new ArrayList();
for (int i = 0; i < COUNTRY_NAMES.length; ++i) {
countryItems.add(new SelectItem(COUNTRY_NAMES[i]));
}
}
return countryItems;
}

public void countryChanged(ValueChangeEvent event) {
FacesContext context = FacesContext.getCurrentInstance();
UIComponent component = event.getComponent();
this.getAttributeCode(component);
if (US.equals((String) event.getNewValue()))
context.getViewRoot().setLocale(Locale.US);
else
context.getViewRoot().setLocale(Locale.ITALY);
}

/**
* riempio il valore del bean con la proprietà settata dalla JSP
*
* @param value
*/
public void actionListenerPippo(String value) {
Utente utente = new Utente();
utente.setMyActionListener(value);
}

private String getParamCode(FacesContext context) {
Map<String, String> params = context.getExternalContext().
getRequestParameterMap();
String value = params.get("valore");
this.param = value;
return value;
}

private String getAttributeCode(UIComponent component) {
Map<String, Object> attrs = component.getAttributes();
String param = (String) attrs.get("attributo");
this.attribute = param;
return param;
}
}
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

messages.properties

Messaggioda alesslud » mer ott 28, 2009 11:08 am

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_it.properties

Messaggioda alesslud » mer ott 28, 2009 11:09 am

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 » mer ott 28, 2009 11:09 am

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

ico_flags_ita

Messaggioda alesslud » mer ott 28, 2009 11:10 am

ico_flags_ita
Allegati
ico_flags_ita.gif
ico_flags_ita.gif (1.42 KiB) Osservato 11400 volte
alesslud
Site Admin
 
Messaggi: 779
Iscritto il: mer nov 08, 2006 8:09 am

ico_flags_eng

Messaggioda alesslud » mer ott 28, 2009 11:11 am

ico_flags_eng
Allegati
ico_flags_eng.gif
ico_flags_eng.gif (1.48 KiB) Osservato 11400 volte
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