Java 中的 URL 路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2943787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
URL Routing in Java
提问by Hyman Edmonds
Coming from other web frameworks, I'm used to being able to map parts of a URL to method parameters. I know that web.xml provides a way to map an entire URL to a Servlet but is there a way to get more features out of this, such as mapping pieces of the URL to method parameters?
来自其他 Web 框架,我习惯于能够将 URL 的一部分映射到方法参数。我知道 web.xml 提供了一种将整个 URL 映射到 Servlet 的方法,但是有没有办法从中获得更多功能,例如将 URL 的一部分映射到方法参数?
采纳答案by calavera.info
Using Spring (MVC) is overkill for this. If you don't need dependency injection, you'll be happy with redirect filter.
为此,使用 Spring (MVC) 是多余的。如果你不需要依赖注入,你会对重定向过滤器感到满意。
回答by Pascal Thivent
Actually, most MVC frameworks support RESTful actions (i.e. allow to map URLs on methods of actions): Spring MVC, Stripes, Struts 2 with the REST plugin.
实际上,大多数 MVC 框架都支持 RESTful 操作(即允许在操作方法上映射 URL):Spring MVC、Stripes、带有REST 插件的Struts 2 。
If you're not using any of them, you could achieve this with URL rewriting. The UrlRewriteFilteris pretty famous and allows to implements such things. From the documentation about Method Invocation:
如果您没有使用它们中的任何一个,您可以通过 URL 重写来实现这一点。该UrlRewriteFilter相当出名,并允许实现这样的事情。从关于方法调用的文档中:
The standard servlet mapping that is done via web.xml is rather limiting. Only .xxx or /xxxx/, no abilty to have any sort of smart matching. Using UrlRewriteFilter any rule when matched can be set to run method(s) on a class.
Invoke a servlet directly
<rule> <from>^/products/purchase$</from> <run class="com.blah.web.MyServlet" method="doGet" /> </rule>This will invoke doGet(HttpServletRequest request, HttpServletResponse response) when the "from" is matched on a request. (remeber this method needs to be public!)
Use it to delegate cleanly to your methods
<rule> <from>^/pref-editor/addresses$</from> <run class="com.blah.web.PrefsServlet" method="runAddresses" /> </rule> <rule> <from>^/pref-editor/phone-nums$</from> <run class="com.blah.web.PrefsServlet" method="runPhoneNums" /> </rule>
通过 web.xml 完成的标准 servlet 映射相当有限。只有.xxx 或 /xxxx/,没有任何智能匹配的能力。使用 UrlRewriteFilter 匹配的任何规则都可以设置为在类上运行方法。
直接调用servlet
<rule> <from>^/products/purchase$</from> <run class="com.blah.web.MyServlet" method="doGet" /> </rule>当“from”与请求匹配时,这将调用 doGet(HttpServletRequest request, HttpServletResponse response) 。(记住这个方法需要公开!)
用它来干净地委派给你的方法
<rule> <from>^/pref-editor/addresses$</from> <run class="com.blah.web.PrefsServlet" method="runAddresses" /> </rule> <rule> <from>^/pref-editor/phone-nums$</from> <run class="com.blah.web.PrefsServlet" method="runPhoneNums" /> </rule>
回答by mtomis
回答by duffymo
You can do such things with Spring web MVC. Their controller API can map parts of the URL to specific calls on the back end.
你可以用 Spring web MVC 做这样的事情。他们的控制器 API 可以将 URL 的一部分映射到后端的特定调用。

