Clean way for conditionally rendering HTML in view?

别说谁变了你拦得住时间么 提交于 2019-12-10 11:09:05

问题


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/



来源:https://stackoverflow.com/questions/1444225/clean-way-for-conditionally-rendering-html-in-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!