任何人都可以澄清我们如何在一般情况下使用,或者在现实世界中使用这个代码片段?
<f:metadata>
<f:viewParam id="id" value="#{bean.id}" />
<f:viewAction action="#{bean.init}" />
</f:metadata>
Run Code Online (Sandbox Code Playgroud) 防止用户角色执行操作.
在一个真实的案例中,我有这个:
public String delete() {
if(FacesContext.getCurrentInstance().getExternalContext().isUserInRole("administrator"){
//.....the action to perform
}
return "Denied";
}
Run Code Online (Sandbox Code Playgroud)
我希望我可以使用@RolesAllowed()EJB 的注释,但我不使用EJB而是使用ManagedBeans.所以问题是:有没有办法同时使用多个角色?一些解决方法!示例:如果必须允许对3个角色执行操作(管理员,主持人,经理).我有义务这样做:
if (FacesContext.getCurrentInstance().getExternalContext().isUserInRole("administrator")
|| FacesContext.getCurrentInstance().getExternalContext().isUserInRole("manager")
|| .....) {
//....
}
Run Code Online (Sandbox Code Playgroud)
重现所有方法都很痛苦.像数百种方法的东西:(
我通过托管bean以这种方式获取连接用户的用户名(使用j_security_check):
......
username = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal().getName();
Run Code Online (Sandbox Code Playgroud)
然后以这种方式在jsf页面中显示它:#{userBean.username}
但我认为无法获得连接用户的数量并获得他们的角色.换句话说,除了用户名,用户角色和连接用户数之外,我还要显示.
我怎样才能做到这一点!?在此先感谢您的帮助!
编辑:我现在可以使用托管bean中的namedquery获取连接用户的角色:
public Users getUserRole(){
try {
Users auser = (Users)
em.createNamedQuery("Users.findByUsername").
setParameter("username", getRemoteUser()).getSingleResult();
return auser;
} catch (NoResultException nre) {
JsfUtil.addErrorMessage(nre, "getUserRole Error");
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
并在xhtml页面中:
<h:outputLabel for="rolefacet" value="Role: "/>
<h:outputFormat id="rolefacet" value="#{UserBean.userRole.ugroup}" />
Run Code Online (Sandbox Code Playgroud)
而ugroup是Users实体类中的角色名称.
编辑:一个仍然不适合我的解决方案是在我的web.xml中添加一个HttpSessionListener:
package beans;
/**
*
* @author med81
*/
import java.io.Serializable;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.ArrayList;
import javax.faces.context.FacesContext;
public class SessionCounter implements …Run Code Online (Sandbox Code Playgroud) 我无法得到primeFaces的<p:filedownload工作.我成功使用自定义下载bean下载文件.似乎issu与primefaces文件下载ActionListener有关,但我没有看到如何.有人有线索吗?谢谢.
这是我的豆子:
@ManagedBean(name="fileDownloadBean")
@SessionScoped
public class FileDownloadBean implements Serializable {
private StreamedContent file;
public StreamedContent getFile() {
return file;
}
public void setFile(StreamedContent file) {
this.file = file;
}
/** Creates a new instance of FileDownloadBean */
public FileDownloadBean() {
InputStream stream = this.getClass().getResourceAsStream("sessionlog.csv");
file = new DefaultStreamedContent(stream, "application/csv", "sessionlog.csv");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的观看代码:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:o="http://openfaces.org/"
template="./../template.xhtml">
<ui:define name="title">
#{bundle.Log}
</ui:define>
<ui:define …Run Code Online (Sandbox Code Playgroud) In JSF 1.2 One was listing Items using Java List :
private List<Customer> customerItems = null;
Run Code Online (Sandbox Code Playgroud)
, but in JSF 2.0 JSf DataModel (ListDataModel) is the way to go. (I am using JSF 2.0)
private DataModel customerItems = null;
public abstract DataModel createPageDataModel();
Run Code Online (Sandbox Code Playgroud)
However, I still don't get how to efficiently work with it. Rather say, I can't even implement a simple and mandatory case which is, navigation.
I have a <h:datatable value="#{customerController}" var="customer"> that gets its row objects from …
晚上好,
在测试JSF 2.0 Web应用程序中,我试图获取活动会话的数量,但HttpSessionListener的sessionDestroyed方法中存在问题.实际上,当用户登录时,活动会话的数量增加1,但是当用户注销时,相同的数字保持原样(没有发生去除),更糟糕的是,当同一用户再次登录时(即使他没有验证会话),相同的数字也会增加.用不同的话来说:
1- I登录,活动会话编号增加1. 2- I注销(会话未经验证)3-我再次登录,会话编号增加1.显示为= 2. 4-我重复操作,并且会话编号保持递增,而只有一个用户登录.
所以我认为sessionDestroyed方法没有被正确调用,或者可能在会话超时后被有效地调用,这是WEB.XML中的一个参数(我的是60分钟).这很奇怪,因为这是一个会话监听器,我的班级没有任何问题.
请有人知道吗?
package mybeans;
import entities.Users;
import java.io.*;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import jsf.util.JsfUtil;
/**
* Session Listener.
* @author TOTO
*/
@ManagedBean
public class SessionEar implements HttpSessionListener {
public String ctext;
File file = new File("sessionlog.csv");
BufferedWriter output = null;
public static int activesessions = 0;
public static long creationTime = 0;
public static int remTime = 0;
String …Run Code Online (Sandbox Code Playgroud) 我不想附加?faces-redirect=true到我的每一个动作中<h:commandButton />,而是希望一次性使用它,faces-config.xml但我还不确定使用它的后果.
它规定redirect将代替使用forward.但后果是什么?这是一种不好的做法,它是否会影响持久性和servlet请求和响应?
对我来说,REDIRECT的使用仅用于正确的URL浏览器显示.我打算用这样的东西:
<navigation-rule>
<from-view-id>/*</from-view-id>
<navigation-case>
<from-outcome>*</from-outcome>
<redirect />
</navigation-case>
</navigation-rule>
Run Code Online (Sandbox Code Playgroud)
非常感谢您的指导.
以前从未使用过 Java 邮件。
在我的 JSF Web 应用程序中,我有一个具有与实体对应(followUp)的属性private Date checkDate;的Animal实体。(一个动物有很多后续记录)。除此以外:
用户必须每 3 个月创建一个新记录 {followUp}并将其标记为已检查并提供其操作的日期,即“checkDate”。但由于用户太懒了,他只为少数动物这样做。因此,他实际上希望通过电子邮件提醒超过 3 个月未检查的动物。示例:我followUp在 2011 年 1 月 1 日为动物“A”创建了一条记录,然后大约在 2011 年 1 月 4 日,用户收到一封电子邮件,提醒他去检查动物 B 的后续行动。
Web 应用程序正在本地企业网络上运行。
我只知道那个片段:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
class SimpleMail {
public static void main(String[] args) throws Exception{
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "mymail.server.org");
props.setProperty("mail.user", "emailuser");
props.setProperty("mail.password", "");
Session mailSession = Session.getDefaultInstance(props, null);
Transport transport = mailSession.getTransport();
MimeMessage …Run Code Online (Sandbox Code Playgroud) 在我的JSF Web应用程序中,我使用EclipseLink
描述符定制器
和
历史政策
填充数据库中的历史表.相应的JPA实体类使用注释@Customizer(beans.HistoryLesionhCustomizer.class)
历史表与源表具有相同的字段,另外还有两个字段(start_date和end_date),用于指定行的操作的开始和结束.它完全有效.但我需要的是填充历史表中的另一个字段.我调用user的这个字段应填充用户主体,这样我就可以跟踪执行CUD(创建/更新/删除)操作的用户.我认为历史策略允许我通过在数据库中指示其对应的名称来添加字段,并指示必须插入的对象值.但情况并非如此,或者可能是我无法弄清楚如何做到这一点.换句话说,与start_date和end_date一起,我想用以下内容填充用户字段:
package beans;
/**
* Whenever there is a change on a record or an insert, change will be traced.
* @author mediterran
*
*/
import javax.faces.context.FacesContext;
import org.eclipse.persistence.config.DescriptorCustomizer;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.history.HistoryPolicy;
public class HistoryLesionhCustomizer implements DescriptorCustomizer {
@Override
/**
* Implementation method to use
*/
public void customize(ClassDescriptor cd) throws Exception {
String user = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
HistoryPolicy policy = new HistoryPolicy(); // Instantiates a new …Run Code Online (Sandbox Code Playgroud) 不幸的是,OpenFaces Datatable还没有像Primefaces或IceFaces那样的导出功能.虽然导出是小菜一碟,过滤IceFaces或primefaces数据表是一些恐怖电影,但primeFaces的过滤不适用于JSF DataModel(悲伤但真实).
使用OpenFaces DataTable过滤是一件小事,非常方便和强大.但是,最终用户想要将他/她过滤的数据表导出为CSV或MS Excel等格式.
来自OpenFaces的伟大的Dmitry给了我一个提示:使用OpenFaces DataTable方法:从数据表中getDisplayedRowDatas()获取当前显示的行,然后使用像Itext这样的第三个thrid库来导出PDF格式.
在我看来,这应该是这样的:
getDisplayedRowsDatas()- 但我担心此方法不会返回列标题的名称 - 或者将OpenFaces Datatable呈现的HTML表作为缓冲字符串并使用Apache POI生成使用Buffer流式传输的Excel文件.在我最近遇到的MyFaces示例中:MyFaces Datatable Export Example,开发人员使用Myfaces组件<t:buffer value=#{myExportBean.myBufferString}> datatable here </t:buffer />将数据表的内容转换为缓冲的String,这使事情变得更容易.不幸的是,JSF Core没有这样的组件.
有人曾经遇到过这个吗?非常感谢帮助或更好的替代方案.
有人可以解释这个小Perl脚本的作用吗?谢谢.
#!/usr/bin/perl
my ($file, $from, $to) = @ARGV;
my $fh;
my $matching = 0;
open($fh, $file) or die $!;
while(<$fh>)
{
if(/\Q$from\E/) { $matching = 1; }
if($matching) { print $_; }
if($matching && /\Q$to\E/) { last; }
}
close($fh);
Run Code Online (Sandbox Code Playgroud) 我在bash脚本中使用以下代码来跟踪代码
#!/bin/bash
function trace()
{
echo "TRACE" \
"${BASH_SOURCE[1]}:${BASH_LINENO[0]}:${FUNCNAME[1]}:" \
"$BASH_COMMAND"
}
set -o functrace
shopt -s extdebug
trap trace DEBUG
# Remainder of code here
exit 0
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,它会吃掉我的变量.一些线索?
jsf ×10
java-ee-5 ×4
java-ee ×2
servlets ×2
apache-poi ×1
bash ×1
datamodel ×1
datatable ×1
debugging ×1
download ×1
eclipselink ×1
export ×1
jakarta-ee ×1
jakarta-mail ×1
java ×1
jpa ×1
jsf-2 ×1
linux ×1
openfaces ×1
perl ×1
primefaces ×1
viewaction ×1
viewparams ×1