Spring MVC 404错误
我疯了,无法理解问题所在:
我有以下结构:
SpringMVC +WebContent -web-inf -web.xml -springMVC-servlet.xml -index.jsp -security -login.jsp
web.xml中
springMVC springMVC org.springframework.web.servlet.DispatcherServlet 1 springMVC *.html springMVC *.do springMVC /index.html index.html
用SpringMVC-servlet.xml中
/ .jsp
我的控制器:
package com.vanilla.springMVC; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.portlet.ModelAndView; @Controller public class DefaultController { @RequestMapping(value="/index.html", method=RequestMethod.GET) public ModelAndView index(){ ModelAndView mv = new ModelAndView("index"); return mv; } @RequestMapping(value="/login.html", method=RequestMethod.GET) public ModelAndView loginPage(){ ModelAndView mv = new ModelAndView("/security/login"); return mv; } }
导航到/index.html没有问题
http:// localhost:8080 / SpringMVC / index.html完美无缺。
但是当我导航到http:// localhost:8080 / SpringMVC / login.html时,我有404错误。
HTTP Status 404 - /SpringMVC/login.jsp type Status report message /SpringMVC/login.jsp description The requested resource (/SpringMVC/login.jsp) is not available.
我不想将login.jsp移动到与index.jsp相同的级别,但为什么我有这个问题?
HTTP 404表示找不到资源。
- 这意味着找不到控制器或直接访问的资源(如CSS文件)
- 它并不意味着找不到控制器中引用的JSP(这将是500内部服务器错误)
HTTP状态404 – /SpringMVC/login.jsp
看起来您发送了HTTP请求/SpringMVC/login.jsp
但您的控制器方法绑定到.html
,因此您需要将您的HTTP请求更改为/SpringMVC/login.html
由于名称(登录),您的Spring Security配置可能不正确。
我只是在这里添加它,因为它解决了我的404问题。 原来我的问题是web.xml
的url-mapping
值。 使用Spring WebMVC版org.springframework:spring-webmvc:4.1.6.RELEASE
我使用以下(不起作用):
/rest
我应该使用以下值(有效):
/rest/*
要么
/
参考: http : //docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html
-
在WEB-INF“jsps”下为您的所有视图创建一个文件夹,在“jsps”下为您的案例创建一个“security”文件夹。 注意:将JSP文件移动到WEB-INF中,可以防止直接访问这些文件。 它是应用程序安全性所必需的。 考虑一种情况,在这种情况下,您的JSP会提供有关您客户的个人信息,并且您的控制器必须授予/检查访问权限。 如果您的JSP存在于WEB-INF之外,则可以直接在它们上访问它们。
-
像这样配置你的视图解析器:
接下来,将您的登录JSP放入“WEB-INF / jsps / security”并从您的loginPage返回“security / login”。
现在,视图解析器搜索“WEB-INF / jsps”下的视图。 因为您的方法返回“security / login”,视图解析器需要一个名为“security”的jsps下的目录和一个名为“login.jsp”的后缀的JSP文件(后缀= jsp)
我怀疑ModelAndView mv = new ModelAndView("/security/login");
是问题所在。 确保根目录中有一个目录“security”文件夹,它包含一个login.jsp文件。 或者尝试在WebContent中移动安全性文件夹
提供DispatcherServlet的contextConfigLocation init参数
appServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/springMVC-servlet.xml 1
我正在使用带有eclipse和MacOS X系统的Spring MVC,并在控制器返回正确的jsp名称时遇到404错误。 后来我发现有问题的jsp文件在文件系统上没有x(执行)权限。 然后我`chmod + x some.jsp’问题解决了。
在这里添加此ans以防万一其他人面临同样的问题。 我的应用程序之前工作正常,然后我升级了Spring版本,并在此过程中还升级了spring-security版本。 当我尝试访问我的登录html页面时,我遇到了这个问题。 我已经完成了所有必要的配置更改,因为spring-security版本从3更改为4.我提到了这个链接 ,它非常有用。
由于我的web-servlet.xml文件中的以下配置,我遇到了这个问题
在我将两个位置值修改为"/app/"
后,它工作正常。 似乎较新版本的spring匹配整个字符串或检查请求的位置是否以上面配置的位置值开头。
我在org.springframework.web.servlet.resource.PathResourceResolver中找到了以下代码:
if (locationPath.equals(resourcePath)) { return true; } locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/"); if (!resourcePath.startsWith(locationPath)) { return false; }
第二个if条件在我的情况下评估为false,因此404 HTTP响应
另一个提示,如果您遇到此类错误,请尝试启用最高级别的springframework日志记录。 您可能从那里了解错误原因。
对于上述问题,这个答案可能已经过时了,但它会帮助刚刚开始spring的其他人。 我也在苦苦寻找404 WEB-INF / welcome.jsp页面找不到错误。
这里的问题是使用ModelAndView导入
更换
import org.springframework.web.servlet.ModelAndView;
同
import org.springframework.web.portlet.ModelAndView;