gpt4 book ai didi

java - JSF 不更新变量

转载 作者:行者123 更新时间:2023-11-30 11:20:08 25 4
gpt4 key购买 nike

我在 Java Server Faces (JSF) 中实现网页时遇到了问题。首先,我将展示我的代码,仅显示与该问题相关的代码。

从 ProblemDomainModel 中,有类 User:

public class User implements java.io.Serializable{
private String name;

public String getName(){
return name;
}

public void setName(String name){
this.name = name;
}
}

现在是 UserBean 类:

import javax.faces.bean.SessionScoped;
import javax.inject.Named;

@Named("UserB")
@SessionScoped
public class UserBean implements java.io.Serializable{
private User singleUser = new User();

public UserBean(){}

public String logIn() throws Exception{
singleUser = Loader.loadSingleUserOnID(1);
return "mainPage";
}

public User getSingleUser{
return singleUser;
}

public setSingleUser(User singleUser){
this.singleUser = singleUser;
}
}

现在我已经制作了两个 xhtml 页面:

索引.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">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<p>Weclome to the Web Page. Press "Log In" to log in.</p>
<p><h:commandButton value = "Log In" action = "#{UserB.logIn}"/></p>
</h:form>
</h:body>
</html>

主页面.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">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
Welcome back, <h:outputText value="#{UserB.singleUser.name}"/>.
</h:form>
</h:body>
</html>

应用程序启动时,会显示一个带有欢迎文本和“登录”按钮的网页。当按下该按钮时,调用 UserBean 中的 logIn() 方法,并更新 singleUser。静态方法 Loader.loadSingleUserOnID() 根据数据库中的 ID 号从数据库中加载用户。该方法经过测试有效,这里不再赘述。在 Debug模式下运行时,我还看到对象 singleUser 确实得到了更新,名称变量发生了变化。

当用户点击按钮时,他/她应该进入下一页,并说“欢迎回来,John Doe”。 (“John Doe”是名称字符串)。取而代之的是:“欢迎回来,。”

我还对 UserBean 中的其他字符串进行了一些测试。假设我使用一个名为 testString 的字符串(使用 getTestString() 和 setTestString(String testString),初始值为“这是一个骗局”,并且 logIn() 方法应该将其更改为“猫王已经离开大楼!” . 如果我用这个替换原来的输出:“欢迎回来,,应该打印出来的是“欢迎回来,猫王已经离开大楼了!”,而不是我得到“欢迎回来,这是一个骗局。”

正如我所说,当在 Debug模式下运行并读取对象上的值时,值会发生变化。但不知何故,旧值仍保留在系统中,因为它是在网页上打印出来的。我的主要问题是:我做错了什么?(注意:我在本页底部还有第二个问题)

我还将添加 beans.xml、faces-config.xml 和 web.xml 的内容,以防您在那里看到我做错了什么:

beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans 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/beans_1_0.xsd">
</beans>

faces-config.xml:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">

<navigation-rule>
<navigation-case>
<from-outcome>index</from-outcome>
<to-view-id>/index.xhtml</to-view-id>
</navigation-case>
</navigation-rule>

<navigation-rule>
<navigation-case>
<from-outcome>mainPage</from-outcome>
<to-view-id>/mainPage.xhtml</to-view-id>
</navigation-case>
</navigation-rule>

</faces-config>

网络.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</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/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>

所以我再问一次:我做错了什么?如何确保变量保持更改状态,以便在网页上显示?

我还有一个次要问题:什么时候使用@Inject,什么时候不用?

谢谢。

最佳答案

UserBean 类中,您对 CDI @Named 使用了错误的 @SessionScoped 注释。使用

 import javax.enterprise.context.SessionScoped;

欧洲工商管理学院

 import javax.faces.bean.SessionScoped;

关于java - JSF 不更新变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22882823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com