问题
I have been trying to conduct some simple tests on Seam Weld and MyFaces CODI. After adding CODI jar files to my projects, I found that it adds a windowId request value to every request even if the bean scope is RequestScoped. Is it really necessary to add windowId request parameter to every request while the bean is in RequestScoped? Is there any practical real-world scenario for this case? Is is possible to remove it if it is not necessary? For example:
This is the code of bean class:
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
@Named("myBean")
@RequestScoped
public class MyBean{
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
This is the body of the page:
<body>
<h:form>
<h:inputText value="#{myBean.firstName}"></h:inputText>
<br/>
<h:inputText value="#{myBean.lastName}"></h:inputText>
<br/>
<h:commandButton value="submit"></h:commandButton>
</h:form>
</body>
回答1:
Apache MyFaces CODI adds the windowId to support browser tab seperated beans. If you use some CODI scopes like @WindowScoped, @ViewAccessScoped, CODIs @ConversationScoped, then you will get a separate contextual instance for each browser tab.
Assume you have a customer relation management app. With CODI @WindowScoped you can open different Customers in different browser tabs/windows. Would you use @SessionScoped, then you would overwrite the values each time (For @SessionScoped beans there is only 1 contextual instance for each session).
And of course you can disabled this feature pretty easily. Please check our official WIKI: https://cwiki.apache.org/confluence/display/EXTCDI/Index
回答2:
It takes some time to find solution in official wiki. https://cwiki.apache.org/confluence/display/EXTCDI/Index
If you do not use @WindowScoped, @ViewAccessScoped and sure you don't need this windowId parameter, then you can create class like this in your project:
@Specializes
@ApplicationScoped
public class CustomWindowContextConfig extends WindowContextConfig {
@Override
public boolean isAddWindowIdToActionUrlsEnabled() {
return false;
}
@Override
public boolean isUrlParameterSupported() {
return false;
}
}
来源:https://stackoverflow.com/questions/7802287/myfaces-codi-and-windowid-request-parameter-issue