我想阻止直接访问我的项目中的*.xhtml文件.在页面中,有一些commandLinks调用某些bean的某些方法.这些bean将视图的名称作为字符串返回.
return "campaign.xhtml?faces-redirect=true";
Run Code Online (Sandbox Code Playgroud)
如果用户写入以下浏览器的地址栏,我不希望用户看到xhtml文件.
http://localhost:8080/myApp/faces/campaign.xhtml
Run Code Online (Sandbox Code Playgroud)
要么
http://localhost:8080/myApp/faces/campaign.xhtml?faces-redirect=true
Run Code Online (Sandbox Code Playgroud)
因为,在某些bean中,我填写了这些xhtml视图.但是,如果用户直接访问xhtml文件,则用户将看到这些视图而没有填充的信息.
当我在web.xml文件中使用时,访问被拒绝.但是,在这种情况下,当bean返回值"campaign.xhtml?faces-redirect = true"时,它也不能同时显示视图.Bean也拒绝访问.
我该怎么做才能防止这种情况发生?
谢谢.
FarukKuşcan
用户看到这些视图没有填充信息.
preRenderView如果信息已填写,请检查事件监听器.如果没有,请重定向回来.
<f:event type="preRenderView" listener="#{bean.init}" />
Run Code Online (Sandbox Code Playgroud)
同
public void init() throws IOException {
if (information == null) {
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
externalContext.redirect(externalContext.getRequestContextPath() + "/otherpage.xhtml");
}
}
Run Code Online (Sandbox Code Playgroud)
FacesContext#isValidationFailed()如果您实际上也在使用<f:viewParam>验证,则可以根据需要将其组合使用.例如
<f:viewParam name="id" value="#{bean.information}" required="true" />
<f:event type="preRenderView" listener="#{bean.init}" />
Run Code Online (Sandbox Code Playgroud)
同
public void init() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
if (context.isValidationFailed()) {
ExternalContext externalContext = context.getExternalContext();
externalContext.redirect(externalContext.getRequestContextPath() + "/otherpage.xhtml");
}
}
Run Code Online (Sandbox Code Playgroud)
更新:在JSF 2.2中,您可以使用<f:viewAction>此功能.
<f:viewAction listener="#{bean.check}" />
Run Code Online (Sandbox Code Playgroud)
public String check() {
if (information == null) {
return "otherpage?faces-redirect=true";
} else {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4581 次 |
| 最近记录: |