Skip to content
Advertisement

JSF Keep Parameter in XHTML

How can ı kep parameter in XHMTL file.

for example ı want to kep parameter like

….param name= “name” value=”john”….

then the other line ı want to print it with calling name parameter.

I want to do this. I have a problem with calling multiple times a sessionscoped bean.

I will solve this way.

the Login Bean is here :

@ManagedBean(name = "UserLogin")
@RequestScoped
public class Login {

@ManagedProperty(value="#{LoggedUser}")
private LoginUser loginUser;
private String username = null;
private String password = null;
private String message = null;
private Boolean isLoggedin = false;
private Boolean isAdmin = false;
private User loggedinUser = null;

public User getLoggedinUser() {
    this.loggedinUser = loginUser.getLoggedUser();
    return loggedinUser;
}

public void setLoggedinUser(User loggedinUser) {
    this.loggedinUser = loggedinUser;
}

public LoginUser getLoginUser() {
    return loginUser;
}

public void setLoginUser(LoginUser loginUser) {
    this.loginUser = loginUser;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public Boolean getIsLoggedin() {
    if(this.loginUser.getLoggedUser() != null){
        this.isLoggedin = true;
    }
    else{
        this.isLoggedin = false;
    }
    return isLoggedin;
}

public void setIsLoggedin(Boolean isLoggedin) {
    this.isLoggedin = isLoggedin;
}

public Boolean getIsAdmin() {
    if(this.loginUser.getLoggedUser() != null){
        if(this.loginUser.getLoggedUser().getAdmin()){
            this.isLoggedin = true;
        }
        else{
            this.isLoggedin = false;
        }
        this.isLoggedin = true;
    }
    else{
        this.isLoggedin = false;
    }
    return isAdmin;
}

public void setIsAdmin(Boolean isAdmin) {
    this.isAdmin = isAdmin;
}

public void doLogin() {
    System.out.println(username+"");
    System.out.println(password+"");
    System.out.println(isLoggedin+"");
    RequestContext context = RequestContext.getCurrentInstance();
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();// static

    Session hiberSession = sessionFactory.openSession();// session a çevir

    ArrayList<User> userList = null;

    try {
        Criteria crt = hiberSession.createCriteria(User.class);
        crt.add(Restrictions.eq("username", this.username));
        crt.add(Restrictions.eq("password", this.password));
        userList = (ArrayList<User>) crt.list();
        if (userList.size() == 0) {
            this.message = "Incorrect username or password!";
            this.isLoggedin = false;
            this.isAdmin = false;
        } else {
            this.loggedinUser = userList.get(0);
            this.loginUser.setLoggedUser(loggedinUser);
            this.isLoggedin = true;
            this.isAdmin = userList.get(0).getAdmin();
            this.message = "";
            FacesContext.getCurrentInstance().addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_INFO,                                     "Welcome,"
                            + this.loggedinUser.getName() + " ("
                            +       this.loggedinUser.getUsername() + ")", ""));
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        hiberSession.flush();
        hiberSession.close();
    }
    context.addCallbackParam("isLoggedIn", isLoggedin);
    context.addCallbackParam("isAdmin", isAdmin);
    context.addPartialUpdateTarget("growl");
}

public void doLogout() {
    this.loginUser.setLoggedUser(null);
    this.isAdmin = false;
    this.isLoggedin = false;
}

public void registerLogin(User user) {
    setUsername(user.getUsername());
    setPassword(user.getPassword());
    doLogin();
}

The XML PART is Here :

    <p:outputPanel id="loginOut">
                                            <p:panel id="loginPanel" header="User Login"        align="center"
                                                rendered="#{LoggedUser.getLoggedUser == null}"
                                                style="text-align:center; margin-bottom:10px;">
                                                <h:form>
                                                    <div style="padding: 6px">

                                                        <div>
                                                            <table style="width: 100%; text-align: left;"
                                                                align="left">
                                                                <tr>
                                                                    <td><h:outputLabel for="loginusername"
                                                                            value="Username : &nbsp;" /></td>
                                                                </tr>
                                                                <tr>
                                                                    <td><p:inputText id="loginusername"
                                                                            value="#{UserLogin.username}" size="26"
                                                                            label="Name and Surname" /> <p:watermark
                                                                            for="loginusername" value="Username" /></td>
                                                                </tr>
                                                                <tr>
                                                                    <td><h:outputLabel for="loginpassword"
                                                                            value="Password : &nbsp;" /></td>
                                                                </tr>
                                                                <tr>
                                                                    <td><p:keyboard id="loginpassword"
                                                                            value="#{UserLogin.password}" size="26"
                                                                            label="Password" password="true" keypadOnly="false" />

                                                                        <p:watermark for="loginpassword" value="Password" /></td>
                                                                </tr>
                                                            </table>
                                                        </div>
                                                        <div>
                                                            <table style="width: 100%; padding-top: 5px;">
                                                                <tr>
                                                                    <td><p:commandButton value="Register"
                                                                            image="ui-icon ui-icon-disk" id="loginLink"
                                                                            onclick="regdlg.show()" title="login">

                                                                        </p:commandButton></td>
                                                                    <td style="text-align: right; padding-right: 5px;">
                                                                        <p:commandButton value="Login"
                                                                            image="ui-icon ui-icon-arrow-1-ne"
                                                                            actionListener="#{UserLogin.doLogin()}"
                                                                            update="userPanelOut, adminPanelOut, loginmessage"
                                                                            oncomplete="handleLogin(xhr, status, args)" />

                                                                    </td>
                                                                </tr>
                                                            </table>
                                                        </div>
                                                        <div
                                                            style="padding-left: 5px; padding-bottom: 6px; padding-top: 6px; text-align: left;">

                                                            <p:commandLink value="I lost my password!"
                                                                onclick="recdlg.show()">
                                                            </p:commandLink>
                                                        </div>
                                                        <div style="padding-bottom: 6px; padding-top: 6px">
                                                            <h:outputText id="loginmessage"
                                                                value="#{UserLogin.message}">  </h:outputText>
                                                        </div>
                                                    </div>
                                                </h:form>
                                            </p:panel>

                                        </p:outputPanel>





                                        <p:outputPanel id="userPanelOut">
                                            <p:panel id="rightUserPanel" header="User Panel"
                                                align="center"
                                                style="text-align:center; margin-bottom:10px;"
                                                rendered="#{LoggedUser.getLoggedUser != null}">
                                                <div style="padding: 6px; height: auto; text-align: left;">
                                                    <ul class="ymenu">
                                                        <h:form>
                                                            <li><p:commandLink value="Edit Profile"
                                                                    onclick="profedlg.show()"
                                                                    style="display: block;text-decoration: none;color: #333;
                                                                    font-size: 14px;padding-left: 18px;padding-right: 18px;
                                                                padding-top: 10px;padding-bottom: 5px;
                                                                height: 20px;background-color: transparent;">

                                                                </p:commandLink></li>
                                                            <h:form>
                                                                <li><h:commandLink value="Logout"
                                                                        action="#{UserLogin.doLogout()}" oncomplate="refreshPage(xhr, status, args)"
                                                                        style="display: block;text-decoration: none;color: #333;
                                                                    font-size: 14px;padding-left: 18px;padding-right: 18px;
                                                                    padding-top: 10px;padding-bottom: 5px;
                                                                    height: 20px;background-color: transparent;">


                                                                        <f:ajax execute="" render="@all" />

                                                                    </h:commandLink></li>
                                                            </h:form>
                                                        </h:form>
                                                    </ul>
                                                </div>
                                            </p:panel>
                                        </p:outputPanel>


                                        <p:outputPanel id="adminPanelOut">

                                            <p:panel id="rightAdminPanel" header="Administration"
                                                align="center"
                                                style="text-align:center; margin-bottom:10px; text-align:center;"
                                                rendered="#{LoggedUser.getLoggedUser != null}">


                                                <div style="padding: 6px; height: auto; text-align: left;">
                                                    <ul class="ymenu">
                                                        <li><a href="edit_user.jsf">Users</a></li>
                                                        <li><a href="edit_category.jsf">Categories</a></li>
                                                        <li><a href="#">Posts</a></li>
                                                        <li><a href="#">Comments</a></li>
                                                        <li><a href="#">General Settings</a></li>

                                                    </ul>
                                                </div>
                                            </p:panel>
                                        </p:outputPanel>

                                        <p:outputPanel id="rightMenuOut">
                                        <p:panel id="rightCategoriesPanel" header="Categories"
                                            align="center"
                                            style="text-align:center; margin-bottom:10px;">
                                            <div style="padding: 6px; height: auto; text-align: left;">
                                                <ul class="ymenu">
                                                    <ui:repeat var="category" value="#{ListCategories.catList}">


                                                        <li><h:outputLink
                                                                value="#{ListCategories.getURL(category)}"
                                                                style="display: block;text-decoration: none;color: #333;
                                                                    font-size: 14px;padding-left: 18px;padding-right: 18px;
                                                                    padding-top: 10px;padding-bottom: 5px;
                                                                    height: 20px;background-color: transparent;">
                                                                <h:outputText value="#{category.name}" />
                                                            </h:outputLink></li>
                                                    </ui:repeat>
                                                    <li><a href="category.jsf?name=Uncategorized">Uncategorized</a></li>

                                                </ul>
                                            </div>
                                        </p:panel>
                                        </p:outputPanel>

Advertisement

Answer

There might be better ways but you could try JSTL tags like <c:set name="name" value="john"/>.

Note, however, that you need to keep in mind when JSTL tag handlers are evaluated and when JSF kicks in. Here’s an article that should help get you started with that: https://rogerkeays.com/jsf-c-foreach-vs-ui-repeat

Additionally, you might want to tell us why you can’t call the bean multiple times. There might be an issue with your design that you could address. For example, try not to caluclate things in getters because they might be called more than once, even for a single expression on a page. Move those calculations/date retrievals to an action that is executed exactly once. Alternatively use a flag to check whether the data has already been calculated/retrieved.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement