如何配置Tomcat的web.xml我的前端控制器 [英] How to configure tomcat's web.xml with my Front Controller

查看:214
本文介绍了如何配置Tomcat的web.xml我的前端控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的前端控制器为我的Java EE应用程序。该控制器拦截所有的URL重定向到在正确的类中相应的方法。

一个典型的URL看起来像这样:的http://domain.tld/appName/Controller/method

我现在面临使用Tomcat 3个问题的时刻:


  1. 如果我尝试访问我的基本URL, HTTP://domain.tld/appName/ (有或没有结束斜线),我的前端控制器是不是叫和我有一个404。

  2. 如果我试图访问一个网址如下:domain.tld /的appName /控制器/方法/(注结束斜线),比点数1,但是,如果没有正常工作的结束斜线同样的事情

  3. 最后,因为我的所有请求都被路由到我的前端控制器我必须定义所有的静态文件送达给默认的servlet,在web.xml中我。一个不太contraining和丑陋的解决方案将是很好。

下面是我的web.xml:

 <?XML版本=1.0编码=UTF-8&GT?;
< web应用程序的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance的xmlns =htt​​p://java.sun.com/xml/ns/javaee的xmlns:网络= http://java.sun.com/xml/ns/javaee/web-app_2_5.xsdXSI:的schemaLocation =HTTP http://java.sun.com/xml/ns/javaee://java.sun。 COM / XML / NS / JavaEE的/ WEB-app_3_0.xsdID =WebApp_ID版本=3.0>
    <&servlet的GT;
        < servlet的名称>&FrontController LT; / servlet的名称>
        <的servlet类> controllers.FrontController< / servlet的类>
    < / servlet的>
    < Servlet映射>
        < servlet的名称>&FrontController LT; / servlet的名称>
        < URL模式> /< / URL模式>
    < / Servlet映射>
    < Servlet映射>
        < servlet的名称>&默认LT; / servlet的名称>
        < URL模式> * HTML和LT; / URL模式>
    < / Servlet映射>
    < Servlet映射>
        < servlet的名称>&默认LT; / servlet的名称>
        < URL模式> * JPG< / URL模式>
    < / Servlet映射>
    < Servlet映射>
        < servlet的名称>&默认LT; / servlet的名称>
        < URL模式> * PNG< / URL模式>
    < / Servlet映射>
    < Servlet映射>
        < servlet的名称>&默认LT; / servlet的名称>
        < URL模式> * CSS< / URL模式>
    < / Servlet映射>
    < Servlet映射>
        < servlet的名称>&默认LT; / servlet的名称>
        < URL模式> * JS< / URL模式>
    < / Servlet映射>
< / web-app的>


解决方案

正如评论所说,我FrontController是有点难看。
重写它正确地做了与同一个web.xml的伎俩。

I've written a simple Front Controller for my Java EE application. This controller intercepts all the urls to redirect them to the corresponding method in the right class.

A typical url looks like this: http://domain.tld/appName/Controller/method

I'm facing 3 issues with tomcat at the moment:

  1. If I try to access to my base url, http://domain.tld/appName/ (with or without the ending slash), my front controller isn't called and I've got a 404.
  2. If I try to access to an url like this: domain.tld/appName/Controller/method/ (remark the ending slash) same thing than point number 1. But without the ending slash it works fine.
  3. Finally, since all my requests are routing to my front controller I have to define all the static file to be served to the default servlet, in my web.xml. A less contraining and ugly solution would be nice.

Here is my web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <servlet>
        <servlet-name>FrontController</servlet-name>
        <servlet-class>controllers.FrontController</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>FrontController</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.jpg</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.png</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
    </servlet-mapping>
</web-app>

解决方案

As said in comment, my FrontController was kinda ugly. Rewriting it properly did the trick with the same web.xml.

这篇关于如何配置Tomcat的web.xml我的前端控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆