简体   繁体   English

如何知道另一个JSP文件是否包含在内?

[英]How to know if a JSP file is beeing included by another one?

I have x.jsp that can either be served as the whole response, or being included in several other jsp files. 我有x.jsp ,可以作为整个响应,或者包含在其他几个jsp文件中。

So how can I know inside x.jsp if it is being included by another file or not? 那么我怎么能知道x.jsp里面是否被其他文件包含?

You can check the value of the attribute javax.servlet.include.request_uri in your x.jsp page. 您可以在x.jsp页面中检查属性javax.servlet.include.request_uri的值。 If set, the jsp page was included. 如果设置,则包含jsp页面。

Further details here: Java HttpServletRequest get URL in browsers URL bar 更多详细信息: Java HttpServletRequest在浏览器URL栏中获取URL

你可以在'其他文件'中有一个隐藏字段,如果你包含x.jsp你应该为它设置一个值,当你想知道它包括你应该检查隐藏字段是否value == 'value'时,在x.jsp value == 'value'

If x.jps have knowledge about inclusion, it means that some best practices is ignored: 如果x.jps有关于包含的知识,则意味着忽略了一些最佳实践:

  • MVC pattern is thrown away because your x.jsp is not just a view , but also some kind of controller . MVC模式被丢弃,因为你的x.jsp不仅仅是一个视图 ,而且还有某种控制器
  • Possibly the Single responsibility principle is broken. 可能单一责任原则被打破。 Try split x.jsp to y.jsp and z.jsp , or introduce a variable which will contain value for appropriate case 尝试将x.jsp拆分为y.jspz.jsp ,或者引入一个包含适当大小写值的变量
  • It possibly increases coupling because x.jsp needs additional data about enviroment. 它可能会增加耦合,因为x.jsp需要有关环境的其他数据。

Of course these practices are not mandatory but you need a strong reason why you ignore them. 当然,这些做法不是强制性的,但你需要一个强有力的理由来忽略它们。

Please give us more information about your case or rephrase you question 请向我们提供有关您案件的更多信息或重新提出您的问题

Edit 编辑

My answer is abstract as your question is abstract. 我的答案是抽象的,因为你的问题是抽象的。 Get us your jsp, what should be changed and I hope you get better answer. 给我们你的jsp,应该改变什么,我希望你得到更好的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM