java 春季MVC;避免在 url 中使用文件扩展名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1399487/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:24:40  来源:igfitidea点击:

Spring MVC; avoiding file extension in url?

javaspringjspspring-mvc

提问by Ezombort

I just started with Spring Web MVC. I'm trying to avoid file extenstions in the url. How can i do this? (I'm using Spring 2.5.x)

我刚开始使用 Spring Web MVC。我试图避免 url 中的文件扩展名。我怎样才能做到这一点?(我使用的是 Spring 2.5.x)

Bean:

豆角,扁豆:

<bean name="/hello.htm" class="springapp.web.HelloController"/>

I want it to be:

我希望它是:

<bean name="/hello" class="springapp.web.HelloController"/>

I cannot get it to work. Any ideas?

我无法让它工作。有任何想法吗?

Edit:

编辑:

Url-mapping

网址映射

<servlet-mapping>
    <servlet-name>springapp</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>

I have tried changing the url-pattern with no luck (* and /*).

我试过在没有运气的情况下更改 url 模式(* 和 /*)。

采纳答案by Paul Whelan

<servlet>
    <servlet-name>spring-mvc</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>spring-mvc</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

Then you need to register your urls to be handled by a particular controller. See the following

然后您需要注册由特定控制器处理的 url。请参阅以下内容

http://static.springsource.org/spring/docs/2.0.x/reference/mvc.html

http://static.springsource.org/spring/docs/2.0.x/reference/mvc.html

回答by ecdragon

In 3.0, / seems to work. That is...

在 3.0 中, / 似乎有效。那是...

<url-pattern>/</url-pattern>

回答by Tom van Zummeren

As far as I know you can't do this if you're using JSP's as your view for controllers.

据我所知,如果您使用 JSP 作为控制器的视图,则无法执行此操作。

Because when you pass a model to a JSP, Spring MVC internally performs a 'forward' to the URL of the JSP. If you use <url-pattern>/*</url-pattern>then this forward will also be handled by your DispatcherServlet and not by your JSP view.

因为当您将模型传递给 JSP 时,Spring MVC 在内部执行到 JSP 的 URL 的“转发”。如果您使用,<url-pattern>/*</url-pattern>那么这个转发也将由您的 DispatcherServlet 处理,而不是由您的 JSP 视图处理。

What you can do is use <url-pattern>/something</url-pattern>and have your JSP's in a different directory

您可以做的是使用<url-pattern>/something</url-pattern>并将您的 JSP 放在不同的目录中

回答by alarmo

In Spring 3.2 at least, the accepted answer above is very nearly but not quite what's needed. The web.xml bit below just worked for me, and I'm adding it to the thread here for reference of whoever googles this next...

至少在 Spring 3.2 中,上面接受的答案非常接近但不完全是需要的。下面的 web.xml 位只对我有用,我将它添加到这里的线程中以供下一个谷歌搜索的人参考......

<servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

回答by serg

Try first:

先试试:

<servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <url-pattern>/hello</url-pattern>
</servlet-mapping>

If that doesn't work then problem is somewhere else. Is your Apache set up to forward those urls to Tomcat? Something like:

如果那不起作用,那么问题出在其他地方。您的 Apache 是否设置为将这些 url 转发到 Tomcat?就像是:

JkMount /hello worker1

回答by Michael Lloyd Lee mlk

Have you tried <url-pattern>/*</url-pattern>in the servlet mapping and <bean name="/hello" .../>?

您是否尝试 <url-pattern>/*</url-pattern>过 servlet 映射和<bean name="/hello" .../>