不同路径解析器

发布于 2024-10-18 10:45:59 字数 337 浏览 0 评论 0原文

我有一个正在困扰的问题。这里只是假设的情况。例如我有两个文件夹 jspPages1 和 jspPages2。我想将一些 jsp 页面分开。我有一个调度程序 servlet。我希望第一个文件夹中的页面映射到“/public/jspNameFromFirstFolder.jsp”,第二个文件夹中的页面映射到“protected/jspNameFromSecondFolder.jsp”。我可以使用一个调度程序 servlet 来完成此操作吗?如果我尝试将 web.xml 中的 url 模式设置为“/protected/'asteriks'”和“/public/'asteriks'”,@RequestMapping 会是什么样子?你能为我澄清一下吗?先感谢您。

I have a question I am struggling with. Just a hypothetical situation here. For example I have two folders jspPages1 and jspPages2. There are jsp pages I want to keep separated. I have one dispatcher servlet. And I want pages from the first folder to be mapped to "/public/jspNameFromFirstFolder.jsp" and from the second folder to "protected/jspNameFromSecondFolder.jsp". Can I do that with one dispatcher servlet? If I try and make url patterns in web.xml to "/protected/'asteriks'" and "/public/'asteriks'", what would @RequestMapping be like? Can you please clarify it for me? Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一影成城 2024-10-25 10:45:59

在我看来,您一开始就不应该暴露您的 JSP。毕竟,您希望用户使用您提供的请求映射来访问页面。然后,您的控制器将决定向用户呈现哪个视图(在您的情况下,它们是 JSP 文件)。

我通常的做法是将视图塞到WEB-INF目录下,以防止用户猜测并直接访问JSP文件。如果有助于您组织文件,您仍然可以将视图分开。因此,您的目录结构可能如下所示: -

WEB-INF
|
|- jsp
|   |
|   |-- public
|   |    |
|   |    |- page1.jsp
|   |    |- page2.jsp
|   |
|   |-- protected
|   |    |
|   |    |- page3.jsp
|   |    |- page4.jsp

这样,您就可以在 Spring MVC 中将视图文件夹注册为 /WEB-INF/jsp/

您的请求映射可以是您想要的任何内容。它与您的视图目录结构无关。

例如:-

// when your user visits /page-1.htm, it returns public/page1.jsp as the view
@RequestMapping("/page-1")
public String showPage1() {
    return "public/page1";
}

// when your user visits /secure/page-3.htm, it returns protected/page3.jsp as the view
@RequestMapping("/secure/page-3")
public String showPage3() {
    return "protected/page3";
}

In my opinion, you shouldn't expose your JSPs in the first place. After all, you want your user to access the page using the request mapping you provide. Your controller will then decide which view (in your case, they are JSP files) to present to the user.

What I usually do is to stuff the views under WEB-INF directory to prevent users from guessing and accessing the JSP files directly. You can still keep the views separated if it helps you to organize your files. So, your directory structure may look like this:-

WEB-INF
|
|- jsp
|   |
|   |-- public
|   |    |
|   |    |- page1.jsp
|   |    |- page2.jsp
|   |
|   |-- protected
|   |    |
|   |    |- page3.jsp
|   |    |- page4.jsp

This way, you can register your view folder to be /WEB-INF/jsp/ in Spring MVC.

Your request mapping can be anything you want. It has nothing to do with your view directory structure.

For example:-

// when your user visits /page-1.htm, it returns public/page1.jsp as the view
@RequestMapping("/page-1")
public String showPage1() {
    return "public/page1";
}

// when your user visits /secure/page-3.htm, it returns protected/page3.jsp as the view
@RequestMapping("/secure/page-3")
public String showPage3() {
    return "protected/page3";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文