如何将原始 Java servlet 映射到 web.xml 中的 HTML 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11828531/
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
How to map raw Java servlet to an HTML file in web.xml?
提问by IAmYourFaja
My WAR is structure is as follows:
我的 WAR 结构如下:
my-web-app.war/
views/
index.html
blah.html
META-INF/
MANIFEST.MF
WEB-INF/
web.xml
lib/
<!-- Dependencies -->
classes/
org.me.mywebapp.controllers/
MyController.class
<!-- Other packages/classes as well -->
I would like to configure web.xml
so that when the WAR is deployed locally it's index.html
page can be accessed by going to http://localhost/my-web-app/index.html
.
我想进行配置,web.xml
以便在本地部署 WAR 时,index.html
可以通过转到http://localhost/my-web-app/index.html
.
Here's what I have so far:
这是我到目前为止所拥有的:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<!-- The display name of this web application -->
<display-name>My Web App</display-name>
<listener>
<listener-class>
org.me.mywebapp.context.ContextImpl
</listener-class>
</listener>
</web-app>
How do I configure this URL to view mapping? Thanks in advance!
如何配置此 URL 以查看映射?提前致谢!
回答by pater
You could map your servlets like this
你可以像这样映射你的 servlet
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>org.me.mywebapp.controllers.MyController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>index.html</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>controller2</servlet-name>
<servlet-class>org.me.mywebapp.controllers.OtherController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>controller2</servlet-name>
<url-pattern>blah.html</url-pattern>
</servlet-mapping>
And if you want to show view/blah.html as /blah.html, in the controller you just dispatch the request to the apropriate views/*.html or jsp or anything you want.
如果您想将 view/blah.html 显示为 /blah.html,则在控制器中您只需将请求分派到适当的 views/ *.html 或 jsp 或任何您想要的内容。
EDIT: As you requested: You can dispatch the request to another page inside the servlet like this:
编辑:按照您的要求:您可以将请求分派到 servlet 内的另一个页面,如下所示:
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/views/blah.html");
dispatcher.forward(request, response);
Although the above code is working, you should probably implement a more "sophisticated" approach inside each servlet to decide to which view you will dispatch, espesially if your application has a lot of controller, views etc. Try reading more about MVC implementations if you haven't done already.
尽管上面的代码可以工作,但您可能应该在每个 servlet 中实现更“复杂”的方法来决定将分派到哪个视图,尤其是如果您的应用程序有很多控制器、视图等。如果您愿意,请尝试阅读有关 MVC 实现的更多信息还没做。
回答by Piotr Gwiazda
You can use a filter that routes specific requests to view
path. See the response : https://stackoverflow.com/a/3593513/221951Then you decide in filter if a request should be passed to servlet or not.
您可以使用过滤器将特定请求路由到view
路径。请参阅响应:https: //stackoverflow.com/a/3593513/221951然后您在过滤器中决定是否应将请求传递给 servlet。
You can alos try to use Tuckey URL rewrite filter http://tuckey.org/urlrewrite/
您也可以尝试使用 Tuckey URL 重写过滤器http://tuckey.org/urlrewrite/
回答by Rahul Agrawal
You can do this by re-writing the URL in Filter.
您可以通过在过滤器中重写 URL 来做到这一点。
Like it is implemented by most of frameworks like Struts, Spring MVC, Tapestry, Wicket, etc
就像它由大多数框架实现,如 Struts、Spring MVC、Tapestry、Wicket 等