How to get the username of user who logged into Ja

2019-07-25 13:06发布

问题:

Please tell me if there is any method through which I can get the user name of the user who logged into the Jasper Server, and that name could be displayed in the report.

Is there any parameter which can be referred or any other work around to obtain the logged in user name or any other way to deal with the issue.

Thanks in Advance!!

回答1:

Try declaring a parameter with the name "LoggedInUsername" (this exact name, since it is reserverd by jasper to hold the username of the logged user).

<parameter name="LoggedInUsername" class="java.lang.String" isForPrompting="false"/>

and use it like $P{LoggedInUsername}.

Note: this will only work on the server, not in iReport.



回答2:

Open report in iReport and create a parameter with name "LoggedInUser" and class "com.jaspersoft.jasperserver.api.metadata.user.domain.User", otherwise you can copy the below XML line in XML of the report where all parameters are listed.

   <parameter name="LoggedInUser" 
        class="com.jaspersoft.jasperserver.api.metadata.user.domain.User"/>

Then add a text field and then you can refer to this parameter, like:

    <textFieldExpression class="java.lang.String">
           <![CDATA[$P{LoggedInUser}.getFullName()]]></textFieldExpression>

or

    <textFieldExpression class="java.lang.String">
           <![CDATA[$P{LoggedInUser}.getUsername()]]></textFieldExpression>