如何从commandButton发送值到java bean来改变ui

时间:2014-03-26 14:49:27

标签: java ajax jsf commandbutton uiinclude

我是jsf的新手。 我正在尝试从commandButton向java bean发送值来改变ui中的src:include并用ajax渲染它所以当我点击commandButton时我可以从页面刷新部分而不加载整个页面 以下是我的代码

\\\\\\\ Bean文件

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named("urls")
@RequestScoped

public class URLPagesBean 
{
    private String urlSRC = "";

    public String getUrlSRC() {
        return urlSRC;
    }

    public void setUrlSRC(String urlSRC) {
        this.urlSRC = urlSRC;
    }

    public String getURL()
    {
        String url = "";
        if(urlSRC == "page1" || urlSRC == "" || urlSRC == null)
        {
            url = "page1.xhtml";
        }
        else if (urlSRC == "page2")
        {
            url = "page2.xhtml";
        }
       return url;
    }
}
/////////////

索引文件

\\\\\\\\\\\\\\\\\\\\\\ 

<h:body >
    <h:panelGroup layout="block" styleClass="mainContentBox">
        <h:panelGroup layout="block" styleClass="mainTopBox">
            <h:panelGroup layout="block" styleClass="logoBox"></h:panelGroup>
        </h:panelGroup>
        <h:form id="subMenuForm">
        <h:panelGroup layout="block" styleClass="mainLiftBox">
                <h:panelGroup id="msgBoard" layout="block" styleClass="mainMenuButtons">Message Board
                    <h:panelGroup layout="block" styleClass="subMenuBox">
                        <h:commandButton id="showMsgBoard" styleClass="subMenuButtonCommand" value="Page1"/>
                    </h:panelGroup>
                </h:panelGroup>
                <h:panelGroup layout="block" styleClass="mainMenuButtons">Registrations Book
                        <h:panelGroup layout="block" styleClass="subMenuBox">
                            <h:commandButton id="registrInternalApprov" styleClass="subMenuButtonCommand" value="Page2">
                                <f:ajax render="mainCenterBox" />
                            </h:commandButton>     
                        </h:panelGroup>
                    </h:panelGroup>
          </h:panelGroup>
            <h:panelGroup id="mainCenterBox" styleClass="mainCenterBox" layout="block">
                    <ui:include  id="centerView" src="#{urls.URL}"/>
            </h:panelGroup>
        </h:form>
    </h:panelGroup>
</h:body>

///////////////////////////////////////

page1.xhtml

\\\\\\\\\\\\\\\\\\\\\
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
    <h:head>
        <title>Page 1</title>
    </h:head>
    <h:body>
        <ui:fragment>
            Page 1
        </ui:fragment>
    </h:body>
</html>
///////////////////////////

page2.xhtml

\\\\\\\\\\\\\\\\\\\\\
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
    <h:head>
        <title>Page 2</title>
    </h:head>
    <h:body>
        <ui:fragment>
            Page 2
        </ui:fragment>
    </h:body>
</html>
///////////////////////////

1 个答案:

答案 0 :(得分:0)

使用action的{​​{1}}属性

h:commandButton