博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jsf 1.2 selelctOneMenu from object
阅读量:6588 次
发布时间:2019-06-24

本文共 7444 字,大约阅读时间需要 24 分钟。

hot3.png

------------------ 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 class

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();
    }
}

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

转载于:https://my.oschina.net/u/1248373/blog/182800

你可能感兴趣的文章
(EM算法)The EM Algorithm
查看>>
grep用法
查看>>
top
查看>>
HTTP与HTTPS简介
查看>>
rhcsa第二天笔记
查看>>
一则 gfs2 集群文件系统无法挂载的解决案例
查看>>
曾经的你
查看>>
ArrayList的subList方法
查看>>
想当好员工,想加薪,想提高的最起码应该注意的几项工作习惯
查看>>
经典SQL语句大全(转)
查看>>
linux下repair filesystem模式修复方法
查看>>
Oracle从非归档模式变成归档模式
查看>>
互联网时代: 从Uber的供需匹配看开发需求
查看>>
在CentOS 6.7部署wordpress博客系统Discuz论坛系统
查看>>
中兴U880刷机
查看>>
Spring4学习笔记-泛型依赖注入
查看>>
OSPF的虚拟链路
查看>>
AngularJS 字符串
查看>>
thinkphp-条件判断-范围判断-in
查看>>
log.py——打印出独立IP,并统计独立IP数
查看>>