Clean way for conditionally rendering HTML in view

2019-05-20 00:16发布

问题:

Is there a cleaner way to do this in a JSP/Struts1 setup ?

... some HTML here ...

EDIT: In admin mode I would like to have access to additional parameters from a form element, e.g. from the form element:

input type="text" value="Test user" name="Owner"

EDIT 2: Actually, my problem is very similar to the question that was asked in : Conditionally Render In JSP By User

But I don't really get the "pseudo-code" from the likely answer

回答1:

Is SessionConfig exposed as a bean in your JSP (as part of request / session / Struts Form)?

If it's not, you can expose it. And if it's a static class containing global settings (which, by the looks of it, is a possibility), you can create a small wrapper and put it in the servlet context which you'd then be able to access from Struts tags as scope="application". Once that's done you can check your condition via Struts tags:

<logic:equal name="sessionConfig" property="adminMode" value="true">
 ... your HTML here
</logic:equal>

Or, if you're using EL / JSTL, same can be done via <core:if>.



回答2:

Without more information, it's hard to answer this, but I'd think instead of separate views: one for admin mode, one for normal mode. Extracting the parts of your pages into tiles will help you do this without a lot of pain; see: http://tiles.apache.org/