I've got a web application that I need to be able to configure parts of from a JSF page. So for example, imagine my application was split into several smaller parts each with a folder/file structure like below:
我有一个Web应用程序,我需要能够从JSF页面配置部分。例如,假设我的应用程序被分成几个较小的部分,每个部分都有一个文件夹/文件结构,如下所示:
/partname
/config
config.xhtml
/template
generaltemplate.xhtml
search.xhtml
results.xhtml
Search.xhtml & results.xhtml both use the template file, generaltemplate.xhtml. This generaltemplate has an to include the contents of config.xhtml. The plan is to put all shared configuration into this page so that the applications can access it. But I'm having trouble doing this.
Search.xhtml&results.xhtml都使用模板文件generaltemplate.xhtml。此generaltemplate包含config.xhtml的内容。计划是将所有共享配置放入此页面,以便应用程序可以访问它。但我这样做有困难。
If i create the variables using <c:set> in the config.xhtml, then they seem to only be accessible from that config page. Previously I would have just changed the scope on <c:set> to request and ny problem would be solved. But as JSF works in a different way, I know that scope in this case is useless.
如果我使用config.xhtml中的
Does anyone have any suggestions for how I am going to be able to create all my config in this one page and then be able to access it throughout my application?
有没有人对我如何能够在这一页中创建所有配置,然后能够在我的应用程序中访问它有任何建议?
This app is written using Seam so using something from Seam could be an option.
这个应用程序是使用Seam编写的,所以使用Seam中的东西可能是一个选项。
3 个解决方案
#1
Assuming you can use facelets, for the c:set issue:
假设你可以使用facelets,对于c:set问题:
In your template:
在您的模板中:
...
<html ...
xmlns:ui="http://java.sun.com/jsf/facelets"
...
<body>
<ui:param name="testParam" value="Test" />
<ui:insert name="test">
</ui:insert>
...
In this case the value of the ui:param can be used as a variable to be used in pages using the template.
在这种情况下,ui:param的值可以用作在使用模板的页面中使用的变量。
Pages using template:
使用模板的页面:
...
<html ...
xmlns:ui="http://java.sun.com/jsf/facelets"
...
<body>
<ui:composition template="/WEB-INF/templates/template.xhtml">
<ui:define name="test">
<h:outputText value="#{testParam}" />
...
With that said, I can think of a very few instances where this should actually be used. Unless your hands are tied, values should come from application/session scoped beans.
话虽如此,我可以想到实际应该使用它的极少数情况。除非你的手绑定,否则值应来自应用程序/会话范围的bean。
Edit:
I apologize for not double checking earlier, as I was extremely busy and used the first answer that came to mind, however, I verified using a c:set in place of the ui:param would work equally as well using the previous defined set up. So, in the template, you could have instead of . This would mean you would also have to include xmlns:c="http://java.sun.com/jstl/core", which I am sure you already have.
我为之前没有仔细检查而道歉,因为我非常忙,并且使用了第一个想到的答案,但是,我使用c:set来验证ui:param将使用之前定义的设置同样有效。因此,在模板中,您可以使用而不是。这意味着你还必须包含xmlns:c =“http://java.sun.com/jstl/core”,我相信你已经有了。
#2
Seam is a Model-View-Controller (MVC) framework. The view is implemented using JSF pages. Configuration is part of the model and should not be put in pages.
Seam是一个模型 - 视图 - 控制器(MVC)框架。该视图使用JSF页面实现。配置是模型的一部分,不应放在页面中。
To implemented proper layering I suggest you put your configuration in some kind of globally accessible bean (instantiated by Seam, probably populated from a .properties file) which you reference in your JSF pages.
为了实现正确的分层,我建议你将你的配置放在某种全局可访问的bean中(由Seam实例化,可能是从.properties文件中填充的),你在JSF页面中引用它。
#3
I don't know how much refactoring would be involved but you could create a configuration bean which would be scoped appropriately, then use the c:set tags to set your configuration bean. Then, later on in your page, you can use the configuration bean rather than directly accessing the variables.
我不知道会涉及多少重构,但您可以创建一个适当范围的配置bean,然后使用c:set标签来设置配置bean。然后,在您的页面中,您可以使用配置bean而不是直接访问变量。
#1
Assuming you can use facelets, for the c:set issue:
假设你可以使用facelets,对于c:set问题:
In your template:
在您的模板中:
...
<html ...
xmlns:ui="http://java.sun.com/jsf/facelets"
...
<body>
<ui:param name="testParam" value="Test" />
<ui:insert name="test">
</ui:insert>
...
In this case the value of the ui:param can be used as a variable to be used in pages using the template.
在这种情况下,ui:param的值可以用作在使用模板的页面中使用的变量。
Pages using template:
使用模板的页面:
...
<html ...
xmlns:ui="http://java.sun.com/jsf/facelets"
...
<body>
<ui:composition template="/WEB-INF/templates/template.xhtml">
<ui:define name="test">
<h:outputText value="#{testParam}" />
...
With that said, I can think of a very few instances where this should actually be used. Unless your hands are tied, values should come from application/session scoped beans.
话虽如此,我可以想到实际应该使用它的极少数情况。除非你的手绑定,否则值应来自应用程序/会话范围的bean。
Edit:
I apologize for not double checking earlier, as I was extremely busy and used the first answer that came to mind, however, I verified using a c:set in place of the ui:param would work equally as well using the previous defined set up. So, in the template, you could have instead of . This would mean you would also have to include xmlns:c="http://java.sun.com/jstl/core", which I am sure you already have.
我为之前没有仔细检查而道歉,因为我非常忙,并且使用了第一个想到的答案,但是,我使用c:set来验证ui:param将使用之前定义的设置同样有效。因此,在模板中,您可以使用而不是。这意味着你还必须包含xmlns:c =“http://java.sun.com/jstl/core”,我相信你已经有了。
#2
Seam is a Model-View-Controller (MVC) framework. The view is implemented using JSF pages. Configuration is part of the model and should not be put in pages.
Seam是一个模型 - 视图 - 控制器(MVC)框架。该视图使用JSF页面实现。配置是模型的一部分,不应放在页面中。
To implemented proper layering I suggest you put your configuration in some kind of globally accessible bean (instantiated by Seam, probably populated from a .properties file) which you reference in your JSF pages.
为了实现正确的分层,我建议你将你的配置放在某种全局可访问的bean中(由Seam实例化,可能是从.properties文件中填充的),你在JSF页面中引用它。
#3
I don't know how much refactoring would be involved but you could create a configuration bean which would be scoped appropriately, then use the c:set tags to set your configuration bean. Then, later on in your page, you can use the configuration bean rather than directly accessing the variables.
我不知道会涉及多少重构,但您可以创建一个适当范围的配置bean,然后使用c:set标签来设置配置bean。然后,在您的页面中,您可以使用配置bean而不是直接访问变量。