------------------ create obj Foo class ------------------
package com;
public class Foo { private String key; // You can also use any Number type, e.g. Long. private String value; public Foo() { } public Foo(String key, String value) { this.key = key; this.value = value; } public String getKey() { return key; } public String getValue() { return value; } // Setters ------------------------------------------------------------------------------------ public void setKey(String key) { this.key = key; } public void setValue(String value) { this.value = value; } // This must return true for another Foo object with same key/id. public boolean equals(Object other) { return other instanceof Foo && (key != null) ? key.equals(((Foo) other).key) : (other == this); } // This must return the same hashcode for every Foo object with the same key. public int hashCode() { return key != null ? this.getClass().hashCode() + key.hashCode() : super.hashCode(); } // Override Object#toString() so that it returns a human readable String representation. // It is not required by the Converter or so, it just pleases the reading in the logs. public String toString() { return "Foo[" + key + "," + value + "]"; } } ------------------------- create Obj to string and string to Obj classpackage com;
import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; public class FooConverter implements Converter { private static FooDao fooDAO = new FooDao(); public Object getAsObject(FacesContext context, UIComponent component, String value) { // Convert the unique String representation of Foo to the actual Foo object. return fooDAO.find(value); } public String getAsString(FacesContext context, UIComponent component, Object value) { // Convert the Foo object to its unique String representation. return ((Foo) value).getKey(); } }---------------- fake DB as test purpurse -------------------
package com;
import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; public class FooDao { private static Map<String, Foo> fooMap; static { loadFooMap(); // Preload the fake database. } public Foo find(String key) { return fooMap.get(key); } public List<Foo> list() { return new ArrayList<Foo>(fooMap.values()); } public Map<String, Foo> map() { return fooMap; } private static void loadFooMap() { // This is just a fake database. We're using LinkedHashMap as it maintains the ordering. fooMap = new LinkedHashMap<String, Foo>(); fooMap.put("fooKey1", new Foo("fooKey1", "fooValue1")); fooMap.put("fooKey2", new Foo("fooKey2", "fooValue2")); fooMap.put("fooKey3", new Foo("fooKey3", "fooValue3")); } }--------------- bean -----------------
package bean;
import com.Foo; import com.FooConverter; import com.FooDao; import java.util.ArrayList; import java.util.List; import javax.faces.model.SelectItem; public class MyBean { private static FooDao fooDAO = new FooDao(); private List<SelectItem> selectItems; private Foo selectedItem; private String ss=""; private boolean isSubmit=false; public boolean isIsSubmit() { return isSubmit; } public void setIsSubmit(boolean isSubmit) { this.isSubmit = isSubmit; } public String getSs() { return ss; } public void setSs(String ss) { this.ss = ss; } { fillSelectItems(); } // Actions ------------------------------------------------------------------------------------ public String action2() { this.ss = selectedItem.getValue(); System.out.println("Selected Foo item: " + selectedItem); this.isSubmit = true; return "welcomeJSF"; } // Getters ------------------------------------------------------------------------------------ public List<SelectItem> getSelectItems() { return selectItems; } public Foo getSelectedItem() { return selectedItem; } // Setters ------------------------------------------------------------------------------------ public void setSelectedItem(Foo selectedItem) { this.selectedItem = selectedItem; } // Helpers ------------------------------------------------------------------------------------ private void fillSelectItems() { selectItems = new ArrayList<SelectItem>(); for (Foo foo : fooDAO.list()) { selectItems.add(new SelectItem(foo, foo.getValue())); } } }--------------- web.xml ----------------- nicht viel gemacht nur add the session bean config
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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-app_2_5.xsd"> <context-param> <param-name>com.sun.faces.verifyObjects</param-name> <param-value>false</param-value> </context-param> <context-param> <param-name>com.sun.faces.validateXml</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>client</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> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>faces/welcomeJSF.jsp</welcome-file> </welcome-file-list> </web-app>-------------------- faces-config.xml ----------------------------
<?xml version='1.0' encoding='UTF-8'?>
<!-- =========== FULL CONFIGURATION FILE ================================== --> <faces-config version="1.2" 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"> <converter> <converter-id>fooConverter</converter-id> <converter-class>com.FooConverter</converter-class> </converter> <managed-bean> <managed-bean-name>myBean</managed-bean-name> <managed-bean-class>bean.MyBean</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean> <navigation-rule> <from-view-id>/welcomeJSF.jsp</from-view-id> <navigation-case> <from-outcome>case1</from-outcome> <to-view-id>/welcomeJSF.jsp</to-view-id> </navigation-case> </navigation-rule> </faces-config>------------------- view ---------------------
<% contentType="text/html"%>
<% pageEncoding="UTF-8"%> <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%> <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%-- This file is an entry point for JavaServer Faces application. --%> <f:view> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1><h:outputText value="select menu from string" /></h1> <h:form> <h:selectOneMenu value="#{myBean.selectedItem}"> <f:selectItems value="#{myBean.selectItems}" /> <f:converter converterId="fooConverter" /> </h:selectOneMenu> <h:commandButton value="submit" action="#{myBean.action2}" /> <h:messages /> <h:outputText value="submitted" rendered="#{myBean.isSubmit}" /> <br/> <br/> <h:outputText value="#{myBean.selectedItem}" rendered="#{myBean.isSubmit}" > </h:outputText> </h:form> </body> </html></f:view>
------------ run ---------------