在我的 Spring 应用程序中,我有以下内容:
@RequestMapping(method = RequestMethod.GET, value = "/")
public String goHome(ModelMap map){
map.addAttribute("content", "home.jsp");
return "index";
}
@RequestMapping(method = RequestMethod.GET, value = "register")
public String getRegisterPage(ModelMap map){
map.addAttribute("content", "register.jsp");
return "index";
}
在本地主机上,我可以分别以 localhost:8080 和 localhost:8080/register 身份访问它们。但是,当我将它们部署在第三方服务器上时,domain.com 工作正常,但 domain.com/register(以及除 domain.com 之外的所有其他链接)会导致 404。
Not Found
The requested URL /register was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
注意:
- localhost 和domain.com 都运行在Tomcat 5.5 上并且具有Servlet 版本2.4
- 我什至将 RequestMapping 值更改为 value="/register" 但没有任何变化
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" 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">
<display-name>Adsense</display-name>
<listener>
<listener-class>com.adsense.connection.MySqlDBPooling</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
感谢帮助
最佳答案
在这里得到我的答案:Tomcat 404 Not Found error
The problem is that in the production environment tomcat is being fronted by an apache httpd which does not forward requests to tomcat for urls of the form /foobar (without an extension). Apache doesn't know how to deal with those URLs and tries to serve them as static files from the disk. The files are not there so it tries to execute the 404 rule which is badly configured (the configured 404 file is itself missing as well - but that's another problem).
Solutions:
Assign an extension to all your servlets and specify that to apache. E.g .do (like struts does) so your servlet becomes /foobar.do. You'll then need to ask your sysadmin to specify that *.do goes to tomcat. Ask your sysadmin to configure apache to send all requests to tomcat apart of those for static files. E.g *.jpg, *.png, *.css, *.js etc
就我而言,它可以很好地处理像/register.do 这样的 url:)
关于java - Spring应用程序对第三方应用程序上的所有URL请求(除了/)返回404(在本地主机上工作正常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8093876/