java - JSF 在 XHTML 中保留参数

标签 java jsf xhtml javabeans facelets

如何在 XHMTL 文件中保留参数。

例如我想保留这样的参数

...参数名称=“名称”值=“约翰”....

然后另一行我想用调用名称参数来打印它。

我想做这个。 我在多次调用 session 范围 bean 时遇到问题。

我就这样解决。

登录 Bean 在这里:

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

XML 部分在这里:

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

最佳答案

可能有更好的方法,但你可以尝试 JSTL 标签,如 <c:set name="name" value="john"/> .

但是请注意,您需要记住何时评估 JSTL 标记处理程序以及何时启动 JSF。下面的一篇文章应该可以帮助您开始使用: https://rogerkeays.com/jsf-c-foreach-vs-ui-repeat

此外,您可能想告诉我们为什么不能多次调用该 bean。您的设计可能存在可以解决的问题。例如,尽量不要在 getter 中计算事物,因为它们可能会被多次调用,即使对于页面上的单个表达式也是如此。将这些计算/日期检索移至仅执行一次的操作。或者使用标志来检查数据是否已被计算/检索。

关于java - JSF 在 XHTML 中保留参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8735751/

相关文章:

java - @Sql 不起作用 @BeforeTransaction Spring 测试注解

java - 将二进制文件转换为音频文件

java - 如何使用<h :commandButton action ="#{method here}"/>?

javascript - 使用Javascript引用具有相同ID的许多元素

javascript - 编写 HTML/CSS/JS 选项卡导航系统的最佳方式(无图像)

java - 验证 void 方法调用

java - 如何在 RecyclerView 中保存 "PDF "文件?

java - JSF 2.1 Web 应用程序上的 Spring Security

JSF 2.0 不渲染任何页面

javascript - 应用键后捕获 HTML 文本输入键?