这是完整的源$ C $ C: http://www.sendspace.com/file/ lwxpyf
我有一个JSF页面,我解决不了的问题。我有一个用于存储应用程序设置成数据库表中的JSF页面。 这是JSF页面的源代码code:
< XML版本=1.0编码=UTF-8&GT?;
!< DOCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0 Strict标准// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
< HTML的xmlns =http://www.w3.org/1999/xhtmlXML:LANG =ENLANG =EN
的xmlns:H =http://java.sun.com/jsf/html
的xmlns:F =http://java.sun.com/jsf/core
的xmlns:UI =http://java.sun.com/jsf/facelets>
< H:头>
< UI:插入名=标头>
<用户界面:包括SRC =header.xhtml/>
< / UI:插入>
< / H:头>
< H:身体GT;
< H1>< IMG SRC =资源/ CSS /图像/ icon.png在ALT =NVIDIA.com/>设置中心< / H1>
<! - 层的按钮的黑色背景 - >
< DIV ID =工具栏的风格=保证金:0汽车;宽度:1180px;高度:30PX;位置:亲属;背景色:黑色>
<! - 包括页面导航 - >
< UI:插入NAME =导航>
<用户界面:包括SRC =Navigation.xhtml/>
< / UI:插入>
< / DIV>
< DIV ID =greenBand级=UI状态默认UI的角落allh的风格=的位置是:相对;顶端:35px;左:0px;>
< H:graphicImage的ALT =应用程序设置的风格=的位置是:相对;顶端:-20px;左:9px;值=资源/图像/ logo_application_settings.png/>
< / DIV>
< DIV ID =main的风格=保证金:0汽车;宽度:1190px;高度:700像素;位置:绝对的;背景颜色:透明;顶部:105px>
< DIV ID =炫魅的风格=保证金:0汽车;宽度:1190px,高度:500px的;位置:绝对的;背景颜色:透明;顶部:80px>
< H:形式GT;
< DIV ID =settingsdiv的风格=宽度:550px;高度:400像素;位置:绝对的;背景颜色,R;顶:20像素;左:1px的>
&所述; H:panelGrid的列=2>
< H:panelGroup中>用户会话超时< /小时:panelGroup中>
< H:panelGroup中>
< H:selectOneMenu用于值=#{ApplicationController.settings ['SessionTTL']}>
< F:选择信息itemValue =#{ApplicationController.settings ['SessionTTL']}itemLabel =#{ApplicationController.settings ['SessionTTL']}/>
< F:选择信息itemValue =二itemLabel =方案二/>
< F:选择信息itemValue =三化itemLabel =选项三/>
< F:选择信息itemValue =自定义itemLabel =定义自定义值/>
< F:AJAX渲染=输入/>
< / H:selectOneMenu用于>&安培; NBSP;
< H:panelGroup中的id =输入>
< H:inputText的值=#{ApplicationController.settings ['SessionTTL']}呈现=#{ApplicationController.settings ['SessionTTL'] =='定制'}要求=真/>
< /小时:panelGroup中>
< /小时:panelGroup中>
< H:panelGroup中>允许的最大用户和LT; /小时:panelGroup中>
< H:panelGroup中>
< H:selectOneMenu用于值=#{ApplicationController.settings ['MaxUsersActive']}>
< F:选择信息itemValue =#{ApplicationController.settings ['MaxUsersActive']}itemLabel =#{ApplicationController.settings ['MaxUsersActive']}/>
< F:选择信息itemValue =二itemLabel =方案二/>
< F:选择信息itemValue =三化itemLabel =选项三/>
< F:选择信息itemValue =自定义itemLabel =定义自定义值/>
< F:AJAX渲染=inputl/>
< / H:selectOneMenu用于>&安培; NBSP;
&所述; H:panelGroup中的id =inputl>
< H:inputText的值=#{ApplicationController.settings ['MaxUsersActive']}呈现=#{ApplicationController.settings ['MaxUsersActive'] =='定制'}要求=真/>
< /小时:panelGroup中>
< /小时:panelGroup中>
< / H:panelGrid的>
< / DIV>
< DIV ID =settingsonediv的风格=宽度:350像素,高度:400像素;位置:绝对的;背景颜色:透明;顶:20像素;左:x 400像素>
< / DIV>
< DIV ID =settingstwodiv的风格=宽度:150像素,高度:60PX;位置:绝对的;背景颜色:透明;顶:380px;左:800px>
< H:的commandButton值=保存设置行动=#{ApplicationController.updateDBSettings}>
< F:AJAX渲染=@表执行=@形与GT;< / F:AJAX>
< / H:的commandButton>
< / DIV>
< /小时:形式GT;
< / DIV>
< / DIV>
< / H:身体GT;
< / HTML>
这是管理bean:
的Bean;
进口javax.enterprise.context.SessionScoped;
//或进口javax.faces.bean.SessionScoped;
进口javax.inject.Named;
/ *包括SQL包* /
进口java.sql.Connection中;
。进口的java.sql preparedStatement;
进口java.sql.ResultSet中;
进口值java.sql.SQLException;
进口的java.util.HashMap;
进口javax.annotation.PostConstruct;
进口javax.sql.DataSource的;
进口javax.annotation.Resource;
进口javax.faces.context.FacesContext;
进口javax.inject.Inject;
进口javax.servlet.http.HttpServletRequest;
进口的javax.servlet.http.HttpSession;
//或进口javax.faces.bean.ManagedBean;
进口org.glassfish.osgicdi.OSGiService;
@命名(ApplicationController中)
@SessionScoped
公共类应用实现Serializable {
/ *该散列映射将被用于存储设置和值* /
私人的HashMap<字符串,字符串> settingsMap = NULL;
公共应用程序(){
}
/ *调用的Oracle JDBC连接驱动程序* /
@Resource(NAME =JDBC /甲骨文)
私人数据源DS;
/ *散列地图
*发送该散列映射的设置和值的JSF页面
* /
公众的HashMap<字符串,字符串>的getSettings(){
返回settingsMap;
}
/ *获取一个哈希地图与设置和值。该表是genarated权
*后构造函数初始化。
* /
@PostConstruct
公共无效initSettings()抛出的SQLException
{
settingsMap =新的HashMap<字符串,字符串>();
如果(DS == NULL){
抛出新的SQLException(无法获取数据源);
}
/ *初始化到Oracle的连接* /
数Connection conn = ds.getConnection();
如果(康涅狄格州== NULL){
抛出新的SQLException(无法获取数据库连接);
}
/ *在SQL语句中得到的所有设置和值* /
。preparedStatement PS =康恩prepareStatement(SELECT * FROM GLOBALSETTINGS);
尝试
{
//从数据库中的数据
ResultSet的结果= ps.executeQuery();
而(result.next())
{
settingsMap.put(result.getString(SettingName),result.getString(SettingValue));
}
}
最后
{
ps.close();
conn.close();
}
}
/ * JSF返回更新后的值到HashMap中* /
/ *更新设置的值* /
公共无效updateDBSettings()抛出的SQLException {
字符串SQL_Statement = NULL;
如果(DS == NULL)抛出新的SQLException();
数Connection conn = ds.getConnection();
如果(康涅狄格州== NULL)抛出新的SQLException();
尝试 {
conn.setAutoCommit(假);
布尔致力于= FALSE;
尝试 {
/ *将新设置的值与一个SQL语句* /
SQL_Statement =UPDATE GLOBALSETTINGS+
SET \SettingValue \=+
CASE+
当\SettingName \='SessionTTL'呢?+
当\SettingName \='MaxUsersActive'呢?+
END+
WHERE \SettingName \IN('SessionTTL','MaxUsersActive');
/ * Exete THRE SQL语句* /
preparedStatement updateQuery =康恩prepareStatement(SQL_Statement)。
updateQuery.setString(1,settingsMap.get(SessionTTL));
updateQuery.setString(2,settingsMap.get(MaxUsersActive));
updateQuery.executeQuery();
conn.commit();
致力于= TRUE;
} 最后 {
如果conn.rollback()(承诺!);
}
}
最后 {
/ *释放资源的所有SQL查询执行后* /
conn.close();
}
/ *刷新哈希地图
*获取再从甲骨文设置
* /
initSettings();
}
}
这是我面对的问题:
当我打开JSF页面我得到这个菜单:
然后我从菜单中选择自定义的:
我让旁边的菜单中选择此输入字段:
然后我从键盘输入自定义值:
但是,当我点击保存按钮,我得到这个。我可以看到,数据库中的字段被更新,但该值是定制
当我输入相同的值的两倍,我得到这样的:
我看来,当我点击自定义
。我输入自定义值HashMap中没有更新。很奇怪,当我再这样做了HashMap的更新和值是正确的。我该如何解决这个问题呢?
最良好的祝愿
修改的
我怀疑是JSF版本会导致此问题。这是 faces-config.xml中的含量
< XML版本=1.0&GT?;
<脸,配置的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_2_0.xsd
版本=2.0>
<管理豆>
<描述>注释< /描述>
<托管豆名称>的ApplicationController< /托管豆名称>
<管理豆级> com.DX_57.SM_57.Application< /托管的bean类>
<托管豆范围>的要求和LT; /托管豆范围>
< /管理豆>
<管理豆>
<描述>注释< /描述>
<托管豆名称>&DashboardController LT; /托管豆名称>
<管理豆级> com.DX_57.SM_57.Dashboard< /托管的bean类>
<托管豆范围>的要求和LT; /托管豆范围>
< /管理豆>
<管理豆>
<描述>注释< /描述>
<托管豆名称>&DatabaseController LT; /托管豆名称>
<管理豆级> com.DX_57.SM_57.Database< /托管的bean类>
<托管豆范围>的要求和LT; /托管豆范围>
< /管理豆>
<管理豆>
<描述>注释< /描述>
<托管豆名称>&GeneralController LT; /托管豆名称>
<管理豆级> com.DX_57.SM_57.General< /托管的bean类>
<托管豆范围>的要求和LT; /托管豆范围>
< /管理豆>
<管理豆>
<描述>注释< /描述>
<托管豆名称>&GlassfishController LT; /托管豆名称>
<管理豆级> com.DX_57.SM_57.Glassfish< /托管的bean类>
<托管豆范围>的要求和LT; /托管豆范围>
< /管理豆>
<管理豆>
<描述>注释< /描述>
<托管豆名称>&LinuxController LT; /托管豆名称>
<管理豆级> com.DX_57.SM_57.Linux< /托管的bean类>
<托管豆范围>的要求和LT; /托管豆范围>
< /管理豆>
<管理豆>
<描述>注释< /描述>
<托管豆名称>&LinuxConfigurationController LT; /托管豆名称>
<管理豆级> com.DX_57.SM_57.LinuxConfiguration< /托管的bean类>
<托管豆范围>的要求和LT; /托管豆范围>
< /管理豆>
<管理豆>
<描述>注释< /描述>
<托管豆名称>&LogController LT; /托管豆名称>
<管理豆级> com.DX_57.SM_57.Log< /托管的bean类>
<托管豆范围>的要求和LT; /托管豆范围>
< /管理豆>
<管理豆>
<描述>注释< /描述>
<托管豆名称>&OracleController LT; /托管豆名称>
<管理豆级> com.DX_57.SM_57.Oracle< /托管的bean类>
<托管豆范围>的要求和LT; /托管豆范围>
< /管理豆>
<管理豆>
<描述>注释< /描述>
<托管豆名称>&UpdatesController LT; /托管豆名称>
<管理豆级> com.DX_57.SM_57.Updates< /托管的bean类>
<托管豆范围>的要求和LT; /托管豆范围>
< /管理豆>
< /面,配置>
这是的beans.xml
< XML版本=1.0编码=UTF-8&GT?;
<豆类的xmlns =http://java.sun.com/xml/ns/javaee的xmlns:XSI =http://www.w3.org/2001/XMLSchema-instanceXSI:的schemaLocation =HTTP: //java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
< /豆>
这是的web.xml
< XML版本=1.0编码=UTF-8&GT?;
< web-app的版本=3.0的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_3_0.xsd">
<的context-param>
<参数 - 名称>&javax.faces.PROJECT_STAGE LT; /参数 - 名称>
<参数值>发展< /参数值>
< /的context-param>
<的servlet>
< servlet的名称>面的Servlet< / Servlet的名称>
<的servlet类> javax.faces.webapp.FacesServlet< / Servlet的类>
&所述;负载上启动→1&其中; /负载上启动>
< / servlet的>
< Servlet映射>
< servlet的名称>面的Servlet< / Servlet的名称>
< URL模式> * JSF< / URL模式>
< / Servlet映射>
<欢迎-文件列表>
<欢迎-文件>的index.html< /欢迎-文件>
< /欢迎-文件列表>
< / web-app的>
他们应该如何才能使用最新版本的JSF更改?
编辑2在Firefox 我禁用了JavaScript。我测试了code。事实证明,它的作品,但我一定要点击保存按钮每次:
我选择自定义,然后单击保存按钮
当我点击保存按钮自定义字段。
当我输入自定义的值,然后单击保存按钮字段被更新。
我想,这是行不通的,因为数据不会发送回从JavaScrips到服务器
修改3 从萤火虫调试的信息:
我从菜单中选择自定义的:
<更新ID =!j_idt5:输入>< [CDATA [<跨度ID =j_idt5:输入><输入类型=文本 NAME =j_idt5:j_idt15值=自定义/>< / SPAN>]]>< /更新>
我输入自定义的值:
< XML版本=1.0编码=UTF-8&GT?;
<部分响应><修改><更新ID =j_idt5><![CDATA [
<形式ID =j_idt5NAME =j_idt5方法=邮报行动=/ SM_57-1.0-快照/ Application.jsfENCTYPE =应用/的X WWW的形式urlen codeD&GT ;
<输入类型=隐藏名称=j_idt5值=j_idt5/>
<表>
< TBODY>
&其中; TR>
< TD>用户会话超时< / TD>
&其中; TD>&其中;选择的id =j_idt5:j_idt10名称=j_idt5:j_idt10大小=1的onchange =mojarra.ab(此,事件,'valueChange',0,'j_idt5:输入') > <期权价值=自定义选择=选择>自定义< /选项>
<期权价值=二与GT;办法二:LT; /选项>
<期权价值=三化与GT;办法三< /选项>
<期权价值=自定义选择=选择>定义自定义值小于/选项>
< /选择><跨度ID =j_idt5:输入><输入类型=文本名称=j_idt5:j_idt15值=自定义/>< / SPAN>< / TD>
< / TR>
&其中; TR>
< TD>允许的最大用户和LT; / TD>
< / TR>
< / TBODY>
< /表>
<输入ID =j_idt5:j_idt18类型=提交名称=j_idt5:j_idt18值=保存设置的onclick =mojarra.ab(本,事件,行动,@的形式,@表');返回false/>
&LT; /形式GT;]]&GT;&LT; /更新&GT;&LT;更新id="javax.faces.ViewState"><![CDATA[-8356663926661541536:7758149566419150570]]></update></changes></partial-response>
我点击保存按钮:
&LT; XML版本=1.0编码=UTF-8&GT?;
&LT;部分响应&GT;&LT;修改&GT;&LT;更新ID =j_idt5&GT;&LT;![CDATA [
&LT;形式ID =j_idt5NAME =j_idt5方法=邮报行动=/ SM_57-1.0-快照/ Application.jsfENCTYPE =应用/的X WWW的形式urlen codeD&GT ;
&LT;输入类型=隐藏名称=j_idt5值=j_idt5/&GT;
&LT;表&gt;
&LT; TBODY&GT;
&其中; TR&GT;
&LT; TD&gt;用户会话超时&LT; / TD&GT;
&其中; TD&GT;&其中;选择的id =j_idt5:j_idt10名称=j_idt5:j_idt10大小=1的onchange =mojarra.ab(此,事件,'valueChange',0,'j_idt5:输入') &GT; &LT;期权价值=wdwdew选择=选择&GT; wdwdew&LT; /选项&GT;
&LT;期权价值=二与GT;办法二:LT; /选项&GT;
&LT;期权价值=三化与GT;办法三&LT; /选项&GT;
&LT;期权价值=自定义&GT;定义自定义值小于/选项&GT;
&LT; /选择&GT;&LT;跨度ID =j_idt5:输入&GT;&LT; / SPAN&GT;&LT; / TD&GT;
&LT; / TR&GT;
&其中; TR&GT;
&LT; TD&GT;允许的最大用户和LT; / TD&GT;
&LT; / TR&GT;
&LT; / TBODY&GT;
&LT; /表&gt;
&LT;输入ID =j_idt5:j_idt18类型=提交名称=j_idt5:j_idt18值=保存设置的onclick =mojarra.ab(本,事件,行动,@的形式,@表');返回false/&GT;
&LT; /形式GT;]]&GT;&LT; /更新&GT;&LT;更新id="javax.faces.ViewState"><![CDATA[-8356663926661541536:7758149566419150570]]></update></changes></partial-response>
解决方案
您已经redefinied在豆类faces-config.xml中
。您的托管bean被redefinied为请求范围之一:
&LT;管理豆&GT;
&LT;描述&GT;注释&LT; /描述&GT;
&LT;托管豆名称&gt;的ApplicationController&LT; /托管豆名称&gt;
&LT;管理豆级&GT; com.DX_57.SM_57.Application&LT; /托管的bean类&GT;
&LT;托管豆范围&GT;的要求和LT; /托管豆范围&GT;
&LT; /管理豆&GT;
在管理bean的配置faces-config.xml中
文件有 precedence 在注解。你的的ApplicationController
bean是在现实中这样的请求范围的JSF管理的bean,而不是一个会话范围的CDI托管bean一样,你必须通过注解声明。在每一个Ajax请求一个全新的将被创建。在previous要求完成所有变更都遗忘。
您有2个选项:
删除从的bean的配置faces-config.xml中
。
更改托管bean范围视图
(请注意,它就会仍然是JSF是谁管理bean的实例,而不是CDI和CDI没有适用 @ViewScoped
注释)。
This is the complete source code: http://www.sendspace.com/file/lwxpyf
I have a problem with a JSF page which I can't solve. I have a JSF page which is used to store application settings into database table. This is the source code of the JSF page:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<ui:insert name="header">
<ui:include src="header.xhtml"/>
</ui:insert>
</h:head>
<h:body>
<h1><img src="resources/css/images/icon.png" alt="NVIDIA.com" /> Settings Center</h1>
<!-- layer for black background of the buttons -->
<div id="toolbar" style="margin: 0 auto; width:1180px; height:30px; position:relative; background-color:black">
<!-- Include page Navigation -->
<ui:insert name="Navigation">
<ui:include src="Navigation.xhtml"/>
</ui:insert>
</div>
<div id="greenBand" class="ui-state-default ui-corner-allh" style="position:relative; top:35px; left:0px;">
<h:graphicImage alt="Application Settings" style="position:relative; top:-20px; left:9px;" value="resources/images/logo_application_settings.png" />
</div>
<div id="main" style="margin: 0 auto; width:1190px; height:700px; position:absolute; background-color:transparent; top:105px">
<div id="mainpage" style="margin: 0 auto; width:1190px; height:500px; position:absolute; background-color:transparent; top:80px">
<h:form>
<div id="settingsdiv" style="width:550px; height:400px; position:absolute; background-color:r; top:20px; left:1px">
<h:panelGrid columns="2">
<h:panelGroup>User Session Timeout</h:panelGroup>
<h:panelGroup>
<h:selectOneMenu value="#{ApplicationController.settings['SessionTTL']}">
<f:selectItem itemValue="#{ApplicationController.settings['SessionTTL']}" itemLabel="#{ApplicationController.settings['SessionTTL']}" />
<f:selectItem itemValue="two" itemLabel="Option two" />
<f:selectItem itemValue="three" itemLabel="Option three" />
<f:selectItem itemValue="custom" itemLabel="Define custom value" />
<f:ajax render="input" />
</h:selectOneMenu>
<h:panelGroup id="input">
<h:inputText value="#{ApplicationController.settings['SessionTTL']}" rendered="#{ApplicationController.settings['SessionTTL'] == 'custom'}" required="true" />
</h:panelGroup>
</h:panelGroup>
<h:panelGroup>Maximum allowed users</h:panelGroup>
<h:panelGroup>
<h:selectOneMenu value="#{ApplicationController.settings['MaxUsersActive']}">
<f:selectItem itemValue="#{ApplicationController.settings['MaxUsersActive']}" itemLabel="#{ApplicationController.settings['MaxUsersActive']}" />
<f:selectItem itemValue="two" itemLabel="Option two" />
<f:selectItem itemValue="three" itemLabel="Option three" />
<f:selectItem itemValue="custom" itemLabel="Define custom value" />
<f:ajax render="inputl" />
</h:selectOneMenu>
<h:panelGroup id="inputl">
<h:inputText value="#{ApplicationController.settings['MaxUsersActive']}" rendered="#{ApplicationController.settings['MaxUsersActive'] == 'custom'}" required="true" />
</h:panelGroup>
</h:panelGroup>
</h:panelGrid>
</div>
<div id="settingsonediv" style="width:350px; height:400px; position:absolute; background-color:transparent; top:20px; left:400px">
</div>
<div id="settingstwodiv" style="width:150px; height:60px; position:absolute; background-color:transparent; top:380px; left:800px">
<h:commandButton value="Save Settings" action="#{ApplicationController.updateDBSettings}">
<f:ajax render="@form" execute="@form"></f:ajax>
</h:commandButton>
</div>
</h:form>
</div>
</div>
</h:body>
</html>
And this is the managed bean:
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
// or import javax.faces.bean.SessionScoped;
import javax.inject.Named;
/* include SQL Packages */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import javax.annotation.Resource;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
// or import javax.faces.bean.ManagedBean;
import org.glassfish.osgicdi.OSGiService;
@Named("ApplicationController")
@SessionScoped
public class Application implements Serializable {
/* This Hash Map will be used to store setting and value */
private HashMap<String, String> settingsMap = null;
public Application(){
}
/* Call the Oracle JDBC Connection driver */
@Resource(name = "jdbc/Oracle")
private DataSource ds;
/* Hash Map
* Send this hash map with the settings and values to the JSF page
*/
public HashMap<String, String> getsettings(){
return settingsMap;
}
/* Get a Hash Map with settings and values. The table is genarated right
* after the constructor is initialized.
*/
@PostConstruct
public void initSettings() throws SQLException
{
settingsMap = new HashMap<String, String>();
if(ds == null) {
throw new SQLException("Can't get data source");
}
/* Initialize a connection to Oracle */
Connection conn = ds.getConnection();
if(conn == null) {
throw new SQLException("Can't get database connection");
}
/* With SQL statement get all settings and values */
PreparedStatement ps = conn.prepareStatement("SELECT * from GLOBALSETTINGS");
try
{
//get data from database
ResultSet result = ps.executeQuery();
while (result.next())
{
settingsMap.put(result.getString("SettingName"), result.getString("SettingValue"));
}
}
finally
{
ps.close();
conn.close();
}
}
/* JSF returns the updated values into the HashMap */
/* Update Settings Values */
public void updateDBSettings() throws SQLException {
String SQL_Statement = null;
if (ds == null) throw new SQLException();
Connection conn = ds.getConnection();
if (conn == null) throw new SQLException();
try {
conn.setAutoCommit(false);
boolean committed = false;
try {
/* Insert the new settings values with one SQL statement */
SQL_Statement = "UPDATE GLOBALSETTINGS " +
"SET \"SettingValue\" = " +
"CASE " +
"WHEN \"SettingName\" = 'SessionTTL' THEN ? " +
"WHEN \"SettingName\" = 'MaxUsersActive' THEN ? " +
"END " +
"WHERE \"SettingName\" IN ('SessionTTL', 'MaxUsersActive')";
/* Exete thre SQL statement */
PreparedStatement updateQuery = conn.prepareStatement(SQL_Statement);
updateQuery.setString(1, settingsMap.get("SessionTTL"));
updateQuery.setString(2, settingsMap.get("MaxUsersActive"));
updateQuery.executeQuery();
conn.commit();
committed = true;
} finally {
if (!committed) conn.rollback();
}
}
finally {
/* Release the resource after all SQL queries are executed */
conn.close();
}
/* Refresh Hash Map
* Get again settings from Oracle
*/
initSettings();
}
}
This is the problem that I face:
When I open the JSF page I get this menus:
Then I select custom from the menu:
I get this input field next to the menu:
Then I enter custom value from the keyboard:
But when I click Save button I get this. I can see that the database field is updated but the values is "custom"
When I enter the same twice value I get this:
I seems that when I click custom
and I enter the custom value the HashMap is not updated. It's strange that when I do it again the HashMap is updated and the value is correct. How I can fix this problem?
Best Wishes
EDIT
I suspect that the JSF version causes this problem. This is the content of the faces-config.xml
<?xml version="1.0"?>
<faces-config 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_2_0.xsd"
version="2.0">
<managed-bean>
<description>comment</description>
<managed-bean-name>ApplicationController</managed-bean-name>
<managed-bean-class>com.DX_57.SM_57.Application</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<description>comment</description>
<managed-bean-name>DashboardController</managed-bean-name>
<managed-bean-class>com.DX_57.SM_57.Dashboard</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<description>comment</description>
<managed-bean-name>DatabaseController</managed-bean-name>
<managed-bean-class>com.DX_57.SM_57.Database</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<description>comment</description>
<managed-bean-name>GeneralController</managed-bean-name>
<managed-bean-class>com.DX_57.SM_57.General</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<description>comment</description>
<managed-bean-name>GlassfishController</managed-bean-name>
<managed-bean-class>com.DX_57.SM_57.Glassfish</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<description>comment</description>
<managed-bean-name>LinuxController</managed-bean-name>
<managed-bean-class>com.DX_57.SM_57.Linux</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<description>comment</description>
<managed-bean-name>LinuxConfigurationController</managed-bean-name>
<managed-bean-class>com.DX_57.SM_57.LinuxConfiguration</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<description>comment</description>
<managed-bean-name>LogController</managed-bean-name>
<managed-bean-class>com.DX_57.SM_57.Log</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<description>comment</description>
<managed-bean-name>OracleController</managed-bean-name>
<managed-bean-class>com.DX_57.SM_57.Oracle</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<description>comment</description>
<managed-bean-name>UpdatesController</managed-bean-name>
<managed-bean-class>com.DX_57.SM_57.Updates</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>
This is the content of 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>
This is the content of web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.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>*.jsf</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
How they should be changed in order to use the latest JSF version?
EDIT 2 I disabled JavaScript in Firefox. I tested the code. It turns out that it works but I have to click Save button every time:
I choose custom and click Save Button
When I click the Save button the custom field appears.
When I enter the custom value and click Save button the field is updated.
I suppose that it won't work because the data is not send back from the JavaScrips to the server
EDIT 3 Debug information from firebug:
I choose from the menu custom:
<update id="j_idt5:input"><![CDATA[<span id="j_idt5:input"><input type="text" name="j_idt5:j_idt15" value="custom" /></span>]]></update>
I type custom value:
<?xml version='1.0' encoding='UTF-8'?>
<partial-response><changes><update id="j_idt5"><![CDATA[
<form id="j_idt5" name="j_idt5" method="post" action="/SM_57-1.0-SNAPSHOT/Application.jsf" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="j_idt5" value="j_idt5" />
<table>
<tbody>
<tr>
<td>User Session Timeout</td>
<td><select id="j_idt5:j_idt10" name="j_idt5:j_idt10" size="1" onchange="mojarra.ab(this,event,'valueChange',0,'j_idt5:input')"> <option value="custom" selected="selected">custom</option>
<option value="two">Option two</option>
<option value="three">Option three</option>
<option value="custom" selected="selected">Define custom value</option>
</select><span id="j_idt5:input"><input type="text" name="j_idt5:j_idt15" value="custom" /></span></td>
</tr>
<tr>
<td>Maximum allowed users</td>
</tr>
</tbody>
</table>
<input id="j_idt5:j_idt18" type="submit" name="j_idt5:j_idt18" value="Save Settings" onclick="mojarra.ab(this,event,'action','@form','@form');return false" />
</form>]]></update><update id="javax.faces.ViewState"><![CDATA[-8356663926661541536:7758149566419150570]]></update></changes></partial-response>
I click save button:
<?xml version='1.0' encoding='UTF-8'?>
<partial-response><changes><update id="j_idt5"><![CDATA[
<form id="j_idt5" name="j_idt5" method="post" action="/SM_57-1.0-SNAPSHOT/Application.jsf" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="j_idt5" value="j_idt5" />
<table>
<tbody>
<tr>
<td>User Session Timeout</td>
<td><select id="j_idt5:j_idt10" name="j_idt5:j_idt10" size="1" onchange="mojarra.ab(this,event,'valueChange',0,'j_idt5:input')"> <option value="wdwdew" selected="selected">wdwdew</option>
<option value="two">Option two</option>
<option value="three">Option three</option>
<option value="custom">Define custom value</option>
</select><span id="j_idt5:input"></span></td>
</tr>
<tr>
<td>Maximum allowed users</td>
</tr>
</tbody>
</table>
<input id="j_idt5:j_idt18" type="submit" name="j_idt5:j_idt18" value="Save Settings" onclick="mojarra.ab(this,event,'action','@form','@form');return false" />
</form>]]></update><update id="javax.faces.ViewState"><![CDATA[-8356663926661541536:7758149566419150570]]></update></changes></partial-response>
解决方案
You've redefinied the beans in faces-config.xml
. Your managed bean is been redefinied as a request scoped one:
<managed-bean>
<description>comment</description>
<managed-bean-name>ApplicationController</managed-bean-name>
<managed-bean-class>com.DX_57.SM_57.Application</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
The managed bean configurations in faces-config.xml
file have precedence over the annotations. Your ApplicationController
bean is in reality thus a request scoped JSF managed bean, not a session scoped CDI managed bean like as you have declared by annotations. On every single ajax request a brand new one will be created. All changes done in previous requests are "forgotten".
You have 2 options:
Remove the bean configuration fromfaces-config.xml
.
Change the managed bean scope to view
(please note that it will then still be JSF who is managing the bean instance, not CDI and that CDI doesn't have a suitable @ViewScoped
annotation).