------------------ create obj Foo class ------------------ html
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 class java
package 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();
}
} web
---------------- fake DB as test purpurse ------------------- session
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"));
}
} app
--------------- bean ----------------- webapp
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()));
}
}
} jsp
--------------- web.xml ----------------- nicht viel gemacht nur add the session bean config ide
<?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> ui
-------------------- faces-config.xml ---------------------------- this
<?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 ---------------------
<%@page contentType="text/html"%>
<% @page pageEncoding="UTF-8"%></f:view>
------------ run ---------------